user1109517
user1109517

Reputation: 39

Jenkins Pipeline, use an Env Var within emailext plugin

My Pipeline is generating a dynamic recipient list based on each Job execution.I'm trying to use that list which I set it as a Variable, to use in the 'To' section of the emailext plugin, the Problem is that the Content of the variable is not resolved once using the mailext part.

pipeline {
    agent {
        label 'master'
    }
    options {
        timeout(time: 20, unit: 'HOURS') 
    }
    stages {
        stage('Find old Projects') {
            steps {
                sh '''
                find  $JENKINS_HOME/jobs/* -type f -name "nextBuildNumber" -mtime +1550|egrep -v "configurations|workspace|modules|promotions|BITBUCKET"|awk -F/ '{print $6}'|sort -u  >results.txt
                '''
            }
        }
        stage('Generate recipient List') {
            steps {
                sh '''
                        for Project in `cat results.txt`
                        do
                            grep "mail.com" $JENKINS_HOME/jobs/$Project/config.xml|grep -iv "Ansprechpartner" | awk -F'>' '{print $2}'|awk -F'<' '{print $1}'>> recipientList.txt
                        done
                        recipientList=`sort -u recipientList.txt`
                        echo $recipientList                     
                    '''
                }
            }
        stage('Generate list to Shelve or Delete') {
            steps {
                sh '''
                    for Project in `cat results.txt`
                    do
                        if [ -f "$JENKINS_HOME/jobs/$Project/nextBuildNumber" ]; then
                            nextBuildNumber=`cat $JENKINS_HOME/jobs/$Project/nextBuildNumber`
                            if [ $nextBuildNumber == '1' ]; then
                                echo "$JENKINS_HOME/jobs/$Project" >> jobs2Delete.txt
                                echo "$Project" >> jobList2Delete.txt
                            else
                                echo "$JENKINS_URL/job/$Project/shelve/shelveProject" >> Projects2Shelve.txt
                                echo "$Project" >> ProjectsList2Shelve.txt
                            fi
                        fi
                    done
                '''
            }
        }
        stage('Send email') {
            steps {
                        emailext    to:     '[email protected]',
                        from:       '[email protected]',
                        attachmentsPattern: 'ProjectsList2Shelve.txt,jobList2Delete.txt',
                        subject:    "This is a subject", 
                        body:       "Hello\n\nAttached two lists of Jobs, to archive or delete,\nPlease Aprove or Abort the Shelving / Delition of the Projects:\n${env.JOB_URL}\n\nBlue Ocean:\n${env.RUN_DISPLAY_URL}\n\nyour Team"
            }
        }
        stage('Aprove or Abort') {
            steps {
                input message: 'OK to Shelve and Delete projects? \n Review the jobs list (Projects2Shelve.txt, jobs2Delete.txt) sent to your email', submitter: 'someone'
            }
        }
        stage('Shelve or Delete') {
            parallel {
                stage('Shelve Project') {
                    steps {
                        withCredentials([usernamePassword(credentialsId: 'XYZ', passwordVariable: 'PA', usernameVariable: 'US')]) {
                        sh '''
                            for job2Shelve in `cat Projects2Shelve.txt`
                            do
                                curl -u $US:$PA $job2Shelve
                            done
                        '''
                        }
                    }
                }
                stage('Delete Project') {
                    steps {
                        sh '''
                            for job2Del in `cat jobs2Delete.txt`
                            do
                                echo "Removing $job2Del"
                            done
                        '''
                    }
                }
            }
        }   
    }
    post {
        success {
            emailext    to:     "$recipientListTest",
                        from:       '[email protected]',
                        attachmentsPattern: 'Projects2Shelve.txt,jobs2Delete.txt',
                        subject:    "This is a sbject", 
                        body:       "Hallo\n\nAttached two lists of Jobs which archived or deleted due to inactivity of more the 400 days\n\n\nyour Team"
        }
    }
}

Upvotes: 2

Views: 1446

Answers (2)

user1109517
user1109517

Reputation: 39

I figured out that the only way would be to add a script part as part of the post section, together with a variable Definition outside of the Pipeline block:

post {
        success {
            script {
                    RECIPIENTLIST = sh(returnStdout: true, script: 'cat recipientListTest.txt')
                                    }
            emailext    to:         "${RECIPIENTLIST}",
                        from:       '[email protected]',
                        attachmentsPattern: 'Projects2Shelve.txt,jobs2Delete.txt',
                        subject:    "MY SUBJECT", 
                        body:       "MY BODY"
        }

Upvotes: 1

Chris Maes
Chris Maes

Reputation: 37742

when you execute a sh command, you cannot reuse the variables that you set within that command. You need to do something like this:

on top you your pipeline file to make this variable global

def recipientsList

then execute your shell command and retrieve the output

recipientsList = sh (
                script: '''for Project in `cat results.txt`
                    do
                        grep "mail.com" $JENKINS_HOME/jobs/$Project/config.xml|grep -iv "Ansprechpartner" | awk -F'>' '{print $2}'|awk -F'<' '{print $1}'>> recipientList.txt
                    done
                    recipientList2=`sort -u recipientList.txt`
                    echo $recipientList2
                ''',
                returnStdout: true
            ).trim()

Now in your email you can use the variable $recipientList...

I renamed your bash variable to recipientList2 to avoid confusion.

EDIT: I don't know what you want to obtain, but consider using some default recipients provided by emailext:

recipientProviders: [ developers(), culprits(), requestor(), brokenBuildSuspects(), brokenTestsSuspects() ],

Upvotes: 0

Related Questions