0
votes

I'm trying to send email notifications to specific user whose branch is currently checked out in the Jenkins build process. For accomplishing this, I have used naming conventions in the environment variables.

E.g.

  1. master_MAIL = [email protected]
  2. ABC_MAIL = [email protected]

Where MASTER and ABC are actual branch names.

env.GIT_LOCAL_BRANCH gives me with the current branch name being checked out.

Based on the current branch name, I have to pick the correct environment variable, and store its value (mail ID) to another environment variable.

pipeline {
    agent any

    environment {
        CURRENT_MAIL = "\$${env.GIT_LOCAL_BRANCH}_MAIL"
        // Here I am trying to pick the specific mail id of the branch being
        // check out and store it to new environment variable named 
        // "CURRENT_MAIL".
        // But This gives only the value "$master_MAIL" but not the value
        // stored in master_MAIL environment variable.
    }

    stages {
    }

    post {
        always {
            mail (
            from: ""
            to: "$CURRENT_MAIL"
            // Here I have to use that environment variable to send email to
            // that specific user.
            subject: "")
        }
    }

The expected result is: CURRENT_MAIL environment variable should have the value contained in the {Branchname}_MAIL environment variable.

E.g. Consider I have these two environment variables set in my configuration:

master_MAIL = [email protected]
ABC_MAIL = [email protected]

Then, if the current branch being checked out is master branch, then the environment variable CURRENT_MAIL should have the value [email protected].

1
When indenting your question's source code I've noticed there is a missing curly bracket closing the pipeline block. Is it also like that in your jenkins configuration, or is it just a copy & paste error to Stack Overlow?sɐunıɔןɐqɐp

1 Answers

0
votes

You need the groovy evaluate() helper method for dynamic evaluation of groovy expressions.

environment {
    CURRENT_MAIL = evaluate(env.GIT_LOCAL_BRANCH + '_MAIL')
    // OR using Groovy GStrings as
    // CURRENT_MAIL = evaluate("${env.GIT_LOCAL_BRANCH}_MAIL")
}