I have been working at a client the past few months that has adopted a desire for a consistent JHipster microservice architecture. We have more than ten microservices that are supported by multiple teams. Developers support multiple services and we needed a uniform approach to deploy quality code quickly to the cloud. We set up Jenkins to support building and testing any branch of all services while maintaining the ability to deploy releases alongside feature development. As Jenkins grew, we decided to maintain our Jenkins related code through source control.
In order for us to satisfy our requirements, we used a couple of the advanced Jenkins tools.
This is the first of a two part series where the target audience is anyone interested in streamlining the process to onboard new micro services.
The source code is available below
microservice-pipelines
)We will set up a Freestyle project (seedJob
) that will be a job that creates jobs
. We will use the Jenkins Job DSL API to set up the foundation for supporting onboarding any number of services.
We will introduce the use of Shared Libraries. In order to maintain support for both of these posts in the same repository, we will take advantage of the version specifier
option associated with the @Library
annotation feature of Shared Libraries. This allows us to configure our JHipster microservices to build from specific branches (or tags) in our Shared Library. Finally, we will update our seedJob
to build a pipelineJob
and multibranchPipelineJob
when onboarding a microservice that will be provided in part 2 of this series.
seedJob
to create and configure other jobs using the Jenkins Job DSL API.seedJob
configuration in github.seedJob
to create a Freestyle Job called poc-micro
.docker pull kcrane121/maven-jenkins:blog
.
docker run -p 8080:8080 -p 50000:50000 kcrane121/maven-jenkins:blog
.http://localhost:8080/
and follow the instructions to complete the set up.docker run -p 8080:8080 -p 50000:50000 kcrane121/maven-jenkins:blog
.
/var/jenkins_home/secrets/initialAdminPassword
in the docker container.Install Suggested Plugins
during the setup process.Now that the prerequisites are out of the way, the first thing we are going to do is set up a Freestyle project (seedJob
) in Jenkins. This job will be used to generate other jobs you want to onboard into Jenkins.
Job DSL
pluginNavigate to http://localhost:8080/
in your browser and login to Jenkins with the credentials you set up (or the default admin ones provided to you during the initial set up). We need to configure Jenkins to use the Jenkins Job DSL API. This provides us the functionality to configure how we want our new jobs built.
Dashboard
> Manage Jenkins
> Manage Plugins
> select Available
tab > search for Job DSL
and install.seedJob
We will set up our seedJob
the same way any other job is created.
New Item
Enter an item name
, enter seedJob
> select the Freestyle project
> select OK
.seedJob
to use microservice-pipelines
github repositoryIn this first part of a two part set of blog posts, we are going to do a little preparation work in this blog to set us up for faster implementation of a Jenkins Shared library in part 2 of this series. We are going to store the code for our seedJob
in a github repository called microservice-pipelines
. Since we are using the microservice-pipelines
repository to load seed.groovy
, we need to configure seedJob
to use this repository.
Dashboard
> select seedJob
> select Configure
.Source Code Management
section > select Git
.Repository URL
field, enter https://github.com/kcrane3576/microservice-pipelines
.
seedJob
to use the seed.groovy
file we will store in githubNow that we have configured Jenkins to use our microservice-pipelines
repository, we need to set up seedJob
to load seed.groovy
from our microservice-pipelines
repository. This is necessary for us to start using the Jenkins Job DSL API functionality.
seedJob
, you could add a groovy script to do the same thing we are doing in our microservice-pipelines
repository (without the benefit of version control).Since we will be using our microservice-pipelines
repository, we will need to add some additional configuration to the seedJob
to get this working.
Dashboard
> select seedJob
> select Configure
.Build
section > select Add Build step
> Select Process Job DSLs
.Look on Filesystem
.DSL Scripts
input field, enter dsl/seed.groovy
(this is the path to the seed.groovy
file we will be setting up later).
seedJob
to use the microservice name as the job nameWe will give our job the name of the microservice we plan to build (poc-micro
). In order to do this we will need to add a String parameter
to the seedJob
that will be used inside of seed.groovy
.
Dashboard
> select seedJob
> select Configure
.This project is parameterized
> select Add Parameter
> select String Parameter
.jobName
in Name
field.Description
field, enter The name of your repo (e.g. poc-micro)
.We are using the microservice-pipelines
github repository. This repository will be used to store our seed
code. In Part 2 of this series, we will include our Shared Library code in this repository.
microservice-pipelines
repository, we have created a directory dsl
with seed.groovy
inside.
seed.groovy
.
String Param
named jobName
from seedJob
to name our Freestyle job.job(jobName) {
description("A simple Freestyle Job created from seed.groovy")
}
Now that we have our seedJob
setup to read in seed.groovy
from our github microservice-pipelines
repository, we are ready to trigger our seedJob
to create a Freestyle job with jobName
.
seedJob
Dashboard
> select seedJob
> select Build Now
.Build History
, select the top red circle.Console Output
.
seed.groovy
. The Script Security Plugin is integrated with the Job DSL
plugin and the Script Security Plugin is set up with a set of scripts that are pre approved for use. Since this is a new script, it will require an admin approval to use.seed.groovy
scriptDashboard
> Manage Jenkins
> In-process Script Approval
.Approve
for the seed.groovy
script.seedJob
Now that we have approved seed.groovy
, we are ready for our seedJob
to run (and succeed).
Dashboard
> select seedJob
> select Build Now
.Build History
, select the top blue circle.Console Output
, you will see GeneratedJob{name='freestyle'}
.
poc-micro
fromseed.groovy
.poc-micro
jobDashboard
and confirm poc-micro
job was created.poc-micro
> select Build Now
.Build History
, select the top blue circle.Console Output
, you will see a successful execution of the poc-micro
job.In this first part of a two part series, we set up the minimum configuration requirements to use seed jobs. Our seedJob
onboards a very simple Freestyle Job (poc-micro) from our microservice-pipelines
repository. The benefit of microservice-pipelines
is that we are now maintaining all of our pipeline configurations in source control. Using seed jobs allows us to onboard/re-onboard services quickly and easily (history is maintained for re-onboarded jobs). At my current client this was extremely helpful because it allowed us to remain agile while adjusting job requirements across all services as requirements changed.
In our next post, we will reconfigure seed.groovy
to build a regular Pipeline job and a Multibranch Pipeline job. Additionally, we will configure the use of Jenkins Shared Library to share and reuse pipeline code between jobs.