Jenkins cheatsheet

Jenkins: Parallel stages, post actions and libraries

5 common Jenkins tasks.

Run stages in parallel

Run independent stages at the same time.

stage('Checks') {
  parallel {
    stage('Unit tests') { steps { sh 'make test' } }
    stage('Lint') { steps { sh 'make lint' } }
  }
}

Run a matrix of combinations

Run the same stages for each combination of axes.

matrix {
  axes {
    axis { name 'NODE'; values '22', '24' }
  }
  stages {
    stage('Test') { steps { sh "echo testing on node ${NODE}" } }
  }
}

React to the result

Run steps after the pipeline depending on the outcome.

post {
  success { echo 'Build passed' }
  failure { mail to: '[email protected]', subject: "Failed: ${env.JOB_NAME}", body: "See ${env.BUILD_URL}" }
  cleanup { cleanWs() }
}

Use a shared library

Load reusable pipeline code defined in another repository.

@Library('my-shared-lib') _

pipeline {
  agent any
  stages {
    stage('Deploy') { steps { deployApp(env: 'staging') } }
  }
}

Use Groovy in a declarative pipeline

A script block runs arbitrary Groovy for logic that declarative syntax can't express.

steps {
  script {
    def version = readFile('VERSION').trim()
    echo "Releasing ${version}"
  }
}

Was this page helpful?