Recently started to take them more seriously to see how much work it would be to take my current work flow and augment their power. Most of my jobs are bash scripts that make Jenkins a glorified cron job. Previously Jenkins was very rudimentary, e.g. I just recently started using Jenkins plug-ins for config injection and secret obfuscation. Now I’m trying to take those newly learned processes and add a whole new DSL on top.

Jenkins Pipelines still lack the myriad of example to make things quick, so a searching I went. The first thing was to figure out how to wholesale copy and paste the bash scripts; this also brings up the need to use the scripted version instead of the declarative version.

Example 1

Lang: groovy
node('master'){
  stage('HW1'){
    echo 'hello world'
  }
}

output:

Lang:
hello world

Example 2

Lang: groovy
node('master'){
  stage('HW2'){
    sh 'echo hello world'
  }
}

output:

Lang:
[in_jenkins-dsl-test_hw_test-L6OPGZSILNYU4LFJKDMY4AGUBMSB2FT3Z3UAW66ODUIPZN4WUNBA] Running shell script
+ echo hello world
hello world

Both Example 1 and 2 do the same thing, but example 2 uses the shell. At the start of my Jenkins Pipelines using the shell, which mean Groovy and bash and even awk string interpolation. This is extra fun since Groovy/JenkinsDSL uses ${VAR1} and bash uses the same thing. So the careful usage of single quotes, double quotes, and character escaping is a must; not to forget the triple-single-quotes and triple-double-quotes.

Example 3

Lang: groovy
node('master'){
  stage('HW3'){
    def hw = 'hello world'
    sh 'echo \'Echo a var: ${hw}\''
  }
}

output:

Lang:
[in_jenkins-dsl-test_hw_test-L6OPGZSILNYU4LFJKDMY4AGUBMSB2FT3Z3UAW66ODUIPZN4WUNBA] Running shell script
+ echo 'Echo a var: ${hw}'
Echo a var: ${hw}

Example 4

Lang: groovy
node('master'){
  stage('HW4'){
    def hw = 'hello world'
    sh "echo 'Echo a var: ${hw}'"
  }
}

output:

Lang:
[in_jenkins-dsl-test_hw_test-L6OPGZSILNYU4LFJKDMY4AGUBMSB2FT3Z3UAW66ODUIPZN4WUNBA] Running shell script
+ echo 'Echo a var: hello world'
Echo a var: hello world

Example 5

Lang: groovy
node('master'){
  stage('HW5'){
    def var5 = 'hello world'
    sh "echo 'Echo a var: ${var5}' \${SHELL}"
  }
}

output:

Lang:
[in_jenkins-dsl-test_hw_test-L6OPGZSILNYU4LFJKDMY4AGUBMSB2FT3Z3UAW66ODUIPZN4WUNBA] Running shell script
+ echo 'Echo a var: hello world' /sbin/nologin
Echo a var: hello world /sbin/nologin