Jenkins Pipeline
Jenkins pipeline is a suite of plugins that support implementing and integrating continuous build and delivery pipeline in Jenkins. Jenkins pipeline provides an extensible set of tools for modeling your CI/CD pipeline as code using DSL (Domain scripting language). If you are new to Jenkins Pipeline, give this a good read (https://jenkins.io/doc/book/pipeline/).
A basic example of Jenkins pipeline looks like this:
- Init — Build initialization, e.g: checking out code
- Linux — Build OS environment, e.g: Linux OS
- Setup — Set up build environment, e.g: create Python venv
- Build — Actual code build, e.g: test, build and package
- Deploy — Deploy to your artifact management system, e.g: Artifactory
Why Jenkins Pipeline?
Because a fully automated CI/CD pipeline boost your teams development efficiency. With auto Git branch detection, pulling, syntax checking, unit testing run, functioning testing, building, deploying and notification, developers only need to focus on coding. Since everything is “As Code”, you can easily recreate your CI/CD pipeline in a newly Jenkins environment. It also increases the chance of code sharing, build environment consistency, and many other good reasons, why not Jenkins Pipeline?
If you and team are still manually creating Jenkins jobs, manually triggering build after each commit, worrying about what if Jenkins server someday goes down, missing your delivery deadline, etc, well, you should really move to use Jenkins Pipeline. CI/CD pipeline as code is the best way to control the fate in your own hands.
How To Create an Automate Jenkine Pipeline?
- You need “Multibranch Pipeline”:
The Multibranch Pipeline project type enables you to implement different Jenkinsfiles for different branches of the same project. In a Multibranch Pipeline project, Jenkins automatically discovers, manages and executes Pipelines for branches which contain a Jenkinsfile in source control.
A template of “Multibranch Pipeline” is available here.
- You need to create a Jenkinsfile and add it to your project Git repo:
Like any other text file, A Jenkinsfile is just a text file that contains the definition of your CI/CD pipeline and checked into Git.
Below is a template of Jenkinsfile (https://github.com/tonylixu/jenkins/blob/master/pipeline/Jenkinsfile):
#!/usr/bin/env groovy
pipeline {
// You can specify agent label in later stage
agent none
options {
parallelsAlwaysFailFast()
}
stages {
stage('init') {
steps {
println "Build started"
}
}
stage('Linux env Build') {
parallel {
stage('linux') {
agent {
label "master"
}
stages {
// Active the Python virtual env
stage('setup') {
steps {
sh label: 'Active python venv', script: '''
source /var/lib/jenkins/python-venvs/testenv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
'''
}
}
stage('build') {
steps {
sh label: 'build', script: '''
source /var/lib/jenkins/python-venvs/testenv/bin/activate
cd test-project
tox
python setup.py sdist
python setup.py bdist_wheel
ls ./
'''
}
}
stage('deploy') {
when {
branch 'master'
}
steps {
script {
env.WORKSPACE = pwd()
def version = readFile "${env.WORKSPACE}/build.info"
version = version.replaceAll("\n", "");
println "Version is $version"
sh label: 'Publish file', script: "deploy to you package management system"
}
}
}
}
post {
// Slack notifications
success {
sh label: 'success', script: 'echo "Success"'
slackSend channel: 'notifications', color: 'good', message: "<${env.BUILD_URL}|${env.JOB_NAME} #${env.BUILD_NUMBER}> - Build completed successfully"
}
failure {
sh label: 'failure', script: 'echo "Failure"'
slackSend channel: 'notifications', color: 'danger', message: "<${env.BUILD_URL}|${env.JOB_NAME} #${env.BUILD_NUMBER}> - Build failed"
}
aborted {
sh label: 'aborted', script: 'echo "Aborted"'
slackSend channel: 'notifications', color: 'danger', message: "<${env.BUILD_URL}|${env.JOB_NAME} #${env.BUILD_NUMBER}> - Build aborted"
}
always {
sh label: 'Linux build done', script: 'echo "Done Linux build"'
cleanWs()
}
}
}
}
}
}
}
Once you have the Jenkinsfile created and configured in your project repo, create a multibranch pipeline project in Jenkins (from this template https://github.com/tonylixu/jenkins/blob/master/pipeline/multibranch-config.xml), then you are good to go!