2.2 Pipeline CI/CD — GitHub, Maven, Jenkins, Artifactory, SonarQube, Docker, Ansible, Tomcat
This is the continuation to the Previous Post 2.1 Pipeline CI/CD — GitHub, Maven, Jenkins, Artifactory, SonarQube, Docker, Ansible, Tomcat
- Breaking the Code:
Link to the Jenkins File — Click
Jenkins Pipeline Style: Declarative Jenkins Pipeline
In the above Screenshot Fig.1.1
- Defined the maven and Artifactory methods globally in Jenkins File
- Defined the method DockerTag() globally to get the latest commit ID
- Included the options to have the timestamps and log rotation of the builds.
- Set the tools name of Java and Maven location as mentioned in the Jenkins — Global Tool Configuration ( Refer Screenshot Fig1.2) below.
Set the Environment of SonarQube scanner tool name as mentioned in the Jenkins — Global Tool Configuration ( Refer Screenshot Fig1.2) above.
- In the Screenshot above Fig1.3 explains the Artifactory, SonarQube Analysis, SonarQube Quality gate Stage.
Note: “ Quality Gate” stage is written in the Jenkins File to make the Jenkins build fail if the Sonar Quality Gate metrics is not met.
- In the Screenshot above Fig1.4, the docker image is build and pushed to the Docker Hub.
- Running the Docker Container of tomcat to port 8050
Note: Dockerfile is found here
- In the above Screenshot Fig1.5, changed the RUN command to startup the tomcat server with the Port 8050 ( Default port is 8080)
- The above screenshot explains about the triggering of the Ansible Playbook. Please find the screenshot of the Ansible Playbook below. Refer Fig.1.6
- In the Playbook, vars is written to get the output of the latest commit ID of the Git referencing from the environment variable ‘DOCKER_TAG’ [Refer Fig1.1] in the Jenkinsfile
- Defined the tasks to delete the previous / old containers and the images.
- ignore_errors is mentioned in the tasks for error handling.
- “creating docker image” task is to run the container with the tag of the latest git commit.
Conclusion:
We have integrated the Jenkins with the Ansible and made the playbook to run in the target machines. In our lab setup, the targets are the tomcat server ( created from the Docker) which is mentioned in the inventory file.
I hope this guide helped you! Enjoy Learning and have fun!!! 😊😊