Reputation: 7
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:
echo ANALYSIS_URL=$ANALYSIS_URL > my.properties
$WORKSPACE/my.properties
Current analysis url: $ANALYSIS_URL
Upvotes: 0
Views: 16337
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}"
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
Reputation: 11
Printing Variables in Post Build Email:
echo count=$count > count.txt
echo distinctcount=$distinctcount > distinctcount.txt
${WORKSPACE}/count.txt
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
Count total:$count Distinct Count: $distinctcount
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:
Upvotes: 1
Reputation: 602
Have you tried the env.Analysis_URL
to access to your environment variable ?
Upvotes: 1