Jenkins Pipelines as Code
At a high level, the software development has a workflow:
- Build you app
- Deploy your app in a given environment
- Test your app
- Package your app, ready to ship
An option to approach this on Jenkins to have a single job for each step and link them as downstream/upstream jobs
- Build application
- Deploy your app
- Run tests
- Publish artifact to package repository
- Run tests
- Deploy your app
From Jenkins 2 we can have one single job performing all those tasks, and there is no need to use the Jenkins Configure Job GUI. We can script jobs as pipelines. A pipeline is the workflow that your app follow from build to delivery.
You just need to set up your job as a Pipeline and write the code.
node {
stage ('build application') {
// Do your building magic
}
stage ('start up terraform virtual environment') {
// Spin up your lovely environment infra
}
stage ('deploy app on virtual environment') {
// Deploy your lovely app
}
stage ('run performance tests') {
// How is your app doing regarding performance?
}
stage ('run functional tests') {
// Have you thought about end users?
}
stage ('publish application on repository') {
// Your app is ready to go!
}
}
But what if you have different apps that share the same stages? Do you have to copy and paste code? No, that is error prone.
You can have your pipelines code shared in a repo. You will need to set up Jenkins to get the pipelines code from that repo. And the code turns into:
@Library('pipelines@master') _
runPipeline()
Where pipelines is the name of the shared libraries defined in Jenkins configuration and master is the branch of your pipelines code baseline and runPipeline is the function that performs the code above.