In this second part of the series, we will be setting up a Jenkins Shared library to execute our Jenkins jobs. As the complexity and number of jobs you maintain grow, the use of Shared Libraries provides the ability to share and reuse code across jobs for CI and CD processes.
In order to set up a practical application with our seedJob, we will modify seed.groovy to build a Pipeline job (deployment job) and a Multibranch Pipeline job (test job). By the end of this series, you will have a foundation set up to onboard micro services consistently as well as execute certain stages specific to the Jenkins jobs you are running.
The source code is available below
microservice-pipelines)poc-micro JHipster microservicekcrane121/maven-jenkins:blog.seedJob set up from Part 1.microservice-pipelinesseed.groovy to create a Pipeline Job (deployment job) and Multibranch Pipeline Job (test job) per service.jenkinsJob.groovy to be used by our micro services as the entry point into our Shared Library.Jenkinsfile in poc-micro to use microservice-pipelines Shared Library.Since we will be using a Shared library, Jenkins needs to be set up to use our Shared Library.
Dashboard > select Manage Jenkins > select Configure System > scroll down to Global Pipeline Libraries > select Addmicroservice-pipelines in the Name fieldmaster in Default Version
Source Code Management, select GitProject Repository field, enter https://github.com/kcrane3576/microservice-pipelines > select Save.We are going to modify seed.groovy to build a Pipeline job and Multibranch Pipeline job for all services we onboard.
seedJob to use a part2 branch we will create in microservice-pipelinesDashboard > select seedJob > select Configure.Source Code Management, change the Branch Specifier to */part2.microservice-pipelines to build our deployment and test jobsWe are going to leave the master branch of microservice-pipelines alone to ensure it works with Part 1 of this series. In order for us to do this, we will introduce the changes to the seed.groovy job on branch part2 of microservice-pipelines.
pipelineJob and multibranchPipelineJob to seed.groovypart2 in microservice-pipelines.part2 branch, remove the original code in seed.groovy and paste in the below code.
pipelineJob and multibranchPipelineJob, make sure to go back and check the Jenkins Job DSL API.def createDeploymentJob(jobName, repoUrl) {
pipelineJob(jobName) {
definition {
cpsScm {
scm {
git {
remote {
url(repoUrl)
}
branches('master')
extensions {
cleanBeforeCheckout()
}
}
}
scriptPath("Jenkinsfile")
}
}
}
}
def createTestJob(jobName, repoUrl) {
multibranchPipelineJob(jobName) {
branchSources {
git {
remote(repoUrl)
includes('*')
}
}
triggers {
cron("H/5 * * * *")
}
}
}
pipelineJob and multibranchPipelineJobFinally we will need to trigger the building of the specific poc-micro_deploy and poc-micro_test jobs we are onboarding.
repo variable with https://github.com/kcrane3576/.deployName variable by using the repo variable and the jobName (poc-micro) when creating the pipelineJob.testName variable by using the repo variable and the jobName (poc-micro) when creating the multibranchPipelineJob.seed.groovy in the part2 branch of microservice-pipelines on github.def buildPipelineJobs() {
def repo = "https://github.com/kcrane3576/"
def repoUrl = repo + jobName + ".git"
def deployName = jobName + "_deploy"
def testName = jobName + "_test"
createDeploymentJob(deployName, repoUrl)
createTestJob(testName, repoUrl)
}
buildPipelineJobs()
Since we will be setting up all of our stages in a Shared Library, we need to set up a groovy script (jenkinsJob.groovy) that the poc-micro service points to from its Jenkinsfile.
jenkinsJob.groovy to microservice-pipelinesWe are going to set up our jenkinsJob.groovy file to checkout our micro service code from source control and execute specific maven commands depending on the job that is running.
environment variables (env.JOB_NAME) in order to obtain the Jenkins job name.
deployName = poc-micro_deploy = env.JOB_NAMEtestName = poc-micro_test = env.JOB_NAMEJenkins Set Environment Variables section at the Building a software project Jenkins wiki for more information on env.JOB_NAME.Create and add the below code to vars/jenkinsJob.groovy in the part2 branch of microservice-pipelines.
def call(){
node {
stage('Checkout') {
checkout scm
}
// Execute different stages depending on the job
if(env.JOB_NAME.contains("deploy")){
packageArtifact()
} else if(env.JOB_NAME.contains("test")) {
buildAndTest()
}
}
}
def packageArtifact(){
stage("Package artifact") {
sh "mvn package"
}
}
def buildAndTest(){
stage("Backend tests"){
sh "mvn test"
}
}
In order for our micro services to execute in Jenkins, we need a Jenkinsfile in poc-micro.
Jenkinsfile in our microserviceWe will configure a Jenkinsfile in our microservices to point to our microservice-pipelines Shared Library.
version specifier feature associated with the @Library annotation in Shared Libraries here. Within the @Library annotation, you will always need to provide the name of your Shared Library (e.g. @Library("microservice-pipelines)). However, if you add another @ sign at the end of the Shared Library name (microservive-pipelines), you can tell your Jenkinsfile to read specific branches or tags from your Shared Library. In fact, in the code below, that is what we did. We are signaling our Jenkinsfile to use the part2 branch of our Shared Library with @part2 (e.g. @Library("microservice-pipelines@part2")).Jenkinsfile with the below code.#!/usr/bin/env groovy
// Configure using microservice-pipelines and using "part2" branch
@Library("microservice-pipelines@part2") _
// Entry point into microservice-pipelines
jenkinsJob.call()
seedJobAll of our configuration is set up and our repositories are ready to use. We will now run our seedJob to create our pipelineJob and multibranchPipelineJob based on our seed.groovy set up.
Dashboard > select seedJob -> select Build with Parameters > enter poc-micro in jobName > select Build.
seed.groovyon our part2 branch of microservice-pipelines repository, this script will require an admin approval in Jenkins.Dashboard > select Manage Jenkins > select In-process Script Approval > select Approve.Dashboard > select seedJob -> select Build with Parameters > enter poc-micro in jobName > select Build.Dashboard > verify poc-micro_test and poc-micro_deploy jobs were created.
poc-micro_test jobNow you have a poc-micro_test job that will run every 5 minutes based on the cron we set up, but you can also trigger it manually.
Dashboard > select poc-micro_test > select master > select Build NowBuild History, select the blinking blue circle (red if previous failure) > Observe mvn test executing in Console Output.poc-micro_deploy jobWe can also observe the poc-micro_deploy job executing mvn package.
Dashboard > select poc-micro_deploy > select Build NowBuild History, select the blinking blue circle (red if previous failure) > Observe mvn package executing in Console Output.During this series we set up a seed job that was used to create a pipelineJob (deploy job) and multibranchPipelineJob (test job) when onboarding our poc-micro micro service with our seedJob. Additionally, we set up our Shared Library to use jenkinsJob.groovy to handle the logic that determines which stages are executed depending on the currently running job (env.JOB_NAME). By using a combination of a "seed" job and Shared Libraries, you now have a foundation set up to onboard any number of services the same way while maintaining job specific stage execution. This provides you the ability to streamline your CI and CD requirements for microservices.