Creating Pipeline-As-Code and Using Pipeline Syntax in Jenkins v2
Lets start by just defining the Jenkins Pipeline.
Jenkins Pipeline
Jenkins Pipeline is a suites of plugins for the continuous delivery. The pipeline constitute of different stages of development, testing and deployment. It integrates dependency on other project as well.
This image will better explain the pipeline and also introduce you to the Jenkins v2.
From the above image, there are different stages which is defined as Build and Push, Test, Release and declarative checkout SCM and post actions.
Jenkins File
The pipeline is written as code into a JenkinsFile, which can be checked in to any repo used by your organization. This helps is tracking the changes done on the pipeline code.
Type of Pipeline
A JenkinsFile can be written into two types of syntax:
- Declarative Pipeline
- Scripted Pipeline
This is from the Jenkins pipeline book:
Declarative and Scripted Pipelines are constructed fundamentally differently. Declarative Pipeline is a more recent feature of Jenkins Pipeline which:
provides richer syntactical features over Scripted Pipeline syntax, and
is designed to make writing and reading Pipeline code easier.
Pipeline Key Concepts
There are certain keywords used in the JenkinsFile which has its own usage.
- Pipeline: The pipeline includes all the building process which constitute of stages of building an application.
- Node: A node is a machine which is part of Jenkins environment and where the pipeline will be executed.
- Stage: This can be viewed as collection of different task in continuous delivery. It will be like, building, testing, deploying the application.
- Step: This is the single task.
How a typical Declarative JenkinsFile looks
This will be how the JenkinsFile is written where the pipeline is written as code.
Agent: This can be defined as 'any' or given a label which identifies the agent/machine from the Jenkins environment, where the pipeline will be executed.
Parameters: These are the building parameters which can be configured by user before running the Jenkins Pipeline. The parameters looks like this:
Sample Step: Script is used to write any pipeline code for a task, where we want to run some specific commands. To run a command, sshCommand utility can be used.
Refer to the below image which illustrates
- the usage of the parameters defined.
- writing code for the pipeline
- running commands on remote machine using sshCommand
As the name suggest, you can do any Post build, test or deployment activity. e.g. you can notify the team about the current Jenkins build status and if there are any errors.
Refer to below image which will email to the respective developers with the default build status and will attach the compressed form of current build logs.
How to get the pipeline-as-code for different activity/task
Lets say that you don't know how the pipeline syntax looks and you are not allowed to use google as well, then how will you pick the correct syntax 🤔
Well a pipeline syntax is given by Jenkins to facilitate this task.
It will provide with all the different task and will also give you different attributes you could use to make the job even more useful.
Lets take an example of email extension:
Now after providing all the details you can click on the generate pipeline-script, which will generate the code for you with the extension attributes and you can directly use it in your JenkinsFile.
So for the above script, this will be the pipeline script generated.
Once the JenkinsFile is written, we can check-in the file to Github repository. I believe this article will help you write your own JenkinsFile and create your own pipeline-as-code.
Reference: https://www.jenkins.io/doc/book/pipeline/
Do let me know your feedback and topics you want me to cover.
Comments
Post a Comment