Vinod Jayachandran
Vinod Jayachandran

Reputation: 3898

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/

Upvotes: 5

Views: 3379

Answers (2)

Vinod Jayachandran
Vinod Jayachandran

Reputation: 3898

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 }}

Upvotes: 5

Servet TAS
Servet TAS

Reputation: 63

Everything looking ok but you must change;

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

after you will see work like below.

docker build -t deneme:latest .
Sending build context to Docker daemon   2.56kB
Step 1/6 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/6 : RUN addgroup -S spring && adduser -S spring -G spring
 ---> Using cache
 ---> ae235df4b0a2
Step 3/6 : USER spring:spring
 ---> Using cache
 ---> 9ac909bf6b8c
Step 4/6 : ENV JAR_FILE=target/*.jar
 ---> Running in 7c8948d44b63
Removing intermediate container 7c8948d44b63
 ---> a26f368cfdba
Step 5/6 : COPY ${JAR_FILE} app.jar
COPY failed: no source files were specified
root@servet:~/denem# ls
app.jar  Dockerfile
root@servet:~/denem# nano Dockerfile
root@servet:~/denem# docker build -t deneme:latest .
Sending build context to Docker daemon   2.56kB
Step 1/6 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/6 : RUN addgroup -S spring && adduser -S spring -G spring
 ---> Using cache
 ---> ae235df4b0a2
Step 3/6 : USER spring:spring
 ---> Using cache
 ---> 9ac909bf6b8c
Step 4/6 : ENV JAR_FILE=*.jar
 ---> Running in a78dfae771a0
Removing intermediate container a78dfae771a0
 ---> bdd4cab1bd9c
Step 5/6 : COPY ${JAR_FILE} app.jar
 ---> 9bec1d061268
Step 6/6 : ENTRYPOINT ["java","-jar","/app.jar"]
 ---> Running in c23f2ddb682b
Removing intermediate container c23f2ddb682b
 ---> e24407464a2b
Successfully built e24407464a2b
Successfully tagged deneme:latest

Upvotes: 0

Related Questions