Setting Up CI/CD for Spring Boot Application with GitHub Actions and Docker



In this blog post, we'll walk through the process of creating a CI/CD pipeline for a Spring Boot application using GitHub Actions. We'll cover each step, from setting up the GitHub Actions workflow to building and pushing Docker images to Docker Hub.

Introduction

Continuous Integration and Continuous Deployment (CI/CD) are essential practices in modern software development, helping automate the build, test, and deployment processes. In this example, we'll set up a CI/CD pipeline using GitHub Actions to build, test, and deploy a Spring Boot application, and then push the Docker image to Docker Hub.

GitHub Actions Workflow

Let's dive into the github/workflows/main.yml file, which defines our CI/CD pipeline.

yml file:

name: Build & Deploy CRM Spring app

on:
push:
branches:
- main

jobs:
build-deploy:
name: Build and Deploy Spring boot
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup JDK 17
uses: actions/setup-java@v3
with:
distribution: 'corretto'
java-version: 17

- name: Unit Tests
run: mvn -B test --file pom.xml

- name: Build the application
run: |
mvn clean
mvn -B package --file pom.xml

- name: Build Docker Image
uses: docker/build-push-action@v2
with:
context: .
dockerfile: Dockerfile
push: false
tags: ${{ secrets.DOCKER_HUB_USERNAME }}/ci-cd:today
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
- name: Push to Docker Hub
uses: docker/build-push-action@v2
with:
context: .
dockerfile: Dockerfile
push: true
tags: ${{ secrets.DOCKER_HUB_USERNAME }}/ci-cd:today

Breakdown of the Workflow

  1. Workflow Name and Trigger:

    • name: Build & Deploy CRM Spring app: This is the name of the workflow.
    • on: push: branches: - main: The workflow is triggered on every push to the main branch.
  2. Job Definition:

    • jobs: build-deploy: Defines a job named build-deploy that runs on the latest Ubuntu runner.
    • steps: Specifies the steps for the job.
  3. Steps Explanation:

    • Checkout Code:

      yaml

      - name: Checkout code
      uses: actions/checkout@v3

      This step checks out the code from the repository so that it can be used in the workflow.

    • Setup JDK 17:

      yaml

      - name: Setup JDK 17
      uses: actions/setup-java@v3
      with:
      distribution: 'corretto'
      java-version: 17

      Sets up the Java Development Kit (JDK) 17 required to build the Spring Boot application.

    • Run Unit Tests:

      yaml

      - name: Unit Tests
      run: mvn -B test --file pom.xml

      Executes unit tests to ensure that the code is functioning correctly.

    • Build the Application:

      yaml

      - name: Build the application
      run: |
      mvn clean
      mvn -B package --file pom.xml

      Cleans and builds the application, creating a JAR file that will be used in the Docker image.

    • Build Docker Image:

      yaml

      - name: Build Docker Image
      uses: docker/build-push-action@v2
      with:
      context: .
      dockerfile: Dockerfile
      push: false
      tags: ${{ secrets.DOCKER_HUB_USERNAME }}/ci-cd:today

      Builds the Docker image from the Dockerfile but does not push it to Docker Hub at this step.

    • Login to Docker Hub:

      yaml

      - name: Login to Docker Hub
      uses: docker/login-action@v1
      with:
      username: ${{ secrets.DOCKER_HUB_USERNAME }}
      password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

      Authenticates with Docker Hub using credentials stored in GitHub Secrets.

    • Push Docker Image:

      yaml

      - name: Push to Docker Hub
      uses: docker/build-push-action@v2
      with:
      context: .
      dockerfile: Dockerfile
      push: true
      tags: ${{ secrets.DOCKER_HUB_USERNAME }}/ci-cd:today

      Pushes the Docker image to Docker Hub with the specified tag.

Dockerfile

Next, let's look at the Dockerfile that defines how the Docker image is built.

dockerfile

FROM openjdk:17-jdk

WORKDIR /app

COPY target/CRM-0.0.1-SNAPSHOT.jar /app/CRM.jar

EXPOSE 8080

CMD ["java", "-jar", "CRM.jar"]

Explanation

  1. Base Image:

    dockerfile

    FROM openjdk:17-jdk

    Uses the OpenJDK 17 image as the base for the Docker container.

  2. Working Directory:

    dockerfile

    WORKDIR /app

    Sets the working directory inside the container to /app.

  3. Copy JAR File:

    dockerfile

    COPY target/CRM-0.0.1-SNAPSHOT.jar /app/CRM.jar

    Copies the built JAR file into the container.

  4. Expose Port:

    dockerfile

    EXPOSE 8080

    Exposes port 8080 for the application.

  5. Run the Application:

    dockerfile

    CMD ["java", "-jar", "CRM.jar"]

    Specifies the command to run the application when the container starts.

In this blog post, we've outlined how to set up a CI/CD pipeline for a Spring Boot application using GitHub Actions and Docker. By following these steps, you can automate the build, test, and deployment process, ensuring that your application is always up-to-date and deployed efficiently.

Feel free to adapt the provided YAML and Dockerfile to fit your specific needs. Happy coding!


Comments