Skip to content

Chapter 5 of 10

Jenkinsfile: Pipeline as Code

Write Declarative and Scripted pipelines with agents, stages, environment, options, and post actions.

44 minutes 10 quick checksBy Subha Prasad
Lesson 5 of 10Course navigation

Lesson content

Read, practise, then check your understanding

Jenkinsfile: Pipeline as Code

A Jenkinsfile gives pipeline changes history, review, branching, and reproducibility. Declarative Pipeline supplies a structured pipeline {} model with validation and standard directives. Scripted Pipeline uses Groovy control flow and offers lower-level flexibility.

Core ideas

  • agent chooses execution capacity globally or per stage.
  • environment declares values; credentials helpers should scope sensitive data narrowly.
  • options configures behavior such as timeouts, timestamps, retry, durability, and retention.
  • post runs actions for states such as always, success, failure, or cleanup.

How it works

Keep the top-level Jenkinsfile readable as a delivery map. Move reusable, tested logic into scripts or a governed Shared Library, not large inline Groovy blocks. Declarative syntax is the default choice for consistent team pipelines; use script only where its flexibility is necessary.

Configuration example

pipeline {
  agent { label 'linux' }
  options {
    timestamps()
    timeout(time: 20, unit: 'MINUTES')
    buildDiscarder(logRotator(numToKeepStr: '30'))
  }
  environment { APP_ENV = 'ci' }
  stages {
    stage('Check') { steps { sh './ci/check.sh' } }
  }
  post {
    always { junit testResults: 'reports/*.xml', allowEmptyResults: true }
    cleanup { deleteDir() }
  }
}

The sample is intentionally small. In a real installation, replace hostnames, labels, credentials, retention, and commands with reviewed values from your own platform standards.

Production guidance

  • Avoid Groovy interpolation of secrets; let the shell expand bound environment variables where safe.
  • Pin external tools and library versions so an old commit remains buildable.
  • Use Replay only for diagnosis, then commit the resulting pipeline change to source control.

The chapter quiz follows this lesson and tests both terminology and safe operating choices.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement correctly describes Jenkinsfile?
Which term matches this explanation: A repository file that defines a Jenkins pipeline as code.
Which statement correctly describes Declarative Pipeline?
Which term matches this explanation: A structured pipeline syntax with a pipeline block and defined sections.
Which statement correctly describes Scripted Pipeline?
Which term matches this explanation: A flexible Groovy-based pipeline syntax built around node blocks.
Which statement correctly describes agent directive?
Which term matches this explanation: The Declarative setting that chooses where a pipeline or stage runs.
Which statement correctly describes post section?
Which term matches this explanation: Declarative actions that run after stages based on completion status.

0 of 10 checks passed

Your progress is saved on this device.