Skip to content
Intermediate CI/CDLesson 3 of 5 2 min read

Writing a Jenkinsfile

Declarative pipeline syntax: stages, steps, and keeping your pipeline checked into source control.

On this page3 sections

A Jenkinsfile is a text file, committed to your repository, that describes your entire pipeline. This is pipeline as code: the build process gets reviewed in pull requests, versioned alongside the app, and never lives only in someone's memory of how the Jenkins UI was clicked through.

Declarative pipeline shape

The most common style is the declarative pipeline - a structured block that reads almost like a checklist:

groovy
pipeline {
  agent any

  stages {
    stage('Build') {
      steps {
        sh 'npm install'
        sh 'npm run build'
      }
    }
    stage('Test') {
      steps {
        sh 'npm test'
      }
    }
    stage('Deploy') {
      steps {
        sh './deploy.sh'
      }
    }
  }

  post {
    failure {
      echo 'Pipeline failed - notify the team'
    }
  }
}

Reading it piece by piece

  • agent any - run this pipeline on whichever available agent Jenkins picks.
  • stages - the ordered list of phases. Each stage shows up as its own box in the Jenkins UI, so you can see exactly which one failed.
  • steps - the actual shell commands (sh '...') that do the work inside a stage.
  • post - runs after everything else, regardless of outcome - the natural place for notifications, cleanup, or artifact archiving.

Why the file lives in your repo

Because the Jenkinsfile travels with the code, a pull request that changes the test command, or adds a new deployment step, is reviewed exactly like any other code change - no separate "go click this setting in Jenkins" step that someone inevitably forgets to do.

Create one now:

bash
touch Jenkinsfile

When you're ready, hit Mark complete and move to the next lesson.

Challenge

Create an empty file named Jenkinsfile in the current directory - the first step toward pipeline-as-code.

Want to experiment first? Try things in the practice terminal (opens in a new tab), then enter the command that solves the task here.

Quiz

Lock in the concepts. Score at least 80% to mark the lesson complete.

0 of 2 answered

Done with this one?

Skipped the challenge? You can mark the lesson complete by hand.

Your notes

Kept on this browser only.