Recently I found myself having to call a bunch of different shell tools inside of a Jenkinsfile. I really struggled to understand how to set, modify and view environment variables. Mostly this came down to the fact that my knowledge of groovy is minimal. Time for that to change.
Of course the new Jenkins pipeline has two different DSLs available. Both are groovy. This allows you to utilize the quite flexible string interpolation features in groovy.
Simple Declarative
Jenkins is a glue tool that holds everything else together. For me, this is often other scripts, tools accessed via the shell. I wanted to demonstrate some of the different ways to do environment variables and string interpolation with the shell, because the syntax is close enough to be really confusing. It is important to choose the correct quotes because this will determine if the shell or groovy interprets a variable.
[Declarative Pipeline] Running shell script + echo Shell /var/jenkins_home/workspace/Declarative Pipeline Shell /var/jenkins_home/workspace/Declarative Pipeline [Pipeline] sh [Declarative Pipeline] Running shell script + echo Shell /var/jenkins_home/workspace/Declarative Pipeline Shell /var/jenkins_home/workspace/Declarative Pipeline [Pipeline] sh [Declarative Pipeline] Running shell script + echo Groovy / Groovy /
Complicated Declarative
Next up is if you want to create new environment variables from old ones, for instance including BUILD_ID into another variable.
Some notes:
- Line 7: You can set environment variables for a block by declaring an environment block. You’ll want to use straight groovy string interpolation to define variables (if necessary). This is one way to escape the declarative pipeline restrictions, because everything inside of the brackets is evaluated as groovy expressions. You should note that I went out of my way to make this example a little goofy, showing the use of groovy expressions inside of the string interpolation. It is equally valid to say
AWESOME_FILE = "${env.BUILD_ID}" + "/working"
with the same result, just using groovy string concatenation - Lines 13 and 15 should remind you that each
sh
is its own interpreter. You start afresh each time. - Lines 18 through 21 are also an example of a sensible way to string together some shell commands in a readable and sensible fashion (since they execute sequentially in the same shell)
[Pipeline] sh [Declarative Complicated Environment] Running shell script + mkdir -p 7/working [Pipeline] sh [Declarative Complicated Environment] Running shell script + touch 7/working/the_file [Pipeline] sh [Declarative Complicated Environment] Running shell script + export AWESOME_FILE=7/working/the_file [Pipeline] sh [Declarative Complicated Environment] Running shell script + echo [Pipeline] sh [Declarative Complicated Environment] Running shell script + export AWESOME_FILE=7/working/the_file + echo 7/working/the_file 7/working/the_file
Hello,
Do you know how can I do :
env.NETWORK = sh “$(echo mon-projet${env.JOB_BASE_NAME}${env.BUILD_ID} | sed ‘s/[^a-zA-Z0-9]*//g’)”
env.NETWORK_NAME = “${env.NETWORK}_backend”
env.NETWORK > fail jenkins and set “No stepsThis stage has no steps”
env.NETWORK = sh “$(echo mon-projet${env.JOB_BASE_NAME}${env.BUILD_ID} | sed ‘s/[^a-zA-Z0-9]*//g’)”
You should probably just replace this with some actual groovy, since it can do all the string manipulation you are trying to do in the shell…
Dude, you saved my life. 2 days with problems. Thank you very much!!!
echo STEP 2 BUILD
echo “Run tests”
cd /d %PROJECTDIR%
ECHO “Going to set custom parameters”
SET VMNAME_FQDN=%VMNAME%.int.kronos.com
SET customParams=%customParams% -Dctx.Environment.Active.WinRM.VM1.host^=%VMNAME_FQDN%
SET customParams=%customParams% -Dctx.Environment.Active.name^=%ENVNAME%
SET customParams=%customParams% -Dctx.TestData.Wfc.DriveLetter^=%DIRECTORY%
SET customParams=%customParams% -Dctx.Expected.Wfan.InitialVersion^=%WFAN_INITIAL_VERSION%
SET customParams=%customParams% -Dctx.Expected.Wfan.TargetVersion^=%WFAN_TARGET_VERSION%
SET customParams=%customParams% -Dctx.Expected.Wfc.BasePack^=%WFC_BASE_VERSION%
SET customParams=%customParams% -Dctx.Expected.Wfc.ServicePack^=%WFC_SERVICE_PACK_VERSION%
SET customParams=%customParams% -Dctx.TestData.Wfan.WfanInstaller.UseMainInstaller^=%USE_MAIN_INSTALLER%
IF “%VERTICAL%”==”Wh” (
SET customParams=%customParams% -Dctx.TestData.SharedDrive.Wfan.TargetReleaseWaKitsLocation^=%LATEST_WA_RELEASE_LOCATION% -Dctx.TestData.SharedDrive.Wfan.TargetReleaseVrKitsLocation^=%LATEST_WH_RELEASE_LOCATION%
)
IF “%VERTICAL%”==”Wm” (
SET customParams=%customParams% -Dctx.TestData.SharedDrive.Wfan.TargetReleaseWaKitsLocation^=%LATEST_WA_RELEASE_LOCATION% -Dctx.TestData.SharedDrive.Wfan.TargetReleaseVrKitsLocation^=%LATEST_WM_RELEASE_LOCATION%
)
IF “%VERTICAL%”==”Wr” (
SET customParams=%customParams% -Dctx.TestData.SharedDrive.Wfan.TargetReleaseWaKitsLocation^=%LATEST_WA_RELEASE_LOCATION% -Dctx.TestData.SharedDrive.Wfan.TargetReleaseVrKitsLocation^=%LATEST_WR_RELEASE_LOCATION%
)
call mvn test -s setting.xml -fae %customParams% -Dcucumber.options=”–tags @net%SCENARIO%%VERTICAL%RemoveWfan” && call mvn site -s setting.xml
call mvn test -s setting.xml -fae %customParams% -Dcucumber.options=”–tags @net%SCENARIO%%VERTICAL%RemoveWfc” && call mvn site -s setting.xml
call mvn test -s setting.xml -fae %customParams% -Dcucumber.options=”–tags @net%SCENARIO%%VERTICAL%CopyRelease” && call mvn site -s setting.xml
call mvn test -s setting.xml -fae %customParams% -Dcucumber.options=”–tags @net%SCENARIO%%VERTICAL%Wfc” && call mvn site -s setting.xml
call mvn test -s setting.xml -fae %customParams% -Dcucumber.options=”–tags @net%SCENARIO%%VERTICAL%DbWfan” && call mvn site -s setting.xml
call mvn test -s setting.xml -fae %customParams% -Dcucumber.options=”–tags @net%SCENARIO%%VERTICAL%Wfan” && call mvn site -s setting.xml
call mvn test -s setting.xml -fae %customParams% -Dcucumber.options=”–tags @net%SCENARIO%%VERTICAL%ConfigureWfanAndWfc” && call mvn site -s setting.xml
call mvn test -s setting.xml -fae %customParams% -Dcucumber.options=”–tags @net%SCENARIO%%VERTICAL%ETL” && call mvn site -s setting.xml
call mvn test -s setting.xml -fae %customParams% -Dcucumber.options=”–tags @net%SCENARIO%%VERTICAL%ValidationScripts” && call mvn site -s setting.xml
call mvn test -s setting.xml -fae %customParams% -Dcucumber.options=”–tags @net%SCENARIO%%VERTICAL%AnalyticsOrganizerVerification” && call mvn site -s setting.xml
I want to set these custom params accordingly in jenkins pipeline using DSL