Pipelines Primer 1

(this post if part of the material I cover in my devops course)

Jenkins pipelines adds advanced features and abilities to Jenkins, allowing a better solution for CI/CD. Mainly, pipelines are written as code and are saved in VCS (like git).
Jenkins pipelines implementation is made of several plugins added tp Jenkins.
Pipelines are made of stages which are then made of steps.
Let's create one to explore pipelines.

Hello World pipeline

  • The basic way to create a pipeline is to create a Jenkins pipeline job, and write the pipeline code inside the GUI field that is used for that.
  • Create a new pipline:
    • click on + New Item
    • fill in a name (e.g pipe1)
    • select Pipeline
    • click ok at the bottom
  • Paste the following code into the Pipeline (pipeline script) edit windows:
 1pipeline {
 2    agent any
 3    stages { 
 4        stage('Hello') {
 5            steps {
 6                echo 'Hello World'
 7            }
 8        }
 9    }
10}
  • Search for the Build Now button and click it.
  • This is how it looks when I build it (using the Build Now button):
    Hello World pipeline

You can clearly see the Hello stage marked in green because it has succeeded.
If you hover on it, you get access to this stage logs.

Running shell commands

  • Create another pipeline like the first one, but this time:

    • search the This project is parameterized checkbox, click it and create two parmeters of string type with the values 10 and 20.
      for example:
      PAR1 10
      PAR2 20
  • Now go to the pipeline script field, create the Hello World script again

  • Find the Pipeline Syntax link below it, and use it to create a line that would run:
    $((PAR1 + PAR2))
    (which is how bash would numerically add the values of PAR1 and PAR2)

  • run the pipeline job to see the results

  • Here's the pipeline I have used:

 1pipeline {
 2    agent any
 3    stages {
 4        stage('Example') {
 5            steps {
 6                echo 'Hello World'
 7            }
 8        }
 9        stage('Compute') {
10            steps {
11                sh '''
12                echo "result: $((PAR1 + PAR2))"
13                '''
14            }
15        }
16    }
17}