0

I have been using Dockerfile and cloudbuild.yaml to build images succesfully,

But now I need to have the BRANCH_NAME value inside the Dockerfile, and since this requirement showed up, my docker builds have started failing.

Here below is my cloudbuild.yaml file :

steps:
- name: 'gcr.io/cloud-builders/docker'
  env:
    - '$BRANCH_NAME=${_$BRANCH_NAME}'
  entrypoint: 'bash'
  args: ['-c', 'docker pull gcr.io/$PROJECT_ID/github.com/videoo-io/videoo-render-$BRANCH_NAME:latest || exit 0']
- name: 'gcr.io/cloud-builders/docker'
  env:
    - '$BRANCH_NAME=${_$BRANCH_NAME}'
  args: [
            'build',
            "--build-arg=ENV=$_BRANCH_NAME",
            '-t', 'gcr.io/$PROJECT_ID/github.com/videoo-io/videoo-render-$BRANCH_NAME:latest',
            '--cache-from', 'gcr.io/$PROJECT_ID/github.com/videoo-io/videoo-render-$BRANCH_NAME:latest',
            '.'
        ]
substitutions:
  _BRANCH_NAME: $BRANCH_NAME
options:
  dynamic_substitutions: true
images: ['gcr.io/$PROJECT_ID/github.com/videoo-io/videoo-render-$BRANCH_NAME:latest']
timeout: 7200s

And below is my Dockerfile :

ARG  BRANCH_NAME=$_BRANCH_NAME

FROM --platform=amd64 gcr.io/videoo7/github.com/videoo-io/videoo-render-${BRANCH_NAME}:latest

EXPOSE 80
COPY . /app
WORKDIR /app
RUN cd /app && cmake . && make

# Define default command.
RUN chmod +x /app/entrypoint.sh
ENTRYPOINT ["/app/entrypoint.sh"]

I cannot get BRANCH_NAME correctly as a substitute variable as suggested here : Google Cloud Build Substitute Variables Link

This is the failing build logs :

Step #0: a561219ed4f5: Pull complete
Step #0: Digest: sha256:09a3f79713e906ff0640bac04c9cb74523ee33e930afe8683f15f2cd5904fbc1
Step #0: Status: Downloaded newer image for gcr.io/videoo7/github.com/videoo-io/videoo-render-main:latest
Step #0: gcr.io/videoo7/github.com/videoo-io/videoo-render-main:latest
Finished Step #0
Starting Step #1
Step #1: Already have image (with digest): gcr.io/cloud-builders/docker
Step #1: Sending build context to Docker daemon  196.9MB

Step #1: Step 1/8 : ARG  BRANCH_NAME=$_BRANCH_NAME
Step #1: Step 2/8 : FROM --platform=amd64 gcr.io/videoo7/github.com/videoo-io/videoo-render-${BRANCH_NAME}:latest
Step #1: invalid reference format
Finished Step #1
ERROR
ERROR: build step 1 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1

Here is my google cloud build trigger setup :

BRANCH_NAME

What shall I do in order to have BRANCH_NAME value inside the Dockerfile ?

london_utku
  • 1,070
  • 2
  • 16
  • 36
  • In your Dockerfile can you try `ARG BRANCH_NAME` instead of `ARG BRANCH_NAME=$_BRANCH_NAME` – Sathi Aiswarya Mar 22 '23 at 07:58
  • Now I get this : Step #1: Step 1/8 : ARG BRANCH_NAME Step #1: Step 2/8 : FROM --platform=amd64 ${BRANCH_NAME} Step #1: base name (${BRANCH_NAME}) should not be blank Finished Step #1 ERROR ERROR: build step 1 "gcr.io/cloud-builders/docker" failed: step exited with non-zero status: 1 – london_utku Mar 22 '23 at 08:59
  • In the `env` section both steps can you set `BRANCH_NAME=$BRANCH_NAME` as in this [document](https://cloud.google.com/build/docs/configuring-builds/substitute-variable-values#yaml) that you shared – Sathi Aiswarya Mar 22 '23 at 10:47
  • I have tried setting BRANCH_NAME as suggested : - name: 'gcr.io/cloud-builders/docker' args: [ 'build', '-t', 'gcr.io/$PROJECT_ID/github.com/videoo-io/videoo-render-$BRANCH_NAME:latest', '--cache-from', 'gcr.io/$PROJECT_ID/github.com/videoo-io/videoo-render-$BRANCH_NAME:latest', '.' ] env: - 'BRANCH_NAME=$BRANCH_NAME' Step #1: Step 1/8 : ARG BRANCH_NAME Step #1: Step 2/8 : FROM --platform=amd64 ${BRANCH_NAME} Step #1: base name (${BRANCH_NAME}) should not be blank Still no luck – london_utku Mar 22 '23 at 11:00
  • I am building through Google Cloud Build trigger, where can I execute this command : gcloud builds submit --config=cloudbuild.yaml \ --substitutions=TAG_NAME="test" as suggested by this document https://cloud.google.com/build/docs/configuring-builds/substitute-variable-values#yaml – london_utku Mar 22 '23 at 11:01
  • Why there is not Dockerfile example in here https://cloud.google.com/build/docs/configuring-builds/substitute-variable-values#yaml – london_utku Mar 22 '23 at 11:29
  • Use substitutions in your build config file to substitute specific variables at build time Also have a look at this stackoverflow [link1](https://stackoverflow.com/questions/58886293/getting-current-branch-and-commit-hash-in-github-action), [link2](https://stackoverflow.com/questions/49134348/possible-to-get-the-git-branch-name-in-a-dockerfile)&[link3](https://stackoverflow.com/questions/58939845/get-branch-name-in-dockerfile) – Sathi Aiswarya Mar 22 '23 at 13:07

0 Answers0