Build CI/CD Multibranch Pipeline with Jenkins and Kubernetes
In this article, we will build multiple branch pipeline with Jenkins and Kubernetes step by step
What’s Jenkins
Jenkins is an open source continuous integration and delivery tool that enable developers automatically build, test and deploy application
To know more about Jenkins https://www.jenkins.io/
What’s Multibranch Pipeline
The Multibranch Pipeline enable developer to implement different Jenkinsfiles for different branches of the same project. It’s can discover branches and execute pipeline automatically with Jenkinsfiles in version control for better management pipeline
To know more about Multibranch Pipeline on Jenkins https://www.jenkins.io/doc/book/pipeline/multibranch/
Prerequisites
Jenkins is up and running
Jenkins Plugin Installation
Manage Jenkins → Manage Plugins → Search “Blue Ocean” and “Kubernetes CLI Plugin→ Download and Install
Blue Ocean provide visualization and editor of pipeline with nice UI to support addressing the issue in the pipeline
To know more about blueocean https://plugins.jenkins.io/blueocean/
Kubernetes CLI provide interaction with your pipeline and kubernetes cluster to perform deployments
To Know more about kubernetes cli https://plugins.jenkins.io/kubernetes-cli/
Create Multibranch Pipeline — CI
In Jenkins
Create New Item and Choose Multibranch Pipeline
In Branch Source, update your Git repository and credential, then you can keep the default behaviour for auto discover branch, or you can filter branches by name
In Build Configuration, use Jenkinsfile and update your file path
In Scan Multibranch Pipeline Triggers, choose Scan by webhook and setup Trigger token
In Github
Setting → Hooks → Payload URL
JENKINS_URL/multibranch-webhook-trigger/invoke?token=TOKEN_YOU_SET_IN_JENKINS
Create Multibranch Pipeline — CD
In Jenkins
Add Credentials → Kind Kubernetes Configuration (kubeconfig)
then paste your kubeconfig for accessing kubernetes cluster
Create Multibranch Pipeline — Jenkinsfile
You may refer the follow Jenkinsfile example to customize your pipeline and remind to put the same Jenkinsfile path you setup before in Jenkins
pipeline {
environment {
//put your own environment variables REGISTRY_URI = }
stages {
stage('Initial Notification') {
steps { //put webhook for your notification channel
echo 'Pipeline Start Notification'
}
}stage('Code Analysis') {
steps { //put your code scanner
echo 'Code Scanning and Analysis'
}
}
stage('Robot Testing') {
steps { //put your Testing
echo 'Robot Testing Start' }
post{
success{
echo "Robot Testing Successfully"
}
failure{
echo "Robot Testing Failed"
}
}
}stage("Build"){
steps {steps {withCredentials([usernamePassword(credentialsId: 'YOUR_ID_DEFINED', passwordVariable: 'YOUR_PW_DEFINED', usernameVariable: 'YOUR_ACCOUNT_DEFINED')]) {
sh """
docker login ${REGISTRY_URI} -u ${YOUR_ACCOUNT_DEFINED} -p ${YOUR_PW_DEFINED}
"""
}echo "Docker Build"sh """
docker build -t ${IMAGE_NAME}:${VERSION_PREFIX}${BUILD_NUMBER} ${WORKSPACE} -f Dockerfile
"""echo "Docker Tag"sh """
docker tag ${IMAGE_NAME}:${BUILD_NUMBER} ${REGISTRY_URI}/${REGISTRY_NAME}/${IMAGE_NAME}:${GIT_BRANCH}-${GIT_COMMIT}
docker tag ${IMAGE_NAME}:${BUILD_NUMBER} ${REGISTRY_URI}/${REGISTRY_NAME}/${IMAGE_NAME}:${GIT_BRANCH}-${BUILD_NUMBER}
docker tag ${IMAGE_NAME}:${BUILD_NUMBER} ${REGISTRY_URI}/${REGISTRY_NAME}/${IMAGE_NAME}:${GIT_BRANCH}-${LATEST}
"""
echo "Docker Push"sh """
docker push ${REGISTRY_URI}/${REGISTRY_NAME}/${IMAGE_NAME}:${GIT_BRANCH}-${GIT_COMMIT}
docker push ${REGISTRY_URI}/${REGISTRY_NAME}/${IMAGE_NAME}:${GIT_BRANCH}-${BUILD_NUMBER}
docker push ${REGISTRY_URI}/${REGISTRY_NAME}/${IMAGE_NAME}:${GIT_BRANCH}-${LATEST}
"""
}
post{
success{
echo "Build and Push Successfully"
}
failure{
echo "Build and Push Failed"
}
}
}stage('Image Scan') {
steps { //Put your image scanning tool
echo 'Image Scanning Start'
}
post{
success{
echo "Image Scanning Successfully"
}
failure{
echo "Image Scanning Failed"
}
}
}stage("Deploy to Production"){
when {
branch 'master'
}
steps {
kubernetesDeploy kubeconfigId: 'kubeconfig-credentials-id', configs: 'YOUR_YAML_PATH/your_k8s_yaml', enableConfigSubstitution: true // REPLACE kubeconfigId
}
post{
success{
echo "Successfully deployed to Production"
}
failure{
echo "Failed deploying to Production"
}
}
}stage("Deploy to Staging"){
when {
branch 'staging'
}
steps {
kubernetesDeploy kubeconfigId: 'kubeconfig-credentials-id', configs: 'YOUR_YAML_PATH/your_k8s_yaml', enableConfigSubstitution: true // REPLACE kubeconfigId
}
post{
success{
echo "Successfully deployed to Staging"
}
failure{
echo "Failed deploying to Staging"
}
}
}}
post{
always{step([
//put your Testing
])
}
success{ //notification webhook
echo 'Pipeline Execution Successfully Notification'}
failure{ //notification webhook
echo 'Pipeline Execution Failed Notification'}
}
}
After commit your code with Jenkinsfile, you would be able to see the multibranch pipeline in Jenkins
Click Open Blue Ocean, it would auto-discover your Github branch and pipeline you defined in Jenkinsfile
After merging stg to master branch, you would be able to see the master branch pipeline initiation, then final deployment to production