Tuesday, September 27, 2022

CI with GitHub Actions, Docker Hub and Spring Boot

GitHub Actions

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. You can create workflows that build and test every pull request to your repository, or deploy merged pull requests to production.

GitHub Actions provided several options to set up a workflow, in this case we used java with maven.









Click on Java with Maven Configure and set up this workflow. github/workflows/ directory, create a new file called ci.yml


















Workflow file

A workflow is a configurable automated process made up of one or more jobs. You must create a YAML file to define your workflow configuration.

Specifies the trigger for this workflow. in this case push.
on:
  push:
    branches: [ "master" ]
  pull_request:
    branches: [ "master" ]

Groups together all the jobs that run in the workflow.

jobs:

Build Spring boot application with maven

- name: Set up JDK 17
        uses: actions/setup-java@v2
        with:
          java-version: '17'
          distribution: 'adopt'
          cache: maven
      - name: Build with Maven
        run: mvn -B package --file pom.xml

Build imagen and publish in Docker Hub, Set your dockerhubuser and dockerHubRepository.
I get yaml file from GitHub Docker build.

- name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          context: .    
          push: true
          tags: dockerhubuser/dockerHubRepository:${{github.run_number}}


GitHub secrets

DOCKERHUB_USERNAME And DOCKERHUB_TOKEN 















Docker Hub Token

Profile -> Account Settings -> Security -> New Access token.







Dockerfile

FROM openjdk:17-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Spring boot with maven  and Docker Configuration:


name: Publish Docker Image

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

jobs:
docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
java-version: '17'
distribution: 'adopt'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: dockerhubuser/dockerHubRepository:${{github.run_number}}

Running the Workflow
















Source Code


Here on Github.




References:


https://www.youtube.com/watch?v=R8_veQiYBjI
https://github.com/docker/build-push-action
https://github.com/actions
https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions
https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions
https://medium.com/@mamthamalar/ci-with-github-actions-publish-docker-images-de8f1f610fac







No comments:

Post a Comment

Deploying a Spring Boot Application with Cloud SQL and Cloud Run on GCP

In this post, we'll explore how to provision Cloud SQL instances with different connectivity options using Terraform and then access the...