Pipeline Structure

A declarative pipeline always has an agent, one or more stages, and steps inside each stage.

Every declarative pipeline follows the same skeleton. Learning this structure once means you can read almost any Jenkinsfile.

The required blocks

  • pipeline { } — wraps the whole thing.
  • agent — where the pipeline runs (a machine or container).
  • stages { } — a container for one or more stages.
  • stage('Name') { } — a named phase of work.
  • steps { } — the commands inside a stage.
Nesting of a declarative pipeline: pipeline, agent, stages, stage, stepspipeline { }agent { } — where it runsstages { }stage('Build') { }steps { sh 'make' } — the commands
Each block nests inside the one above it: pipeline > agent + stages > stage > steps.

Example

Example · bash
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'echo Building the project'
            }
        }
        stage('Test') {
            steps {
                sh 'echo Running tests'
            }
        }
    }
}

When to use it

  • An engineer uses the standard pipeline skeleton to create a new Jenkinsfile and fills in only the stages relevant to their app.
  • A trainer shows students the required blocks — pipeline, agent, stages, stage, steps — so they can read any Jenkinsfile in the wild.
  • A team adds an optional environment block and post block to the skeleton to handle secrets and cleanup notifications.

More examples

Required blocks only

The absolute minimum valid declarative pipeline with only the required pipeline, agent, stages, stage, and steps blocks.

Example · groovy
pipeline {
  agent any
  stages {
    stage('Hello') {
      steps {
        echo 'Pipeline structure is working'
      }
    }
  }
}

Full skeleton with optional blocks

Extends the required skeleton with the optional environment, options, and post blocks.

Example · groovy
pipeline {
  agent any
  environment { APP = 'myapp' }
  options { timeout(time: 30, unit: 'MINUTES') }
  stages {
    stage('Build') { steps { sh 'make' } }
    stage('Test')  { steps { sh 'make test' } }
  }
  post {
    failure { echo 'Build failed!' }
    success { echo 'Build passed!' }
  }
}

Nested stage with parallel

Uses the parallel block inside a stage to run unit tests, linting, and type-checking simultaneously.

Example · groovy
pipeline {
  agent any
  stages {
    stage('Tests') {
      parallel {
        stage('Unit')        { steps { sh 'npm test' } }
        stage('Lint')        { steps { sh 'npm run lint' } }
        stage('Type Check')  { steps { sh 'npx tsc --noEmit' } }
      }
    }
  }
}

Discussion

  • Be the first to comment on this lesson.