Spring Boot Docker Jenkins Pipeline Part-2

Ye Win
8 min readApr 27, 2023

--

Nowadays, Using CI/CD like Jenkins, CircleCI, Bamboo, Gitlab or etc. servers is widely use and helping a lot in application deployment. Jenkins will help you automatic build and push and deploy your application with single click.
Please see the Part 1 here — Spring Boot Docker Jenkins Pipeline Part-1

overview

I showed how to setup and test Jenkins Server in this Part-1 tutorial. Now, I will demonstrate how to connect our application to that Jenkins Server by writing pipeline scripts.

  1. Pipeline definition
  • There are two types of pipelines scripts which used groovy languages.
    i. Scripted Pipeline
    ii. Declarative Pipeline
  • Here, I will use Declarative Pipeline as it’s easy to understand and modern way.

2. Clone sample project

git clone https://github.com/yewin-mm/spring-boot-jpa-docker-jenkins-pipeline.git

You can clone my above sample project or you can build new project.

I recommend to clone my projects and test it first.

3. Test application

  • Run above cloned application to make sure your application is running well in local.
  • For that case, un-comment spring.datasource.url which connect to localhost db and comment out spring.datasource.url which using container db host in application.properties file.
  • Here, if you don’t run your MySQL db with container approach like this way MySQL Container, un-comment spring.datasource.url which connect to localhost db and comment out spring.datasource.url which using container db host in application.properties file.
  • You need to check out docker-compose.yml file if you don't run your MySQL db as container.
  • After successfully running in your local and if you use MySQL as container, comment out spring.datasource.url which connect to localhost db and un-comment spring.datasource.url which using container db host.
  • Please note that, if you run MySQL db as container and gave the container name which not same with my MySQL Container approach, you need to add your container name in application.properties file and docker-compose.yml file.

4. Check Jenkins file

  • You need make sure Maven and Docker Prerequisites are already as in Part-1 (setting up maven and docker) tutorial.
  • Make sure below setting up list as per Part-1 tutorials,
  • i. Maven Plugins
  • ii. Docker plugins
  • iii. Maven Tools setup
  • iv. Add Docker Credentials
  • v. Create Docker Repository
  • After that, in Jenkinsfile, You need to change repositoryas per you created in Docker Hub.
  • You need to change userId , and that id is your Docker Hub account Id.
  • You need to check your setting docker Credentials id in Jenkins Server which need to be same with DOCKER_HUB_CREDENTIALS under environment section of Jenkinsfile.
  • You need to check your installed Maven tools version in Jenkins Server which need to be same with maven field under tools section of Jenkinsfile.

5. Push to GitHub

  • Create Repository in Github.
  • Change git branch name and repository link under stage('Clone Project') in Jenkinsfile. Here, you can use master as branch name when you pushed to your Github.
  • Push this project to your Github with above repository link. (you need to add your git remote url and push with git push -u origin master)

6. Build Application Through Jenkins

  • There are many ways to build application through Jenkins.
  • Here, I will use pipeline to build application to handle dynamic configuration.
  • Using pipeline scripts, there are two ways to build application,
  • i. Using Pipeline in Portal
  • ii. Using Multibranch pipeline
  • Those two approach have different benefits.
  • If you run your application as sample you can use Pipeline and If you run your application with multiple branch, you can use Multibranch pipeline.
  • If you use Multibranch pipeline, you need to remove branch in my Jenkinsfile when you clone from your Github repository.
  • Here, I will use #i approach to build application with sample Pipeline way.

7. Copy Pipeline script and Add in Jenkins

  • Please make sure you already Check Jenkinsfile in above step.
  • Copy the whole script inside Jenkinsfile (which is under project directory).
  • Open Jenkins Server with http://localhost:9000/.
  • Click New Item and give name to spring-boot-jpa-docker-jenkins-pipeline. ref - In Part-1 (Testing pipeline)
  • Under Pipeline Script Section, paste your pipeline script code from Jenkinsfile.
Adding Pipeline Script Sample
  • Click Save.

8. Build

  • After you have entered that above spring-boot-jpa-docker-jenkins-pipeline pipeline job,
  • Click Build Now and refresh page.
  • There you will see job is running with build number #1.
Building Sample
  • Click Console Output which can be reach by clicking down arrow button beside #1 job like above picture, to see console output.
Console Output
  • There, you can see some console which you printed with echo in pipeline script.
  • After that, go back to your Job and there, you can see Successful pipeline with all stages were green line.
Successful pipeline

9. Check Error

  • Here, you may get some error like can’t connect to Docker Hub, can't find repository, etc.
  • You can see above console log to check what exactly error is.
  • If you can’t connect to Docker Hub with permission denied error,
    Please make sure script is already running as I showed in Part-1 (Run Script).
  • If you got error like Can’t Login to Docker Hub with wrong credentials, Please make sure docker hub credentials is already setting up in your Jenkins server as I showed in Part-1 (Add Docker Hub Credentials).
    Please make sure repository name in docker hub is same with repository name in Jenkinsfile.
  • If you got error in Git Clone stage,
    Please make sure you Github repository is existed and public access.
    if it’s private, you need to add Github credentials in Jenkins Server like above adding docker credentials tutorial in Part-1.
    and uncomment git clone with credentials in Jenkinsfile which I already drop as sample.
  • If you are error with Maven,
    Please make sure maven setup is already in Jenkins server as I showed in Part-1 (setting up maven).
    and check name in Jenkinsfile and name that you configure in Jenkins Server is same or not.
  • Please make sure Maven Plugin and Docker plugin is already installed in Jenkins Server.
  • Please make sure your Github Repository is already existed and code are in there and git clone link in Jenkinsfile is correct link to your repository.

10. Check Docker Hub

  • You can use Docker Hub as the public repository cloud storage.
  • So, you can freely pushed your image to docker hub.
  • After you run successfully the whole pipeline,
  • You can check your pushed image is in your Docker Hub Repository.
  • Go to Docker-Hub
  • Login with your account and see under Your created Repository column.
  • You can see your application image is already pushed from local to your Docker Hub.
  • There, you can see image version is 0.0.1 or something like that format.
  • That is due to I added automatic version based on Jenkins Build Number in Jenkins Pipeline Scripted.
  • Version format which I set up is major.minor.patch (eg. 0.0.1) and when the patch version is reach to 999, the minor version was increased and patch version will be start from zero. (eg. 0.1.0)
Check image in Docker Hub

11. Check Test Result Chart

  • You can check test result chart under your application pipeline job in you Jenkins Portal.
  • Because I set up test classes and mapped with Surefire-Report, which can be read from Jenkins Server due to surefire plugin which you already installed when starting Jenkins server.
  • So that Jenkins Server can show Test report as graph chart in relative Pipeline.
  • Chart result can be seen after you have one successful and one failed in your pipeline.
  • So, change docker hub userid in Jenkins credentials or change something in your test class and push to your Github repository.
  • Change docker hub user id to get error in Jenkins Server when pushing image to docker hub is easy to get error. (if you change test code, you need to push code)
  • Please note that you don’t need to create new item for next time in Jenkins server.
  • Go inside your created pipeline job, and click build now button again in your pipeline.
  • You will see failed in pipeline stage.
  • After that you can see Test Result Chart in your pipeline job.
Test Result Graph Chart

12. Test New Image Version in Docker Hub

  • Change back to right userid or back to normal code after you test for failed case.
  • Click build now again and you can see Jenkins build number is increase and image version will use that build number.
  • After successfully running the whole pipeline,
  • Go to Docker-Hub again.
  • Login with your account and see image under your created repository.
  • There, you can see new image with different versions number.
Check New Version Sample
  • Congratulations!!! you’ve successfully built application by using Jenkins Server and pipeline script.
  • This example, I showed only pushed app image to Docker Hub. You can deploy and test the API as well.
  • After that you can see the code, Jenkinsfile, Dockerfile and docker-compose.yml file and see the comment as well. You can learn it, and you can apply in your job or study fields.

Have Fun and Enjoy in Learning Code

Please see the Part 1 here — Spring Boot Docker Jenkins Pipeline Part-1

Below is the Project Link for Spring-boot application with Jenkins.

Below is my GitHub profile and you can checkout all of my projects under repository tag, If you satisfied with my projects or you got some help from my projects, please help me also by giving star on GitHub and give recommendation in LinkedIn as I dropped my LinkedIn profile in below Contact Me section.

Ye Win Github — https://github.com/yewin-mm

✉️ Contact Me

Name — Ye Win
LinkedIn profile — Ye Win
Email Address — yewin.mmr@gmail.com
WhatsApp — +959252656065
Website — https://yewin.me

Project Link: Spring-Boot-jpa-docker-jenkins

🥰 Becoming a Sponsor

If you like any of my projects or if you want to support my work, please kindly consider becoming a sponsor. It gives me great motivation and I can relentlessly maintain my projects and contribute to the open-source community.

Buy Me A Coffee ˗ˏˋ☕ˎˊ˗

--

--

Ye Win
Ye Win

Written by Ye Win

Senior Java Developer, Semi-DevOps and also Software Architect.

No responses yet