Automate, customize, and execute your software development workflows right in your repository with GitHub Actions. You can discover, create, and share actions to perform any job you’d like, including CI/CD, and combine actions in a completely customized workflow.

Prepare Docker Hub repository

Create a repository in your Docker hub for this GitHub repository Docker image

Create workflow in GitHub Actions

Go to the Actions page of your GitHub repository, and create a whatever workflow you’d like.

Image for post

Write workflow and define env

Use a branch dev to trigger the CI. Define the build args “PROFILE” or any other argument in the workflow and you will use it as the profile to run SpringBoot. Be noted that you should define the Docker Hub account and repository here.

on:
  push:
    branches:
      - dev

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to build and push docker
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_HUB_USER }}
          password: ${{ secrets.DOCKER_HUB_PASS }}

      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          pull: true
          push: true
          cache-from: type=registry,ref=hustakin/hello-world-docker-action:latest
          cache-to: type=inline
          tags: hustakin/hello-world-docker-action:latest
          build-args: PROFILE=nectar,ARG2=test

Create secrets for the Github repository

You should create the secrets DOCKER_HUB_USER (Docker Hub account) and DOCKER_HUB_PASS (Docker Hub password) as per you defined in the above workflow.

Image for post

Create Dockerfile

Create a Dockerfile in the root folder of your project. You can specify the build argument in the Dockerfile and read it. You will see the Github Action workflow triggered after you push the code to the dev branch.

Image for post

See GitHub Actions workflow logs

You will see the build argument PROFILE could be get in your Dockerfile.

Image for post

Write Dockerfile for SpringBoot

The jar file of your SpringBoot project can be built by command: mvn clean package -Dmaven.test.skip=true. Write the Dockerfile to run your SpringBoot jar file. Please be noted you cannot read the ARG directly, but you can pass it to an env variable and read the variable instead.

FROM openjdk:8-jre
ARG PROFILE
ENV PROFILE_VAR=$PROFILE
VOLUME /tmp
## Add the built jar for docker image building
ADD target/hello-world-docker-action.jar hello-world-docker-action.jar
ENTRYPOINT ["/bin/bash", "-c", "java","-Dspring.profiles.active=$PROFILE_VAR","-jar","/hello-world-docker-action.jar"]
## DO NOT USE(The variable would not be substituted): ENTRYPOINT ["java","-Dspring.profiles.active=$PROFILE_VAR","-jar","/hello-world-docker-action.jar"]
## CAN ALSO USE: ENTRYPOINT java -Dspring.profiles.active=$PROFILE_VAR -jar /hello-world-docker-action.jar
EXPOSE 80

Unlike the shell form, the exec form does not invoke a command shell. This means that normal shell processing does not happen. For example, ENTRYPOINT [ “echo”, “$HOME” ] will not do variable substitution on $HOME. If you want shell processing then either use the shell form or execute a shell directly, for example: ENTRYPOINT [ “sh”, “-c”, “echo $HOME” ]. When using the exec form and executing a shell directly, as in the case for the shell form, it is the shell that is doing the environment variable expansion, not docker.

Another way to pass variables

There is another way to pass variables if the above way doesn’t work. You can put the command into a script file and add the shell as entrypoint.

FROM openjdk:8-jre
ARG PROFILE
ENV PROFILE_VAR=$PROFILE
VOLUME /tmp
## Add the built jar for docker image building
ADD target/hello-world-docker-action.jar hello-world-docker-action.jar

## Build a shell script because the ENTRYPOINT command doesn't like using ENV
RUN echo "#!/bin/bash \n java -Dspring.profiles.active=${PROFILE_VAR} -jar /hello-world-docker-action.jar" > ./entrypoint.sh
RUN chmod +x ./entrypoint.sh

## Run the generated shell script.
ENTRYPOINT ["./entrypoint.sh"]
EXPOSE 80

Write workflow to build jar

Add a step to run the maven building command in order to get the above jar file.

on:
  push:
    branches:
      - dev

jobs:
  hello_world_job:
    runs-on: ubuntu-latest
    name: A job to build and push docker
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8

      - name: Build with Maven
        run: mvn clean package -Dmaven.test.skip=true

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_HUB_USER }}
          password: ${{ secrets.DOCKER_HUB_PASS }}

      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          pull: true
          push: true
          cache-from: type=registry,ref=hustakin/hello-world-docker-action:latest
          cache-to: type=inline
          tags: hustakin/hello-world-docker-action:latest
          build-args: PROFILE=nectar,ARG2=test

#github #docker #devops

Setup CI/CD on GitHub Actions for Multiple Environments Deployment
14.50 GEEK