Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Action to maven build followed by Docker build push

I am a novice with GitHub Actions. I have the below DockerFile

FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

I am trying to use the GitHub Action Publish Docker and below is my action code

name: Publish Docker
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Publish to Registry
      uses: elgohr/Publish-Docker-Github-Action@master
      with:
        name: myDocker/repository
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}

The build fails with below error

Step 5/6 : COPY ${JAR_FILE} app.jar
COPY failed: no source files were specified

I assume it's not able to get the jar file under target folder. Should I add another step for Maven Build before docker build ensure target folder is generated with Jar.

I updated my action as below yet same error

name: Build and push Docker images

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

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - 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 -B package --file pom.xml
      - uses: actions/checkout@master
      - name: Publish to Registry
        uses: elgohr/Publish-Docker-Github-Action@master
        with:
          name: vinodjayachandran/spring-boot-docker
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

Refer my repo on git for exact code: https://github.com/vinodjayachandran/spring-boot-docker/

like image 485
Vinod Jayachandran Avatar asked Apr 23 '20 13:04

Vinod Jayachandran


People also ask

Does GitHub Actions use Docker?

GitHub Actions A GitHub Action is used inside a GitHub workflow. An action can be built with Javascript or with Docker. To use an action in a workflow, you use uses: followed by a reference to the action, which is just a GitHub repository.

Do GitHub Actions run in containers?

GitHub Actions has a relatively little known feature where you can run jobs in a container, specifically a Docker container. I liken it to delegating the entire job to the container, so every step that would run in the job will instead run in the container, including the clone/checkout of the repository.


1 Answers

The solution is to have multi stage docker build. Essentially do a maven build from DockerFile and not from GitHub Actions.

My Final DockerFile

#
# Build stage
#
FROM maven:3.6.0-jdk-11-slim AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package

#
# Package stage
#
FROM openjdk:11-jre-slim
COPY --from=build /home/app/target/*.jar /usr/local/lib/app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/local/lib/app.jar"]

GitHub Action :

name: Build and push Docker images

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

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Publish to Registry
      uses: elgohr/Publish-Docker-Github-Action@master
      with:
        name: vinodjayachandran/spring-boot-docker
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
like image 125
Vinod Jayachandran Avatar answered Oct 21 '22 06:10

Vinod Jayachandran