0

This is similar to Passing files from Google Cloud Container Builder to Docker build task but I can't seem to figure out what the difference is.

I am attempting to build a simple Java program and package it into a Container using Google Cloud Build. I am following mostly along with https://cloud.google.com/build/docs/building/build-java but using my own repo which is a fork of https://github.com/jchraibi/cloud-native-workshop

steps:
  - name: 'maven:3-openjdk-11'
    args:
      - test
      - '-f=code/inventory-quarkus/pom.xml'
    entrypoint: mvn
  - name: 'maven:3-openjdk-11'
    args:
      - package
      - '-f=code/inventory-quarkus/pom.xml'
      - '-Dmaven.test.skip=true'
    entrypoint: mvn
  - name: 'gcr.io/cloud-builders/docker'
    args: 
      - 'build'
      - '-t'
      - 'gcr.io/$PROJECT_ID/inventory-quarkus'
      - '--build-arg=JAR_FILE=workspace/code/inventory-quarkus/target/$_BUILD_ARTIFACT'
      - '-f' 
      - 'code/inventory-quarkus/gcp/Dockerfile'
      - '/workspace'

The above are the build steps. When the maven package completes, the output is as follows:

Step #1: [INFO] [io.quarkus.deployment.pkg.steps.JarResultBuildStep] Building thin jar: /workspace/code/inventory-quarkus/target/inventory-quarkus-1.0.0-SNAPSHOT-runner.jar

However, no matter what combination of copying things and contexts I attempt, I can't seem to find inventory-quarkus-1.0.0-SNAPSHOT-runner.jar anywhere in the filesystem of the Docker build. Note that the Dockerfile lives in a different place than where the target of the build seems to end up.

FROM openjdk:11
ARG JAR_FILE=JAR_FILE_MUST_BE_SPECIFIED_AS_BUILD_ARG
RUN find .
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-Djava.security.edg=file:/dev/./urandom","-jar","/app.jar"]

In the logs, interestingly, the output of the find command doesn't have a workspace folder at all. I'm sure this has something to do with the fact that the Dockerfile is not in the root of the repo, but I just can't find the right invocation.

The link at the beginning of this post to the other thread ended up using an extra step with the git image to do some kind of copy, but I don't see why that would be required given all of the Google examples don't have to go through any hoops like that.

The build containers documentation has the following statement:

If your Dockerfile and source code are in different directories, add -f and the path to the Dockerfile to the list of arguments in the args field:

This is definitely what I'm doing in my build step. It's clearly finding the Dockerfile, because it's processing the find and the copy. I just can't seem to find where/how to get the Maven package step's workspace folder into the builder, or what the source path would be to copy the file from it.

Erik Jacobs
  • 841
  • 3
  • 7
  • 19

1 Answers1

4

Thank you for your question! I cloned your repo and added a cloudbuild.yaml at the root and added a Dockerfile in the inventory-quarkus/src/main/docker directory. I'm sure this isn't exactly the repo structure you're working with, but the concept should carry over.

Essentially, you want to use the dir field to set your working directory between the steps to more easily pass the data around. This cloudbuild.yaml worked for me:

steps:
  # Test
  - name: 'maven:3-openjdk-11'
    entrypoint: mvn
    args:
      - test
      - '-f=labs/inventory-quarkus/pom.xml'

  # Package
  - name: 'maven:3-openjdk-11'
    entrypoint: mvn
    dir: 'labs/inventory-quarkus'
    args:
      - package 
      - -f=pom.xml 
      - -Dmaven.test.skip=true

  # Docker Build
  - name: 'gcr.io/cloud-builders/docker'
    dir: 'labs/inventory-quarkus'
    args: 
      - 'build'
      - '-t'
      - 'gcr.io/$PROJECT_ID/inventory-quarkus'
      - '--build-arg=JAR_FILE=target/inventory-quarkus-1.0.0-SNAPSHOT-runner.jar'
      - '-f' 
      - 'src/main/docker/Dockerfile'
      - '.'

I also want to draw your attention to the fact that we now have Artifact Registry, if you wanted to store the JAR there and pull it down from the cloud. You can also use it to store you Docker image.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • This did the trick. Thanks! I was aware of the artifact repository, but, for the purposes of this test, I wasn't using it. Using it is now the next step :) – Erik Jacobs May 07 '21 at 16:41