From the course: Intermediate Jenkins: Automate, Integrate, and Secure CI/CD Workflows at Scale

Pipeline as code with Jenkinsfile

So far, we've seen how Jenkins can use pipelines to describe the stages and steps in a workflow. But entering pipeline configurations directly in the Jenkins user interface is a manual approach and doesn't lend itself to developing workflows at scale. For that, we can increase automation by working with pipelines as code. Jenkins file is a format we can use to capture our pipeline definitions as code. And since the format is saved as a text-based file, we can store it and track changes along with any other files in the project. But the Jenkins file lets us go beyond capturing pipeline definitions. It also lets us define the project configuration itself. We can use a Jenkins file to capture definitions for tools, options for our project, and triggers to run a project, and much more. Essentially, any configuration that we would make in the Jenkins Graphical User Interface we can capture as code in the Jenkins file and store it along with other files in the project. When a Jenkins file is stored in a repository along with the rest of a project's code, you can start practicing GitOps. This means the repository becomes the single source of truth for everything associated with a project. From the application code to configuration for tools that support the application including continuous integration tools like Jenkins. With GitOps, any changes made to the pipeline can be reviewed and discussed with the team before being merged and applied using an automated process. Now that we know how we can use and benefit from a Jenkins file, let's update the first pipeline we worked with in this course to use a pipeline stored as code. I've got two tabs open for this demo. The GitHub repo for the course exercise files. the configuration for the very first pipeline we worked with at the beginning of the course. On the Repo tab, I'll select Code, HTTPS, and then I'll select the stack squares icon to copy the repo URL. Now I'll go back to the job configuration. I'll start by changing the pipeline definition to use Pipeline Script from SCM. Now I'm prompted to enter the repository URL and any credentials I might need to access the repo. Under SCM, I'll select Get. Now I'm prompted to enter the repository URL and any credentials I might need to access the repo. I'll start with my credentials first. At the time I'm recording this, the exercise files repo is private, so I need to add my credentials to access it. By the time you access this lesson, the repo will be public and available for you to access without credentials. Now I can paste in the repository URL. Under Branches to build, I need to change the branch specifier from master to main. Most modern repos use main as the default branch, but the default for the git plugin still uses master for this option. Now I'll configure the script path. This is the location that Jenkins will use to find the Jenkins file in the repository. In most cases, the Jenkins file is placed in the root of the repository. However, you may also find repos where the Jenkins file is located in a subfolder. If that's the case, then you would put the path to the Jenkins file in this location. Ok, I'll save this configuration and build the project. Now I'll select the most recent run and the pipeline overview. The overview shows two stages in this pipeline. In the first stage Jenkins checks out the code and configures the pipeline. The second stage has some details on using Jenkins files along with this course. I'll leave it as a challenge for you to configure a pipeline project to use the Jenkins file from the course repo.

Contents