Being remote developer and work from home during this pandemic, I realize that there is need of system or mechanism which monitor state of the your project. Some tool which does following for you
- Continuously run unit tests on each check-in pushed in any branch
- Run other checks say lint, security tools, static analysis
- Running tests on multiple platform
- Send notification to team in case of failure
- Pull Request build status
Jenkins is typically used as CI/CD. But I would like to consider Jenkins as a product quality checker, running unit tests continuously on various platforms. Here is how to setup a CI/CD using jenkins for Multi-branch pipeline.
Choose a Multiple branch item
Add branch source details
Build configuration using Jenkinfile
What is jenkinfile
‘Pipeline as code’ or defining the deployment pipeline through code rather than configuring a running CI/CD tool, provides tremendous benefits for teams automating infrastructure across their environments. One of the most popular ways to implement a pipeline as code is through Jenkins Pipeline. Jenkins, an open source automation server, is used to automate tasks associated with building, testing, and deploying software.
Following is the typical Jenkins file I used for my GoLang project.
pipeline {
agent {
node {
label 'label to get the node'
}
}
parameters {
booleanParam(name: 'releaseBuild', defaultValue: false, description: 'New Release Build')
booleanParam(name: 'skipTests', defaultValue: false, description: 'Skip Tests')
booleanParam(name: 'uploadBuild', defaultValue: false, description: 'Upload the tar Artifactory')
}
environment {
DISABLE_AUTH = 'true'
DB_ENGINE = 'sqlite'
def t = readFile(file: 'VERSION')
VERSION_ENV = t.trim()
}
stages {
stage('Build dependent projects/Module') {
when {
allOf {
expression { params.releaseBuild }
branch 'master'
}
}
steps {
sh '<build Command>'
}
}
stage('Execute depedent project/modules tests') {
when {
allOf {
expression { params.releaseBuild }
expression { !params.skipTests }
branch 'master'
}
}
steps {
sh '<test command>'
}
}
stage ('Build') {
steps {
sh 'printenv'
sh 'ls -alf .'
sh '<build command>'
}
}
stage ('Test') {
when {
allOf {
expression { !params.skipTests }
}
}
steps {
sh 'make test'
sh 'echo "done"'
}
}
stage('Stage') {
when {
allOf {
expression { params.releaseBuild || params.uploadBuild }
branch 'master'
}
}
steps {
script {
sh 'make build-stage TARGET_FILE_NAME=*.tar'
sh 'ls -lrt out'
sh 'ls -lrt'
}
}
}
stage('Upload To Artifactory') {
when {
allOf {
expression { params.releaseBuild || params.uploadBuild }
branch 'master'
}
}
steps {
script {
def server = Artifactory.server "Artifactory"
def uploadSpec = """{
"files": [
{
"pattern": "*.tar",
"target": "linux/"
}
]
}"""
def buildInfo = server.upload spec: uploadSpec
server.publishBuildInfo buildInfo
}
}
}
stage("Build Installer") {
when {
allOf {
expression { params.releaseBuild }
branch 'master' //only run these steps on the master branch
}
}
steps {
script {
build job: 'installer/master',
propagate: true,
wait: true,
parameters: [
[$class: 'BooleanParameterValue', name: 'releaseBuild', value: "${params.releaseBuild}"],
[$class: 'StringParameterValue', name: 'stageFileName', value: "*.tar"],
[$class: 'StringParameterValue', name: 'platform', value: "linux"],
[$class: 'StringParameterValue', name: 'buildNumber', value: "${BUILD_NUMBER}"]
]
}
}
} // deploy
} // stages
post {
always {
mail to: 'receipient@jenkins.com',
subject: "${currentBuild.currentResult} Pipeline: ${currentBuild.fullDisplayName}",
body: "More Details ${env.BUILD_URL}"
}
}
}