Defining Pipeline

On this page

Jenkins Pipeline is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins. Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines "as code" via the Pipeline DSL. [1]

Typically, this "Pipeline as Code" would be written to a Jenkinsfile and checked into a project’s source control repository, for example:

// Declarative //
pipeline {
    agent any (1)

    stages {
        stage('Build') { (2)
            steps { (3)
                sh 'make' (4)
            }
        }
        stage('Test'){
            steps {
                sh 'make check'
                junit 'reports/**/*.xml' (5)
            }
        }
        stage('Deploy') {
            steps {
                sh 'make publish'
            }
        }
    }
}
// Script //
node {
    stage('Build') {
        sh 'make'
    }

    stage('Test') {
        sh 'make check'
        junit 'reports/**/*.xml'
    }

    stage('Deploy') {
        sh 'make publish'
    }
}
1 agent indicates that Jenkins should allocate an executor and workspace for this part of the Pipeline.
2 stage describes a stage of this Pipeline.
3 steps describes the steps to be run in this stage
4 sh executes the given shell command
5 junit is a Pipeline step provided by the plugin:junit[JUnit plugin] for aggregating test reports.

Pipeline terms

Step

A single task; fundamentally steps tell Jenkins what to do. For example, to execute the shell command make use the sh step: sh 'make'. When a plugin extends the Pipeline DSL, that typically means the plugin has implemented a new step.

Node

Most work a Pipeline performs is done in the context of one or more declared node steps. Confining the work inside of a node step does two things:

  1. Schedules the steps contained within the block to run by adding an item to the Jenkins queue. As soon as an executor is free on a node, the steps will run.

  2. Creates a workspace (a directory specific to that particular Pipeline) where work can be done on files checked out from source control.

Depending on your Jenkins configuration, some workspaces may not get automatically cleaned up after a period of inactivity. See tickets and discussion linked from JENKINS-2111 for more information.
Stage

stage is a step for defining a conceptually distinct subset of the entire Pipeline, for example: "Build", "Test", and "Deploy", which is used by many plugins to visualize or present Jenkins Pipeline status/progress. [2] [3]

Getting started with Pipeline

A basic Pipeline can be created in either of the following ways:

  • By entering a script directly in the Jenkins web UI.

  • By creating a Jenkinsfile which can be checked into a project’s source control repository.

The syntax for defining a Pipeline with either approach is the same, but while Jenkins supports entering Pipeline directly into the web UI, it’s generally considered best practice to define the Pipeline in a Jenkinsfile which Jenkins will then load directly from source control. [4]

The Jenkins project maintains a Pipeline Syntax Reference which may useful to refer to when creating a Jenkinsfile.