Scripting your own Pipeline (as Code)
How many times have you heard "This will take a lot of time to implement in the CI/CD pipeline? Do you even know how difficult it is to change a operational CI/CD pipeline? We cant afford a change in the CI/CD pipeline right now."
I heard it a lot and always felt curious to see what is behind the CI/CD pipeline and reason out the tedious scripts written behind them until last year when I first started evaluating the Pipeline as Code option available in Jenkins. Now I do not bother to look into any scripts, I just write them on my own.
I use a scripted pipeline, all my projects have a Jenkinsfile in the root. You can refer the official documentation here for details. I will give you some insights to writing up a good Jenkinsfile.
When you think about CI/CD the basic requirement will be to,
- Checkout the source code from the repository.
- Build your deployment artifacts.
- Store the artifact in an artifactory.
- Generate deployment files.
- Finally deploy to the runtime environment.
- Archive the workspace artifacts.
There would be other stuff like quality, security and storing deployment logs but we will first focus on the basics here.
If you have read my first article you would have noticed a Dockerfile in the repo. I use docker to build my images. On the same context, I use a file called as Jenkinsfile in my repo which describes the stages for the pipeline.
To start with just to get familiar with the syntax. You will notice a node at the root and stages within the node. To keep it simple I will use a single node for the entire process. I would also suggest you to get Groovy 'ing a bit
node {
stage('greetings') {
sh 'echo "Godspeed"'
}
}
The basic flow would be to,
checkoutFromGit > build > lint > audit > test > pushToRegistry > deploy
A sample Jenkinsfile here demonstrates the stages mentioned above.
The advantages of having a scripted pipeline is that you can move to any jenkins setup anytime you want. It gives you the flexibility to move away from tedious configurations and scripting required to manage the pipeline.
A couple of plugins to look out for are blue-ocean, credentials, sonarQube and slack.
Disclaimer: the views and opinions expressed above are solely derived from my personal experiences and does not represent my employers or anyone else’s.