Last active
July 26, 2023 18:53
-
-
Save 24HOURSMEDIA/c44e67bd1743c653084b380223d5e8a3 to your computer and use it in GitHub Desktop.
Jenkinsfile that shows how variables are handled in a regular agent and an agent running docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipeline{ | |
/** | |
* A declarative Jenkins pipeline tjat demonstrates how variables are handled in Jenkins pipelines | |
* and more specifically how they are handled in stages where the agent running the stage is a docker containers. | |
* | |
* EAP Bachman / 24hoursmedia | |
* https://www.24hoursmedia.com | |
*/ | |
agent none | |
// We are going to examine how parameters are available in the normal agent and in the docker container | |
parameters { | |
string(defaultValue: "foo param 1", description: "myParam1", name: "myParam1") | |
string(defaultValue: "bar param 2", description: "myParam2", name: "myParam2") | |
} | |
// Same for environment, also we show how env variables can be created dynamically from parameters | |
environment { | |
def myString = "Hello World" | |
def myParamsVal = "${params.myParam1}-${params.myParam2}" | |
} | |
stages{ | |
// Show variables using a regular agent | |
stage("variables"){ | |
agent any | |
steps{ | |
echo "========executing variables========" | |
// Show the variables in the current agent | |
echo "myString: ${myString}" | |
echo "myParamsVal: ${myParamsVal}" | |
echo "myParam1: ${params.myParam1}" | |
echo "myParam2: ${params.myParam2}" | |
// write a file that shows variable content in the agent and executes it | |
echo "======== creating a file with variables and executing it========" | |
writeFile file: './showvars', text: '''/bin/sh | |
echo hello from shellscript | |
echo ${myString} | |
echo ${myParam1} | |
''' | |
sh "chmod +x ./showvars" | |
sh "cat ./showvars" | |
sh "./showvars" | |
} | |
} | |
// Show variables using when the stage is executed in a docker container | |
stage("variables in docker"){ | |
agent { | |
docker { image 'alpine:latest' } | |
} | |
steps { | |
// Make sure sh executes in the docker container | |
// The output should show 'alpine' | |
sh "cat /etc/os-release" | |
// This demonstrates variables in the sh commands, | |
// but they are substituted before the command is executed in the container | |
sh "echo === VARIABLE SUBSTITUTION ===" | |
sh "echo ${myString}" | |
sh "echo ${myParamsVal}" | |
sh "echo ${params.myParam1}" | |
echo "" | |
// Print the environment variables available in docker | |
// If you print all environment variables, you will see the params | |
// and env variables as myParam1, myParam2, myString, myParamsVal | |
sh "echo === ALL ENV VARIABLES ===" | |
sh "printenv" | |
// This confirms the variables would be availble to a script in the container, | |
// we run the script we created in the previous stage but now in the docker container | |
// used in this stage | |
sh "echo === EXECUTING SCRIPT IN DOCKER ===" | |
sh "./showvars" | |
} | |
} | |
} | |
post { | |
success{ | |
echo "========pipeline executed successfully ========" | |
} | |
failure{ | |
echo "========pipeline execution failed========" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Started by user 24hoursmedia | |
Replayed #26 | |
[Pipeline] Start of Pipeline | |
[Pipeline] withEnv | |
[Pipeline] { | |
[Pipeline] stage | |
[Pipeline] { (variables) | |
[Pipeline] node | |
Running on Jenkins in /var/jenkins_home/workspace/dev-docker | |
[Pipeline] { | |
[Pipeline] echo | |
========executing variables======== | |
[Pipeline] echo | |
myString: Hello World | |
[Pipeline] echo | |
myParamsVal: foo param 1-bar param 2 | |
[Pipeline] echo | |
myParam1: foo param 1 | |
[Pipeline] echo | |
myParam2: bar param 2 | |
[Pipeline] echo | |
======== creating a file with variables and executing it======== | |
[Pipeline] writeFile | |
[Pipeline] sh | |
+ chmod +x ./showvars | |
[Pipeline] sh | |
+ cat ./showvars | |
/bin/sh | |
echo hello from shellscript | |
echo ${myString} | |
echo ${myParam1} | |
[Pipeline] sh | |
+ ./showvars | |
hello from shellscript | |
Hello World | |
foo param 1 | |
Post stage | |
[Pipeline] echo | |
========always======== | |
[Pipeline] echo | |
========variables executed successfully======== | |
[Pipeline] } | |
[Pipeline] // node | |
[Pipeline] } | |
[Pipeline] // stage | |
[Pipeline] stage | |
[Pipeline] { (variables in docker) | |
[Pipeline] node | |
Running on Jenkins in /var/jenkins_home/workspace/dev-docker | |
[Pipeline] { | |
[Pipeline] isUnix | |
[Pipeline] withEnv | |
[Pipeline] { | |
[Pipeline] sh | |
+ docker inspect -f . alpine:latest | |
. | |
[Pipeline] } | |
[Pipeline] // withEnv | |
[Pipeline] withDockerContainer | |
Jenkins seems to be running inside container 3bfaadfe2e0111f7176f8ac7f211b6041b49d2fba925943dd3f3ff242c4a35b0 | |
$ docker run -t -d -u 0:0 -w /var/jenkins_home/workspace/dev-docker --volumes-from 3bfaadfe2e0111f7176f8ac7f211b6041b49d2fba925943dd3f3ff242c4a35b0 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** alpine:latest cat | |
$ docker top cd0db34bdb4bb2b9b9640251c1b7fd846869b49341ad568b55f70b1f86faef3c -eo pid,comm | |
[Pipeline] { | |
[Pipeline] sh | |
+ cat /etc/os-release | |
NAME="Alpine Linux" | |
ID=alpine | |
VERSION_ID=3.17.3 | |
PRETTY_NAME="Alpine Linux v3.17" | |
HOME_URL="https://alpinelinux.org/" | |
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues" | |
[Pipeline] sh | |
+ echo '===' VARIABLE SUBSTITUTION '===' | |
=== VARIABLE SUBSTITUTION === | |
[Pipeline] sh | |
+ ls -l | |
total 4 | |
-rwxr-xr-x 1 root root 70 Jul 26 18:35 showvars | |
[Pipeline] sh | |
+ echo Hello World | |
Hello World | |
[Pipeline] sh | |
+ echo foo param 1-bar param 2 | |
foo param 1-bar param 2 | |
[Pipeline] sh | |
+ echo foo param 1 | |
foo param 1 | |
[Pipeline] echo | |
[Pipeline] sh | |
+ echo '===' ALL ENV VARIABLES '===' | |
=== ALL ENV VARIABLES === | |
[Pipeline] sh | |
+ printenv | |
CI=true | |
HOSTNAME=cd0db34bdb4b | |
RUN_CHANGES_DISPLAY_URL=https://jenkins.dev.cc/job/dev-docker/27/display/redirect?page=changes | |
NODE_LABELS=built-in | |
HUDSON_URL=https://jenkins.dev.cc/ | |
SHLVL=2 | |
HOME=/root | |
BUILD_URL=https://jenkins.dev.cc/job/dev-docker/27/ | |
JENKINS_SERVER_COOKIE=durable-255b70e6d51c3dab08be8941198f7fb0b5add85458c1d65066003868d94324b9 | |
WORKSPACE=/var/jenkins_home/workspace/dev-docker | |
NODE_NAME=built-in | |
RUN_ARTIFACTS_DISPLAY_URL=https://jenkins.dev.cc/job/dev-docker/27/display/redirect?page=artifacts | |
STAGE_NAME=variables in docker | |
EXECUTOR_NUMBER=1 | |
BUILD_DISPLAY_NAME=#27 | |
RUN_TESTS_DISPLAY_URL=https://jenkins.dev.cc/job/dev-docker/27/display/redirect?page=tests | |
JOB_BASE_NAME=dev-docker | |
HUDSON_HOME=/var/jenkins_home | |
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin | |
BUILD_ID=27 | |
BUILD_TAG=jenkins-dev-docker-27 | |
myParam1=foo param 1 | |
JENKINS_URL=https://jenkins.dev.cc/ | |
myParam2=bar param 2 | |
JOB_URL=https://jenkins.dev.cc/job/dev-docker/ | |
BUILD_NUMBER=27 | |
JENKINS_NODE_COOKIE=eb23c9c9-4053-4063-8599-06b50768cd6d | |
RUN_DISPLAY_URL=https://jenkins.dev.cc/job/dev-docker/27/display/redirect | |
HUDSON_SERVER_COOKIE=ab6c8b6ac85c1205 | |
JOB_DISPLAY_URL=https://jenkins.dev.cc/job/dev-docker/display/redirect | |
myParamsVal=foo param 1-bar param 2 | |
myString=Hello World | |
CLASSPATH= | |
JOB_NAME=dev-docker | |
PWD=/var/jenkins_home/workspace/dev-docker | |
WORKSPACE_TMP=/var/jenkins_home/workspace/dev-docker@tmp | |
GITLAB_OBJECT_KIND=none | |
[Pipeline] sh | |
+ echo '===' EXECUTING SCRIPT IN DOCKER '===' | |
=== EXECUTING SCRIPT IN DOCKER === | |
[Pipeline] sh | |
+ ./showvars | |
hello from shellscript | |
Hello World | |
foo param 1 | |
[Pipeline] } | |
$ docker stop --time=1 cd0db34bdb4bb2b9b9640251c1b7fd846869b49341ad568b55f70b1f86faef3c | |
$ docker rm -f --volumes cd0db34bdb4bb2b9b9640251c1b7fd846869b49341ad568b55f70b1f86faef3c | |
[Pipeline] // withDockerContainer | |
[Pipeline] } | |
[Pipeline] // node | |
[Pipeline] } | |
[Pipeline] // stage | |
[Pipeline] stage | |
[Pipeline] { (Declarative: Post Actions) | |
[Pipeline] echo | |
========always======== | |
[Pipeline] echo | |
========pipeline executed successfully ======== | |
[Pipeline] } | |
[Pipeline] // stage | |
[Pipeline] } | |
[Pipeline] // withEnv | |
[Pipeline] End of Pipeline | |
Finished: SUCCESS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment