Google Cloud Build Docker build-arg from file

Wang Yang :

I have a problem when I use Google Cloud Build. I can't pass the key into docker by cloudbuild.yaml

Google buildfile.yaml:

- name: 'gcr.io/cloud-builders/gcloud'
  args:
  - kms
  - decrypt
  - --ciphertext-file=A.enc
  - --plaintext-file=/root/.ssh/id_rsa
  - --location=global
  - --keyring=keyringxxx
  - --key=keyxxx
  volumes:
  - name: 'ssh'
    path: /root/.ssh
- name: 'gcr.io/cloud-builders/docker'
  args: [
    'build', '.',
    '-t', 'gcr.io/$PROJECT_ID/xxx:latest',
    '--build-arg', 'READ_KEY=`cat /root/.ssh/id_rsa`'
  ]
  volumes:
  - name: 'ssh'

Dockerfile:

FROM golang:1.11 AS builder

ARG READ_KEY
RUN mkdir -p ~/.ssh &&  \
    echo "$READ_KEY" > ~/.ssh/id_rsa && \
    chmod 0600 ~/.ssh/id_rsa && \
    ssh-keyscan github.com >> /root/.ssh/known_hosts && \
    git config --global url.ssh://[email protected]/XXXX.insteadOf https://github.com/XXXX

......

The above code failed. cat does not work.

ryanhos :

The GCloud Docker Builder is using the Exec form of ENTRYPOINT. Your arguments from the cloudbuild.yaml are not being passed to a shell, thus your cat will not be executed.

Why not direct KMS to write the id_rsa directly to the /workspace and do away with the ssh volume altogether?

- name: 'gcr.io/cloud-builders/gcloud'
  args:
  - kms
  - decrypt
  - --ciphertext-file=A.enc
  - --plaintext-file=/workspace/id_rsa
  - --location=global
  - --keyring=keyringxxx
  - --key=keyxxx
- name: 'gcr.io/cloud-builders/docker'
  args: [
    'build', '.',
    '-t', 'gcr.io/$PROJECT_ID/xxx:latest'
  ]

And the Dockerfile becomes:

FROM golang:1.11 AS builder

RUN mkdir -p ~/.ssh
COPY id_rsa ~/.ssh/
RUN ssh-keyscan github.com >> ~/.ssh/known_hosts && \
    chmod -R 0600 ~/.ssh/ && \
    git config --global url.ssh://[email protected]:.insteadOf https://github.com

Don't forget to mount that .gitconfig into the additional build steps. I just make it part of my CI build script, rather than requiring the extra volume.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Docker - Build Arg in Source File

Pass arg to docker build

Passing files from Google Cloud Container Builder to Docker build task

Why does the Google Cloud Build configuration file not use an entrypoint for docker build step(s)?

docker build with --build-arg with multiple arguments

Docker build-arg and copy

Deploying Google Cloud function from a Google Build

Docker build failure from docker file

Dockerfile build ARG in COPY --from=

Use an argument from docker-compose build --build-arg inside a Dockerfile

Is there a way to specify a `--build-arg` with Cloud Run?

How can I build a Docker Image in Google Cloud Build and use in later Build Steps?

Google Cloud Container Builder - Build Docker container from Go source with vendored dependencies

Can't use ARG in docker multistage build

envsubst within docker using build-arg

google cloud build syntax

Docker compose build time args from file

docker multistage build fails with multiple --build-arg

docker build --build-arg loses value and expands to empty string

Nesting parameters in --build-arg of docker build command

In a Docker Build with the build-arg flag,values are not getting passed correctly

In Google Cloud Build, what is the difference between an array of args and a long string as an arg?

Google Cloud Build - How to prune docker images on VM?

Docker build command hangs in Google Cloud Shell without any message

Passing variables from docker build file to docker file

Equivalent of --env-file for build-arg?

No such file or directory in docker build

Docker build no No such file or directory

Google AppEngine ENV variables from Google Cloud Build Dockerfile