07 / 14

Discuss the structure of the Pipeline script in detail.

A Jenkins Pipeline script is structured around a top-level 'pipeline' block containing core sections like 'agent', 'stages', and 'post', with stages further broken down into 'steps' that execute the actual build commands, supporting both Declarative and Scripted syntaxes

A Jenkins Pipeline script, typically stored in a Jenkinsfile, is a powerful way to define your entire CI/CD process as code, enabling version control, collaboration, and reusability . The script is fundamentally structured around a series of nested components that orchestrate the build, test, and deployment phases. Jenkins supports two distinct syntaxes for writing these scripts: Declarative Pipeline and Scripted Pipeline .

Core Structural Components of a Declarative Pipeline
  1. 1

    pipeline: The top-level, mandatory block that contains the entire definition of the Pipeline's logic . All valid Declarative Pipelines must be enclosed within this block.

  2. 2

    agent: This section, which can be placed at the top-level or within individual stages, directs Jenkins on where to execute the Pipeline or a specific stage . It can specify any available agent (agent any), a specific labeled node (agent { label 'my-node' }), or even a Docker container (agent { docker 'maven:3.8.1' }) .

  3. 3

    stages: This container block holds a sequence of one or more stage directives. It represents the entire collection of tasks that the Pipeline will run .

  4. 4

    stage: A logical grouping of steps that performs a specific part of the overall workflow, such as 'Build', 'Test', or 'Deploy' . The stage block helps in visualizing the Pipeline's progress .

  5. 5

    steps: Located inside a stage block, this section defines the concrete actions to be executed . A step could be a shell command (sh), checking out code from a Git repository (git), or archiving artifacts . A stage must contain at least one step .

  6. 6

    post: An optional but powerful section that defines actions to be run at the end of the Pipeline or a stage, based on its completion status (e.g., success, failure, unstable, always) . This is commonly used for sending notifications or cleaning up workspace.

  7. 7

    environment: Defines key-value pairs that are set as environment variables for the steps in the Pipeline or a specific stage .

  8. 8

    when: A directive placed inside a stage that allows the stage to be executed conditionally, based on criteria like the current branch or the value of an environment variable .

  9. 9

    triggers: Defines automated ways to re-trigger the Pipeline, such as by a cron schedule (cron) or by polling the SCM (pollSCM) .

  10. 10

    tools: Automates the installation and puts on the PATH of tools like Maven or JDK, which must be pre-configured in Jenkins .

  11. 11

    parameters: Defines a list of inputs that a user must provide when triggering the Pipeline manually .

Example: Complete Declarative Pipeline Structure

Scripted Pipeline is the more traditional and flexible syntax, based directly on Groovy programming . It does not enforce a rigid structure like Declarative Pipeline. Instead, it uses a node block to allocate an executor and workspace, and the rest of the script is written in Groovy, allowing for complex logic, loops, and variable definitions . While more powerful for advanced use cases, it requires a deeper understanding of Groovy .

Example: Scripted Pipeline Structure