Stages and Steps

Stages break your pipeline into logical phases; steps are the actual commands that run inside them.

Syntaxstage('Build') { steps { sh 'make' } }

Stages and steps are the heart of every pipeline.

Stages

A stage is a named block of work, such as Build, Test, or Deploy. Jenkins draws each stage in the Stage View so you can see progress at a glance. Every stage needs a unique name.

Steps

Inside steps { } you list the commands. The most common are:

  • sh 'command' — run a shell command (Linux/macOS).
  • bat 'command' — run a Windows batch command.
  • echo 'message' — print a message to the log.
  • git url: '...' — check out a repository.

Example

Example · bash
pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                git url: 'https://github.com/example/app.git', branch: 'main'
            }
        }
        stage('Build') {
            steps {
                sh 'npm ci'
                sh 'npm run build'
            }
        }
        stage('Test') {
            steps {
                sh 'npm test'
            }
        }
    }
}

When to use it

  • A pipeline has separate Build, Test, and Deploy stages so failures are pinpointed to the exact phase without guessing.
  • A stage groups all linting steps together so the pipeline UI shows a single Lint stage with a clear pass or fail status.
  • An ops pipeline uses a Deploy stage with multiple sh steps to run a Helm upgrade, then verify pod readiness.

More examples

Basic stages and steps

Two stages each containing multiple steps executed sequentially within the stage.

Example · groovy
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        sh 'mvn package -DskipTests'
        echo "Artifact: target/*.jar"
      }
    }
    stage('Test') {
      steps {
        sh 'mvn test'
      }
    }
  }
}

Steps with error handling

Uses catchError to mark a stage as FAILURE when the lint step fails but continue running the audit step.

Example · groovy
stage('Lint') {
  steps {
    catchError(buildResult: 'UNSTABLE', stageResult: 'FAILURE') {
      sh 'npm run lint'
    }
    sh 'npm run audit'
  }
}

Nested stages for grouping

Groups three quality-check stages inside a parent stage so they appear as a collapsible group in the UI.

Example · groovy
stage('Quality Gates') {
  stages {
    stage('Lint')        { steps { sh 'golangci-lint run' } }
    stage('Unit Tests')  { steps { sh 'go test ./...' } }
    stage('Vet')         { steps { sh 'go vet ./...' } }
  }
}

Discussion

  • Be the first to comment on this lesson.