By the end of the previous lesson, the orders-api process was working: orders-api-build builds, tests and archives the jar, and orders-api-smoke copies and checks it. But getting there took two jobs, a plugin, a permission between them, a copy step and a conditional trigger, all configured through forms in different parts of the interface. If Laura wanted to add a quality analysis stage and a deployment stage, she would need more jobs and more links between them. Pipelines exist precisely to solve this problem. In this lesson you will understand what a pipeline is and what concepts it is made of, create your first pipeline, orders-api-pipeline, run it and learn to read its stage view and its console. You will also see one of its great advantages: surviving a Jenkins restart.

The Limits of Chained Freestyle Jobs

Review what you have built in the previous lessons and look at its problems:

Limitation How it shows up at TecnoShop
Configuration in the interface The steps of orders-api-build exist only in Jenkins forms. To find out what it does, you have to open Configure
Not versioned If someone changes the script in step 2, there is no record of who did it, when or why. There is no easy way to go back to the previous version
Hard to review Diego cannot propose a change to the build process through a pull request for Laura to review, the way he does with code
Scattered flow The whole process (build, test, archive, check) is split across two jobs. With five stages it would be five jobs and four links
No shared state The smoke job does not know which parameters the build ran with. Passing the jar along required a plugin and a permission
Does not survive restarts If Jenkins restarts in the middle of a Freestyle build, that build is interrupted and is not resumed

None of these problems is serious with two jobs. All of them become serious with twenty projects and a hundred jobs.

What a Pipeline Is

A pipeline (in Jenkins, Pipeline with a capital P) is a type of job that describes the entire delivery process as a program: a sequence of stages, written in a domain-specific language based on Groovy, that Jenkins runs from start to finish as a single build.

This approach naturally models a project's delivery flow:

flowchart LR
    A["Commit to main"] --> B["Checkout"]
    B --> C["Build"]
    C --> D["Tests"]
    D --> E["Quality<br/>(module 6)"]
    E --> F["Deploy<br/>(module 7)"]

Each box is a stage of the same pipeline. If one fails, the following stages do not run and the whole build ends in FAILURE, exactly like the steps of a Freestyle project, but with the entire process in plain view. In this lesson you will implement the first three stages; the rest will come throughout the course.

Key Concepts: Pipeline, Stage, Step and Agent

Concept What it is Example in orders-api-pipeline
pipeline The block that contains the entire process definition The whole pipeline { ... } block
agent (or node) Where it runs: which node and, therefore, which workspace agent any: any available node (today, the built-in node)
stage A named stage that groups related work. It is the unit you see in the stage view Checkout, Build, Tests
step A specific action within a stage git ..., sh './mvnw -B test'
steps The block that contains a stage's steps steps { ... }

About agent and node: both terms refer to "where it runs". agent is the word used by the declarative syntax, the one you will use in this course; node is its equivalent in the scripted syntax and also the name that appears in the console. You will study the differences between the two syntaxes in Declarative vs Scripted Pipelines.

Creating the orders-api-pipeline Job

  1. On the Dashboard, click New Item.
  2. Name: orders-api-pipeline.
  3. Type: Pipeline. Click OK.

The configuration of a Pipeline job is shorter than that of a Freestyle project. It has General and Triggers (with the same options you already know) and a new section, Pipeline, which replaces Source Code Management, Environment, Build Steps and Post-build Actions: all of that moves inside the script.

  1. In Description: Learning pipeline for orders-api: checkout, build and tests.
  2. In the Pipeline section, leave Definition set to Pipeline script. A text editor appears.
  3. Enter the following script, replacing <your-username> with your GitHub username:
pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                git url: 'https://github.com/<your-username>/orders-api.git', branch: 'main'
            }
        }

        stage('Build') {
            steps {
                sh 'chmod +x mvnw'
                sh './mvnw -B -DskipTests package'
            }
        }

        stage('Tests') {
            steps {
                sh './mvnw -B test'
            }
        }
    }
}
  1. Leave the Use Groovy Sandbox checkbox checked: it runs the script under security restrictions. You will understand its role in Configuring Security.
  2. Click Save.

The editor's try sample Pipeline... drop-down offers ready-to-use examples, and the Pipeline Syntax link below the editor opens a snippet generator that you will get to know in Developing and Debugging Pipelines.

The Pipeline Line by Line

Before running it, make sure you understand each piece. Heads-up: this section explains only what you need to read this script; the full syntax is covered in module 3, starting with Pipeline as Code and Pipeline Syntax.

pipeline {

Opens the definition of a declarative pipeline. The entire process goes between this brace and the last one in the script. Braces { } delimit blocks, as in Java.

    agent any

Indicates that the pipeline can run on any node with free executors. Today only the built-in node exists, so it will run there, in its own workspace: /var/jenkins_home/workspace/orders-api-pipeline. Once linux-agent-01 exists, we will change this line to agent { label 'linux' }.

    stages {

Opens the list of stages. A declarative pipeline has a single stages block with one or more stages inside it.

        stage('Checkout') {
            steps {
                git url: 'https://github.com/<your-username>/orders-api.git', branch: 'main'
            }
        }
  • stage('Checkout'): a stage called Checkout. The name, in single quotes, is the one you will see in the stage view.
  • steps { ... }: the stage's steps.
  • git url: ..., branch: 'main': the git step clones the repository and checks out the main branch. It is the equivalent of the Freestyle project's Source Code Management section. Notice the comma-separated name: value syntax: these are the step's named arguments.
        stage('Build') {
            steps {
                sh 'chmod +x mvnw'
                sh './mvnw -B -DskipTests package'
            }
        }
  • sh '...': the sh step runs a shell command, like the Freestyle project's Execute shell. Just as there, if the command returns a non-zero exit code, the step fails and the pipeline stops.
  • ./mvnw -B -DskipTests package: compiles and packages the jar without running the tests. We separate compilation from testing so that, if something fails, the stage view tells you right away whether it is a compilation problem or a test problem.
        stage('Tests') {
            steps {
                sh './mvnw -B test'
            }
        }
  • ./mvnw -B test: runs the tests. Maven detects that the classes are already compiled in target/ and does not compile them again, so this stage focuses on the tests.

The final lines close stages and pipeline. A common mistake is forgetting a closing brace: Jenkins will catch it when running the script and display a syntax error with the line number.

Running It and Reading the Stage View

Click Build Now. While it runs and once it finishes, the job page shows a graphical representation of the stages. Exactly what you see depends on the installed plugins:

  • If your installation includes the Pipeline: Stage View plugin (it was part of the suggested plugins for years), you will see the Stage View in the central area: a table with one column per stage and one row per build.
  • If you see a graph of connected stages instead, your installation already ships with Pipeline Graph View, the plugin that replaces it. You will install it (if you do not have it) and use it in Using Jenkins Plugins. The information it provides is the same.

In the Stage View:

                 Checkout    Build       Tests
Average stage    2s          18s         6s
times:
#1  Nov 28 11:40 2s          18s         6s
Element What it indicates
Column headers The stage names, in the order they appear in the script
Average stage times The average duration of each stage across recent builds
Green cell The stage completed successfully
Red cell The stage failed; the following stages did not run
Cell with animated stripes The stage is running
Time in each cell How long that stage took in that build

Hover over a cell and click Logs: a window opens with the output of that stage only, step by step. This is one of the big advantages over Freestyle: instead of searching through a console with thousands of lines, you go straight to the stage that failed.

With several builds, the Stage View also lets you see at a glance which stage has become slower. If Tests goes from 6 to 60 seconds from one build to the next, you will notice immediately.

Reading a Pipeline's Console

Open the Console Output of build #1. You will see the output of the commands, as in a Freestyle project, interspersed with lines that start with [Pipeline]:

Started by user Laura Martin
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/orders-api-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] git
The recommended git tool is: NONE
No credentials specified
Cloning the remote Git repository
...
Checking out Revision 4b1d2e7... (refs/remotes/origin/main)
...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
+ chmod +x mvnw
[Pipeline] sh
+ ./mvnw -B -DskipTests package
[INFO] Scanning for projects...
...
[INFO] Tests are skipped.
...
[INFO] BUILD SUCCESS
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Tests)
[Pipeline] sh
+ ./mvnw -B test
...
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
...
[INFO] BUILD SUCCESS
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

How to read the markers:

Marker Meaning
[Pipeline] Start of Pipeline / End of Pipeline Start and end of the run
[Pipeline] node + Running on Jenkins in ... A node has been assigned (here, the built-in node, which the console calls Jenkins) along with a workspace
[Pipeline] stage + [Pipeline] { (Build) The Build stage starts
[Pipeline] git, [Pipeline] sh A step of that type starts; its output follows
[Pipeline] } + [Pipeline] // stage The stage ends
[Pipeline] // node The node and its executor are released

Notice that there are two Maven BUILD SUCCESS lines, one for each ./mvnw, and a single Jenkins Finished: SUCCESS: the rule you learned in Your First Jenkins Job still holds. The line that counts is Finished:.

Durability: Pipelines That Survive a Restart

A fundamental difference between Freestyle and pipeline jobs is what happens if the controller restarts while builds are running (because of an upgrade, a crash or maintenance):

  • A running Freestyle build is interrupted and not resumed: you have to start it again.
  • A pipeline is durable: as it progresses, Jenkins saves to disk where in the script it is. After the restart, it resumes execution from where it left off.

In the console, a resumed pipeline shows messages like these:

Resuming build at Fri Nov 28 11:52:10 CET 2026 after Jenkins restart
Waiting to resume part of orders-api-pipeline #3: ...
Ready to run at Fri Nov 28 11:52:14 CET 2026

There is an important caveat: what survives is the pipeline's state on the controller. If an sh step is running a process on a separate agent, that process stays alive on the agent and Jenkins reconnects to it after the restart. But in our environment, builds run on the built-in node, inside the same container as the controller: when the container restarts, the Maven process dies with it, and the step will fail when it resumes. This is another reason to run builds on agents, as you will do in module 5. Steps managed by the controller itself, such as a wait (sleep), do resume without any problem; you will verify this in exercise 3.

Freestyle vs. Pipeline

flowchart TB
    subgraph FS["Chained Freestyle jobs"]
        direction LR
        F1["orders-api-build<br/>(forms)"] -->|"Build other projects"| F2["orders-api-smoke<br/>(forms)"]
        F1 -.->|"Archive + Copy Artifact<br/>+ permission"| F2
    end
    subgraph PL["A single pipeline"]
        direction LR
        P1["Checkout"] --> P2["Build"] --> P3["Tests"] --> P4["...more stages"]
    end
    FS -->|"evolution"| PL
Aspect Freestyle Pipeline
How it is defined Interface forms A script (and, from module 3 on, a Jenkinsfile in the repository)
Versioning and review No Yes, when the script is in the repository
Multi-phase process Several chained jobs A single job with several stages
Sharing data between phases Plugins (Copy Artifact, trigger parameters) Shared variables and workspace within the same build
Visualization A single console Stage view and per-stage logs
Controller restart The build is interrupted The pipeline resumes
Logic (conditions, parallelism, retries) Limited, through scripts and plugins Built into the language (modules 3 and 4)
Learning curve Very gentle Somewhat steeper
Recommended use Simple or one-off tasks Building, testing and deploying applications

Freestyle jobs do not disappear from the course all at once: orders-api-build and orders-api-smoke will remain as a reference. But from here on, the orders-api process will evolve as a pipeline.

The Next Step: Moving the Pipeline Out of the Interface

Look again at the table of limitations at the beginning. The pipeline already solves the scattered flow, the shared state and restarts. But the script is still written in a text box in the interface: it is not yet versioned and cannot be reviewed. If someone modifies it, there is no history; if you delete the job, you lose the script.

The solution is to store the script in a file called Jenkinsfile inside the orders-api repository itself, next to the code, and to configure the job to read it from Git (the Pipeline script from SCM option in the Definition drop-down, which you will already have noticed). This is known as Pipeline as Code, and it is exactly where module 3 begins.

Common Mistakes and Tips

  • Not replacing <your-username> in the URL: the git step fails with a repository-not-found error. Copy the URL from the Code button on GitHub.
  • Forgetting the git step: in a pipeline with Pipeline script there is no automatic checkout. Without it, the workspace is empty and ./mvnw fails with not found.
  • Unclosed quotes or unbalanced braces: the build fails instantly with a message like WorkflowScript: 12: expecting '}'. The number tells you the line in the script. An editor with brace matching helps a lot.
  • Using double quotes unnecessarily: in Groovy, "..." interprets expressions such as ${...} and '...' does not. For fixed commands, use single quotes, as in the example. You will study this in depth in module 3.
  • Looking for Source Code Management or Build Steps in the configuration: they do not exist in a Pipeline job; everything is in the script.
  • Unchecking Use Groovy Sandbox "because it causes errors": the script would then require approval from an administrator, and you lose a safeguard. Leave it checked.
  • Tip: give stages short, clear names. They are the first thing anyone opening the job will see.

Exercises

Exercise 1. Break an orders-api test (change the expected 3 to a 4, as in lesson 01-05), push it and start orders-api-pipeline. What color is each stage in the stage view? What does the Logs button of the red stage show? How does this differ from diagnosing the same failure in orders-api-build? Restore the test when you are done.

Exercise 2. Add a fourth stage, Smoke, to the pipeline that performs the same check as orders-api-smoke: that target/orders-api-1.0.0-SNAPSHOT.jar exists and is not empty. Run it and compare the solution with what you needed for the orders-api-smoke job.

Exercise 3. Verify durability. Temporarily add the step sleep time: 60, unit: 'SECONDS' at the beginning of the Tests stage. Start the pipeline and, while it is waiting, restart Jenkins with docker compose restart jenkins-controller (from the directory containing your docker-compose.yml). When Jenkins comes back, open the build's console. What happened? Remove the sleep when you are done.

Solutions

Solution 1. Checkout and Build appear in green, because the code compiles; Tests appears in red. The Logs button for Tests shows only the output of ./mvnw -B test, where you can see expected: <4> but was: <3> and Tests run: 3, Failures: 1. The difference from orders-api-build is that there you had to read the entire console from the bottom up to work out which phase had failed; here the stage view tells you at a glance (it is not a checkout or compilation problem, it is a test problem) and the per-stage log narrows down the search. If you added more stages after Tests, they will not have run.

Solution 2. After the Tests stage, inside stages:

        stage('Smoke') {
            steps {
                sh '''
                    JAR=target/orders-api-1.0.0-SNAPSHOT.jar
                    test -s "$JAR"
                    ls -lh "$JAR"
                    echo "Smoke OK: the jar exists and is not empty"
                '''
            }
        }
  • sh '''...''': the triple single quotes let you write a multi-line script in a single step. Because they are single quotes, Groovy does not interpret $JAR, which reaches the shell as is.
  • test -s "$JAR": this is the command form of [ -s "$JAR" ]; if the file does not exist or is empty, it returns a non-zero exit code and, since the step runs with -e, the stage fails.

Compare it with orders-api-smoke: you did not need a second job, Copy Artifact, the copy permission or Build other projects. The jar is in the same workspace because everything happens in the same build, and the check always corresponds to the code that was just compiled.

Solution 3. The console shows the start of the wait, the interruption and the resumption:

[Pipeline] { (Tests)
[Pipeline] sleep
Sleeping for 1 min 0 sec
Resuming build at Fri Nov 28 12:05:31 CET 2026 after Jenkins restart
Waiting to resume part of orders-api-pipeline #4: ...
Ready to run at Fri Nov 28 12:05:36 CET 2026
[Pipeline] sh
+ ./mvnw -B test
...
Finished: SUCCESS

The build was not lost: Jenkins saved to disk that it was at the sleep step of the Tests stage and, when it came back, it waited for the remaining time and carried on. If you had restarted during ./mvnw instead of during the sleep, the Maven process would have died with the container and the stage would have failed when it resumed, for the reasons explained in the durability section. The dates and times in your console will be different.

Conclusion

You have seen why chained Freestyle jobs do not scale: unversioned configuration that is hard to review, a flow scattered across jobs, no shared state and no resilience to restarts. A pipeline describes the entire process as a program that runs as a single build, organized into stages with steps and executed on an agent. You have created orders-api-pipeline with the Checkout, Build and Tests stages, read it in the stage view and in the console with its [Pipeline] markers, and verified that a pipeline can resume after a restart. Before moving the script into the repository in module 3, you should learn to manage the building blocks that pipelines, and almost everything else in Jenkins, depend on: plugins. You will do that in the next lesson, Using Jenkins Plugins, where you will also install Pipeline Graph View to see orders-api-pipeline in a more modern way.

© Copyright 2026. All rights reserved