What is a Pipeline?

A Jenkins Pipeline models your entire build-to-deploy process as code, in stages that run in sequence.

A Pipeline is Jenkins' modern way to define your whole delivery process — build, test, deploy — as code rather than clicking through forms.

Why pipelines beat freestyle jobs

  • As code — the definition lives in a Jenkinsfile in your repo, so it is versioned and reviewed like any code.
  • Durable — a pipeline survives a Jenkins restart and can resume.
  • Visual — Jenkins shows each stage as a box, so you see exactly where a build is or where it failed.
  • Powerful — loops, conditionals, parallel branches, and shared libraries are all possible.

Stages and steps

A pipeline is organized into stages (like Build, Test, Deploy), and each stage contains steps (the actual commands).

Example

Example · bash
pipeline {
    agent any
    stages {
        stage('Build') {
            steps { echo 'Building...' }
        }
        stage('Test') {
            steps { echo 'Testing...' }
        }
        stage('Deploy') {
            steps { echo 'Deploying...' }
        }
    }
}

When to use it

  • A backend team replaces a fragile series of freestyle jobs with a single pipeline that build, tests, and deploys atomically.
  • A DevOps engineer stores the pipeline definition in Git so that any change to the build process goes through code review.
  • An organization uses pipelines to enforce that no code reaches production without passing both unit and integration test stages.

More examples

Minimal declarative pipeline

A three-stage declarative pipeline that builds, tests, and deploys a Maven project.

Example · groovy
pipeline {
  agent any
  stages {
    stage('Build')  { steps { sh 'mvn package -q' } }
    stage('Test')   { steps { sh 'mvn test' } }
    stage('Deploy') { steps { sh './deploy.sh' } }
  }
}

Pipeline with environment variables

Defines pipeline-wide environment variables and uses them in a Docker tagging step.

Example · groovy
pipeline {
  agent any
  environment {
    APP_NAME = 'my-service'
    VERSION  = "1.0.${BUILD_NUMBER}"
  }
  stages {
    stage('Tag') {
      steps { sh "docker tag ${APP_NAME}:latest ${APP_NAME}:${VERSION}" }
    }
  }
}

Pipeline with post notifications

Adds a post block that emails the team when the pipeline fails.

Example · groovy
pipeline {
  agent any
  stages {
    stage('Build') { steps { sh 'make all' } }
  }
  post {
    success { echo 'Pipeline succeeded!' }
    failure { mail to: '[email protected]',
                   subject: "FAILED: ${JOB_NAME} #${BUILD_NUMBER}",
                   body: "Check ${BUILD_URL}" }
  }
}

Discussion

  • Be the first to comment on this lesson.