Declarative Pipelines in Jenkins

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

In this hands-on lab we will be looking at Declarative Pipelines. Declarative style pipelines limit what is available to the user with a more strict and pre-defined structure, making it an ideal choice for simpler continuous delivery pipelines.

We’ll be building the Pipeline (Declarative) and ensuring that it has gone through the build stages as defined and provided a satisfactory Jenkins job result.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Jenkins UI

Once you access the Jenkins landing page, click New Item on the left side of the screen to proceed towards selecting the type of Jenkins job you want to create.

Once the new page loads, click on Pipeline and in the input box give a name to your job, such as MyDeclarativePipeline.

After selecting the type of job, Pipeline, and giving it a name, click on OK to proceed with configuring the job.

Configuring a Jenkins Pipeline Job (Declarative Pipeline)

On the new page which loads for configuring the pipeline job, scroll down to the Pipeline section and ensure that Pipeline Script is selected in the Definition drop-down. An input area should be visible. Copy the following body of text (which is basically in Groovy DSL format, a format understood by Jenkins) into the input area:

pipeline {
    agent {
        docker { image 'linuxacademycontent/jenkins_pipelines' }
    stages {
        stage('fetch') {
            steps {
                sh 'git clone'
                sh 'gcc --std=c99 -o mario content-pipelines-cje-labs/lab1_lab2/mario.c'
                archiveArtifacts artifacts: 'mario'

Leave everything else at the default settings and click on Save.

Note: This Declarative Pipeline will pull down and run a Docker container which has git and gcc utilities installed for cloning and compiling the code.

Running/Building Your Jenkins Declarative Pipeline Job

After hitting Save in the previous step, you will be taken to the control page for the job you just configured. There you can see the job status and other job statistics, and you will be able to edit the configuration again.

To kick off a build, click on Build Now. This should start the build, and you should see the output of build status in a couple of places on your current screen.

Under the Build History box, on the left of your screen you’ll see a build number, and a progress bar if the build is currently running. The page will also be updated with a Stage View, which gives details of execution success (green) and the time taken by each build stage that was defined in our Declarative Pipeline. Subsequent builds will add more data to average run times in the same area.

If you click on the build number under Build History (#1 for example), you will be taken to the details of the specific build. This includes things like Console Output for showing the execution of commands defined in the pipeline for each step.

On this page you’ll also find the build output files, or Build Artifacts, which are the outcome of the pipeline stages that you built. You can click on the actual file name shown under build artifacts to download it to your system.

Additional Resources

To lessen the reliance on your team having to know Groovy syntax for Scripted Jenkins Pipelines, and to simplify the Pipeline syntax for Pipelines, your company has opted to move to Declarative Pipelines in Jenkins.

In addition, your boss also wants see to if dependencies installed directly onto Jenkins servers (such as gcc compiler for compiling source code, and git for cloning/pulling down source code) can be cut down.

To reduce having to deal with these dependencies, you are thinking of running your whole Jenkins Pipeline job inside a Docker container which has gcc and git utilities pre-installed, and then saving any artifacts produced.

You have a rough idea of how you would tackle this:

  1. Ensure that in your Declarative Pipeline you have a Docker agent which pulls down a Docker image and executes all steps within it. The Docker image with gcc and git pre-installed is named linuxacademycontent/jenkins_pipelines.
  2. Have three stages, at least:
    1. A Preparation stage where you clone code from a Git repo:
    2. A Build stage where actual code is assembled, linked, and built, providing an executable binary.
      • Sample build command: gcc --std=c99 -o mario content-pipelines-cje-labs/lab1_lab2/mario.c
    3. An Archiving stage where you save or preserve the output of your Build stage. Hint: Use an archiveArtifacts step and the output (-o mario). mario is the file name.

Note: All steps should take place inside a Docker agent which has gcc and git pre-installed.

With the above in mind, you now have to come up with a Jenkins Declarative Pipeline to automate the task at hand.

Logging In

A Jenkins server will be made available. Once the lab interface provides details of the public ip address, please use the default Jenkins port in your browser to access it:


You will be directly logged into the Jenkins instance, no password authentication is required.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?