sadrab
sadrab

Reputation: 7

How to get environment variable in Jenkins email notification?

In Jenkins job configuration I have written a bat script in the command window of build section. In one of the script commands I set an environment variable as a system environment variable in the server machine as so:

setx Analysis_URL "http://analysis_url/analysis/%analysis_id%.html

My task now is to get this environment variable value back to Jenkins and include it in my post build notification email content. Is there a simple way to do it ?

In my research I have come across the plugin envInject but I think it is used for setting environment variables, is that right ?

UPDATE 1 :

It turned out that the variable could be accessed by a simple $Analysis_URL in the email content, however, that raised another issue as my environment variable changes its value after each job build, but as Jenkins only takes a copy of the system environment variables I keep getting the same variable value after each build in my email content, it only changes after restarting Jenkins. Is there a way to get the updated system environment variables to Jenkins ?

UPDATE 2 :

EnvInject plugin did the job I wanted. These are the steps that I performed:

  1. Build step "batch command window": added command :

echo ANALYSIS_URL=$ANALYSIS_URL > my.properties

  1. Build step "Inject environment variables": in field "Properies File Path"

$WORKSPACE/my.properties

  1. Post-Build Actions: "Editable Email Notification", Field "Default Content":

Current analysis url: $ANALYSIS_URL

Upvotes: 0

Views: 16337

Answers (4)

Guilherme Fontenele
Guilherme Fontenele

Reputation: 21

You can use environment variables and global variables inside script with sh. For example:

pipeline {
  environment {
    APP_VERSION = "1.2.1"
  }
  post {
    success {
      script {
        sh "sed -i 's#%BUILD_URL#$BUILD_URL#g' .jenkins/email.html"
        sh "sed -i 's#%APP_VERSION#${APP_VERSION}#g' .jenkins/email.html"
        emailext attachLog: true, mimeType: 'text/html', body: '${FILE, path=".jenkins/email.html"}', subject: '[$BUILD_STATUS] $PROJECT_NAME', to: '[email protected]'
      }
    }
  }
}

But the secret is here:

  • ' single quote parses global variables like $BUILD_STATUS $BUILD_URL ...
  • " double quote parses env variables like ${APP_VERSION}

So, you can use:

subject: '$BUILD_STATUS'

or

subject: "${APP_VERSION}"

Final solution

Transform all global variables you need to environment variables:

pipeline {
  environment {
    APP_VERSION = "1.2.1"
  }
  post {
    success {
      script {
        BUILD_STATUS = '$BUILD_STATUS'
        PROJECT_NAME = '$PROJECT_NAME'
        sh "sed -i 's#%BUILD_URL#$BUILD_URL#g' .jenkins/email.html"
        sh "sed -i 's#%APP_VERSION#${APP_VERSION}#g' .jenkins/email.html"
        emailext attachLog: true, mimeType: 'text/html', body: '${FILE, path=".jenkins/email.html"}', subject: "${BUILD_STATUS} ${PROJECT_NAME} ${APP_VERSION}", to: '[email protected]'
      }
    }
  }
}

Upvotes: 1

Cassandra Rodriguez
Cassandra Rodriguez

Reputation: 11

Printing Variables in Post Build Email:

  1. Add this to build: execute shell section:
echo count=$count > count.txt
echo distinctcount=$distinctcount > distinctcount.txt

execute shell section

  1. Add this to Inject Environmental Variables section under "Properties File Path":
${WORKSPACE}/count.txt

Properties File Path and do the same also for the second file ${WORKSPACE}/distinctcount.txt (as I have two variables)

Note 1: ${WORKSPACE} is pwd for jenkins and lists the location)

Note 2: You can actually put both variables in just one file by using echo distinctcount=$distinctcount >> count.txt

  1. Call the variable in the "default content" section of "editable email notification":
Count total:$count Distinct Count: $distinctcount

editable email notification

NOTE Be sure you have email setup as "always" under "advanced settings" at the bottom of the email settings, otherwise it only emails upon first success or any failure:

advanced settings email setup as "always"

Upvotes: 1

nityanarayan
nityanarayan

Reputation: 378

You can just use: ${yourDefinedVriableName}

Upvotes: -1

Emile
Emile

Reputation: 602

Have you tried the env.Analysis_URL to access to your environment variable ?

Upvotes: 1

Related Questions