I expect you to be familiar with basics of Jenkins. If you’re not please visit Introduction to Jenkins, this post will take you through very basics of Jenkins. What I want to introduce to you, are post setup type of things. Means, you have already setup Jenkins and now you are worried about how to do pipelining of the project.
I will take you through the problems that might come when you pick working on a Jenkins Pipeline project that uses Docker images. To understand what is a Docker Image please visit Introduction to Docker, this post will take you through the basics of Docker Image.
Pipe-Lining a Maven Project:
Creating a Pipe-line is similar to create a simple Jenkins Job, but in here you have to give some different configurations for your job. Let’s start.
1. Goto Jenkins Home and click new Item.
2. Select the pipeline option and give a suitable job name and press OK.
3. Now give proper configurations for this Job as defined below:
a. In General tab you can give the project based security to a particaular person/group of people and define what role/permissions you want this person/group to be involved in.
b. You don’t need to touch other settings in General/Job Notification/Office 365 Connector Tabs for a simple pipeline.
c. In next Tab i.e. Build Triggers, you can define the type of trigger you want to automate to build your Job or you can leave it blank if you want to manually trigger your Build.
d. Then the most important configuraion is Pipeline tab.
i) There are two ways to make pipeline, first you can write a script in given textbox in Pipeline Script option, second you can select Pipeline Script from SCM, and provide a Jenkinsfile in your project and give the path in script path.
ii) Then define your Source Code Management(SCM) in SCM option i.e. Git in our case.
iii) Then define your Git Repository URL, your will see an error as show below in the image steps, the next image show how to resolve it. You’ll have to create a proper Jenkins credential for the given Repo and select it in Credential option. The error will then disapear.
Follow below image references:
That’s how a Pipeline looks like. Now click Apply and then Save, you’ll be redirected to home. This is how you create a Pipeline on Jenkins. Now Lets try to implement this Pipeline in your given Repo.
How to implement jenkins in Repository:
You need to create and define a Jenkinsfile in your project which can be read by Jenkins Pipeline. So, we’ll create this file in parallel to parent pom.xml just to avoid long paths to find jenkinsfile.
This is how a Jenkinsfile looks like, this file is written in Groovy mostly and contains all commands which are needed to build your project as per expectation, e.g. the builderMethod method is an automated function which defines all commands that are required to build my application. you may hard code them here in Jenkins File.
Now to Create a Docker Image of your build you can configure Docker in your Project. Open you IMPL pom.xml and define the Docker Properties and profiles there.
Now whenever an mvn install will be run on your code, it’ll create an docker image on the defined docker Registry URL. You need some more this docker process. Create a docker folder in your src/main and create a docke-bundle.xml in it. insert following code:
Now create a simple file named Dockerfile parallel to you docker-bundle.xml and insert following code in it.
Create an entry Point for your execution i.e. EntryPoint.sh
If you want to deploy your docker image on DC/OS then you’ll some certificates also don’t forget to provide the certificates and the DCOS token/credentials of course.
Create a marathon.json for your application/environment level variables. For example see the below code:
Now, I think your application is ready to deploy. Go ahead and follow below steps:
- Push your code in your repository and press build now in your Jenkins Job.
You’ll see your job has started to build. You can check the progress under Stage View.
See the console output of a build and read the logs to be sure everything is getting execute by Jenkins. Now your Pipeline has been built and working fine.
Now click on the build and looks at the build name this is the name of your docker image. You can promote this image in any running environment to deploy.
References: These are my finding/understanding while working on my project.
Please post your comments below if I left something and if it could be better someway. Please feel free to add your knowledge of the area in comment or contact me at firstname.lastname@example.org.