Jenkins' Pipeline syntax

Internal reference: topics/03-2.md

Pipeline syntax

In this course we use the declarative syntax that is recommended for all new projects. The other options are a Groovy-based DSL and XML (created through the web interface). The declarative syntax was designed to make it as simple as possible to understand the pipeline, even by people who do not write code on a daily basis. This is why the syntax is limited only to the most important keywords.

Let's try an example and read the comments carefully:

pipeline {
     //Uses any available agent
     agent any
     //Triggers every 15 minutes     * see more https://linuxhandbook.com/crontab/
     triggers { cron('H/15   * * * *') }
     //Stops if the execution takes more than 5 minutes
     options { timeout(time: 5) }
     //Asks for the Boolean input parameter before starting
     parameters { 
          booleanParam(name: 'DEBUG_BUILD', defaultValue: true, 
          description: 'Is it the debug build?') 
     }
     stages {
          stage('Example') {
               //Sets Node-Backend as the NAME environment variable
               environment { NAME = 'Node-Backend' }
               when { expression { return params.DEBUG_BUILD } } 
               steps {
                    echo Building $NAME
                    script {
                         def browsers = ['chrome', 'firefox']
                         for (int i = 0; i < browsers.size(); ++i) {
                              echo Testing the ${browsers[i]} browser.
                         }
                    }
               }
          }
     }
     post { always { echo 'I will always say Hello again!' } }
}


What are Sections?

Sections define the pipeline structure and usually contain one or more directives or steps. They are defined with the following keywords:


What are Directives?

Directives express the configuration of a pipeline or its parts:


What are Steps?

Steps are the most fundamental part of the pipeline. They define the operations that are executed, so they actually tell Jenkins what to do:


The complete specification of the available steps can be found at → here.


Based on the book: „Continuous Delivery with Docker and Jenkins, 3rd Edition - Third Edition By Leszko“


Daniel Garavaldi