- Get link
- X
- Other Apps
- Get link
- X
- Other Apps
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
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 themain
branch.
Job Definition:
jobs: build-deploy
: Defines a job namedbuild-deploy
that runs on the latest Ubuntu runner.steps
: Specifies the steps for the job.
Steps Explanation:
Checkout Code:
yaml- name: Checkout codeuses: actions/checkout@v3This step checks out the code from the repository so that it can be used in the workflow.
Setup JDK 17:
yaml- name: Setup JDK 17uses: actions/setup-java@v3with:distribution: 'corretto'java-version: 17Sets up the Java Development Kit (JDK) 17 required to build the Spring Boot application.
Run Unit Tests:
yaml- name: Unit Testsrun: mvn -B test --file pom.xmlExecutes unit tests to ensure that the code is functioning correctly.
Build the Application:
yaml- name: Build the applicationrun: |mvn cleanmvn -B package --file pom.xmlCleans and builds the application, creating a JAR file that will be used in the Docker image.
Build Docker Image:
yaml- name: Build Docker Imageuses: docker/build-push-action@v2with:context: .dockerfile: Dockerfilepush: falsetags: ${{ secrets.DOCKER_HUB_USERNAME }}/ci-cd:todayBuilds 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 Hubuses: docker/login-action@v1with: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 Hubuses: docker/build-push-action@v2with:context: .dockerfile: Dockerfilepush: truetags: ${{ secrets.DOCKER_HUB_USERNAME }}/ci-cd:todayPushes 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
Base Image:
dockerfileFROM openjdk:17-jdk
Uses the OpenJDK 17 image as the base for the Docker container.
Working Directory:
dockerfileWORKDIR /app
Sets the working directory inside the container to
/app
.Copy JAR File:
dockerfileCOPY target/CRM-0.0.1-SNAPSHOT.jar /app/CRM.jar
Copies the built JAR file into the container.
Expose Port:
dockerfileEXPOSE 8080
Exposes port 8080 for the application.
Run the Application:
dockerfileCMD ["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!
Automation
Build Pipeline
CI/CD
Continuous Integration
Deployment
DevOps
Docker
Docker Hub
GitHub Actions
Java
Maven
Spring Boot
- Get link
- X
- Other Apps
Comments
Post a Comment