The Jenkinsfile

A pipeline in Jenkins is defined using a script called the Jenkinsfile. This provides more automation, allows your pipeline to be treated like all other application code, and can be stored in version control. This provides more benefits for your team since your pipeline can be reviewed like code and the pipeline acts as a single source of truth. In this section, we will cover the two ways of creating the pipeline, from the web UI and by adding it to source control. We will also learn how to configure Jenkins to read from the remote repository.

The pipeline syntax can be presented in two forms: declarative and scripted. The declarative syntax is a simple and opinionated way of writing your pipeline. The scripted pipeline is built with Groovy and is generally a more flexible and expressive way of creating your pipelines. When choosing which model to use, it all depends on your requirements. The declarative model works with simple pipelines and lacks most of the flexibility offered by the scripted model. We will be working with the scripted pipeline in this chapter.

In the previous section, we learned about the branching workflow, which enables us to make the most out of our CI setup and makes it easier for our teams to collaborate on a project. In this section, we will create the pipeline script for our project with the necessary steps required for it to run.

While working with the Jenkins scripted pipeline, we use standard Groovy syntax. The scripted pipeline has some special directives that perform different functions. Let's explain each directive we see in our pipeline script:

Directive Explanation node
node This defines where the job is going to be run. We will explore more about this in the next chapter as we cover setting up master-slave relationships on Jenkins.
dir This directive defines what directory/folder to run the following directives on.
stage  This defines the stage of your pipeline, for example, what task it's running.
git This points to the remote repository where you pull the changes from.
sh This defines the shell script to run on a UNIX-based environment. On a Windows environment, we would use the bat directive instead.
def As mentioned previously, the pipeline is written in Groovy; thus, we can define functions to perform different actions. In this case, we defined a printMessage function, which prints out different messages at the start and end of our pipeline.
The previous list is not exhaustive. There are a lot of things that can be achieved in the Jenkinsfile using different directives. For an exhaustive list, refer to the official site: https://jenkins.io/doc/.

Also, note the different  kinds of pipelines, that is, declarative versus scripted. Both of these have different syntax and they are not compatible. So, while viewing the documentation, be careful not to use the declarative syntax while working with a scripted pipeline and vice versa.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.23.130