Skip to main content

Using Jenkins pipeline with maven, tomcat & docker to deploy spring application.

Assalam o Alaikum

Let's get some hands on Jenkins pipeline.

This blog will help you with getting some basic idea of how to work on Jenkins pipeline with docker.

Prerequisite
1. Some knowledge of Jenkins, at-least knowing its purpose.
2. Basic knowledge of what docker does & get docker running on your system.

What will we achieve
We will be running Jenkins on docker. Create a pipeline project. Maven build will be made with docker. The war file generated will be deployed on tomcat server running on docker.

Lets start with setting up Jenkins on docker.

Jenkins is easy to start on docker but you might face some issues.

First see the docker-compose file I am using

Code:

Why I am using the docker compose file?

This docker-compose file is used to quick start a docker setup with docker containers linked with each other. In this case I am starting a single docker container for which I don't need it but it makes easy to manage configurations. Don't worry if you don't understand this, I will help you out.

First comes the volume. Volume are used to persist data & share data among docker containers. Second comes in the services where I was defining containers. Currently there is only one container so there isn't much between them. Then we define the container name for reference. "build" is used to provide the reference of "Dockerfile" from where it will build. So I have created few folders and kept the file there.

Code:

Ignore the "dns" part. Then I have define the port mapping where port "8888" of host machine maps to port "8000" of docker container.

For "volume", it is the mapping of directories:
  • "jenkins-data:/var/jenkins_home" is used mount volume to directory on docker container.
  • "war-directory:/var/war-directory" is used for the same purpose as above, it will be latter used to access war file.
  • "/var/run/docker.sock:/var/run/docker.sock" is used to run further docker instances to be started from host machine. This might create issue on windows machine. I have resolved it by added environmental variable as "COMPOSE_CONVERT_WINDOWS_PATHS" to value of "1".
And the last "user: root" will define which user to be used when logged in.

That's it from the docker compose file. Now lets move on to running that container. Start a command prompt from the root directory where you have "docker-compose.yml" file. Type "docker-compose up". It will take some time when starting for the first time as it downloads the image.

Now open "localhost:8888" on browser, if all things go well application will start. Go through the basic setup, download plugins. If all goes well you will reach to this screen:



Create a folder from "New Item". Then with in the folder create a pipeline project.

Let me give you an idea how pipelines work. Pipelines are basically a concept of Jenkins with multiple process running in parallel. For different stages we have different or common steps. It is more like pipeline as a code. The JenkinsFile defines the pipeline & its steps.

After you have created the pipeline project. Click on "Configure". There you can scroll down to pipeline. Here you can either write the steps there or you can connect it to a repository. Connecting to the repository means you want to keep the pipeline files separate from the source code. Like I did it here. Make sure to add the "src" folder as Jenkins expects this. Then provide the script path like "src/mvn-demo-project.groovy". Once done just click build now & it will run the build. Obviously nothing will happen as it is empty.

Now lets start with the JenkinsFile.

Code:

This is a declarative pipeline so all the code is inside pipeline

pipeline {
    /* insert Declarative Pipeline here */
}
First there is the agent part which is defined as any. This means that any agent that is available will do the job. Agent is simple a any client that is available to perform the job.

Next is the options part where settings can be defined. "skipDefaultCheckout()" disables the default checkout of the repository we had connected in the configure. We don't need it as we just used the repository to get the Jenkinsfile. After this is the environment area where we can define environmental variables for latter use.

Then we define stages & stages can have multiple stages in them. The first stage is "Checkout". I am using the "GitSCM" plugin to pull from repository into folder. For that I have used a spring boot application. You can create a simple spring boot application from spring boot demo project. I have used this project https://github.com/mtalhajamil/mvn-demo-project.

Next stage is "Build". We use a docker image of maven to build the project. So first we define an agent as to who will build the project. "args" is used to provide arguments to docker run command. Here we have defined the .m2 folder. .m2 saves all the downloaded libraries. So we link that directory to one of our volumes. If we don't do so, code will still execute but will will take a long time every time is starts. Below in steps it used the directory where code was checkout. We kept the checkout directory in a variable because different stages have different folder. Once this part is successfully executed it will make a war file in the project target directory.

There is an issue now. Our Jenkins is running on docker & we have to start another docker. Then transfer file from one docker to another. For this purpose we created a volume named as "war-directory". We copy the war file in that volume. After this we start the tomcat image in next stage. To start the tomcat docker we have linked the "war-directory" to tomcat "webapps" directory. This will copy the war to tomcat webapps directory & start tomcat. If it successfully starts. You can see some signs of application. In my case "http://localhost:8080/mvnproject-0.0.1-SNAPSHOT".


This is the "Blue Ocean" UI, it is an extended UI for Jenkins. This shows all the stages of pipeline. And we can see the logs against each step.

This was just a basic explanation of how things work in Jenkins pipeline. There are many things that we can do better, but it's just to get around things.

That's all from me. Waiting for your comments on how helpful it was 😃

Have a nice day.

Comments

Post a Comment