Saturday, October 19, 2024

How to Create Spring Boot CI/CD With Jenkins

Jenkins is a popular automation tool that provides an alternative to CI/CD processes. It automates tasks such as building, testing, and deploying applications. In this article, we will delve into how to use Jenkins to automate a Spring Boot application.

First, we need to create a docker-compose file to run Jenkins using a Docker image. However, we encounter a common issue: the docker: not found error within the platform. Despite installing Docker extensions, the problem persists.

To overcome this challenge, we create a custom image based on the Jenkins image using a Dockerfile. We include the Docker-ce-cli package in this image to avoid Docker command errors within the Jenkins environment.

  1. Navigate to the directory containing the Dockerfile and run the command docker build -t [imageName] . to create the image. Use docker images to confirm its creation.

  2. Go to the root directory of our Spring Boot application and create a docker-compose file. Include the created image and execute the command docker compose up -d.

For remote server deployments, we can create a dedicated folder and configure the $Directory:/var/jenkins_home volume to prevent data loss when containers are restarted or stopped. This approach ensures data persistence within the server folder.

  1. After starting the container, use the docker container ls command for verification.

  2. Access the Jenkins setup screen. Since we're working locally, we go to localhost:8080.

  3. Obtain the password by running the docker logs -f [container_name] command. The password will be displayed in yellow.

  4. Paste the password and proceed. Select the "Install Suggested Plugins" option.

  5. Once the installation process completes, create an administrator account.

  6. Configure the Jenkins application URL.

  7. After successful configuration, install necessary plugins.

  8. Navigate to "Manage Jenkins," then "Plugins," and then "Available Plugins" to install the following:

  • [Plugin list]

Restart the application.

Before creating our Jenkins project, we need a Dockerfile for our Spring Boot application. This Dockerfile will download dependencies, build the application into a JAR file, copy it to the /app directory, and then run the application.

It's crucial to find the appropriate JDK image based on your Maven and Java versions. Once we have the Dockerfile in place, we can move on to Jenkins.

  1. Go to "Dashboard," then "New Item," and choose "Freestyle Project."

  2. Configure the GitHub settings.

  3. Among the installed plugins, we have "Generic Webhook Trigger." This plugin allows our project to be triggered whenever a commit is made. If you wish to use it, check the corresponding checkbox.

  4. Add a build step and select "Execute Shell."

  5. Enter the following commands:

  • docker build --rm -t [buildName] .

  • docker run --rm -d -p [Port:Port] --name [tagName] [buildName]

Apply and save the changes.

  1. Go to the project page and click "Build Now." Observe the build process under the "Build" tab.

  2. To trigger Jenkins configurations after each commit, add a webhook to the GitHub repository.

  3. Navigate to github.com/[username]/[projectName]/settings/hooks, add a webhook, and include the Payload URL. Save the changes.

Jenkins streamlines the automation of building, testing, and deploying your Spring Boot application. By following these steps, you can leverage the power of Jenkins to streamline your development workflow.

0 comments:

Post a Comment