header

Jenkins Pipeline

Getting started

In this technical post we will review basic concepts we need to know in order to create and execute Jenkins Pipeline. A pipeline is an expression of our process to implement Continuous Integration, we write our process using a DSL and this is the basic srtucture using Groovy

#!/usr/bin/env groovy
pipeline {
  agent any
    stages {
      stage('build') {
        steps {
          echo 'Hello World!'
        }
      }
   }
}

where:

  • agent Instructs Jenkins to allocate an executor
  • echo Writes simple string in the console output
  • stage A phase in our continuous integration process
  • steps All steps within our stage

Now lets instruct to Jenkins to obtain your pipeline from Source Control Management (SCM) in this case Github

SCM definition

So, if you save it and click on Build Now button you should be able to see this execution flow in the console output

Started by user josdem
Obtained Jenkinsfile from git https://github.com/josdem/pipeline-workshop.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
Running on Jenkins in /var/lib/jenkins/workspace/pipeline-sandbox
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (build)
[Pipeline] echo
Hello World!
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Now, moving to a real project

#!/usr/bin/env groovy
pipeline {
  agent any
  stages {
    stage('Stop Jugoterapia') {
      steps {
        echo 'Stoping Jugoterapia'
        sh 'sudo systemctl stop jugoterapia-webflux'
        echo 'Done!'
      }
    }
    stage ('Start Jugoterapia Webflux Job') {
      steps {
        echo 'Starting Jugoterapia Build Job'
        build job: 'jugoterapia-webflux'
        echo 'Done!'
      }
    }
    stage('Start Jugoterapia') {
      steps {
        echo 'Starting Jugoterapia'
        sh 'sudo systemctl start jugoterapia-webflux'
        echo 'Done!'
      }
    }
  }
}

where:

  • sh 'sudo systemctl stop jugoterapia-webflux' Is a shell command to execute systemctl stop jugoterapia service
  • build job: 'jugoterapia-webflux' Build another existing job in Jenkins named jugoterapia-webflux
  • sh 'sudo systemctl start jugoterapia-webflux' Starts Jugoterapia service again.

Jugoterapia Pipeline

For more information how to execute shell commands or shell scripts using Jenkins, please go to my previous post Jenkins Shell Execution. To browse the project go here and here, to download the project:

git clone git@github.com:josdem/pipeline-workshop.git

And

git clone git@github.com:josdem/jugoterapia-pipeline.git

Return to the main article

comments powered by Disqus