how to access content across successive build steps (including in docker steps) in google cloudbuild

578 Views Asked by At

Within Google Cloud Platform, in a multistage cloudbuild process it is possible to use the shared /workspace to access content from one build step to the next. For instance the following cloudbuild.yaml would print "hello world" where the text hello is accessed from the first build step.

steps:
  - name: gcr.io/cloud-builders/gcloud
    entrypoint: 'bash'
    args: [ '-c', " echo world > sample.txt" ]
  - name: gcr.io/cloud-builders/gcloud
    entrypoint: 'bash'
    args: [ '-c', "echo hello $( < sample.txt)" ]

However, it seems this method does not work when it comes to accessing this content in the docker build step. There does not appear to be a way to access files within the docker build step.

The following step, when executed as the third step subsequent to the above steps executes a Dockerfile but doesn't appear to make the sample.txt file available to commands within it.

  # Build the container image
  - name: gcr.io/cloud-builders/docker
    args: ['build',
           '-t', 'repositoryname:latest',
           '.']

I've tried the following:

  • looking for the default directory named workspace within the container, to no avail.
  • creating a custom volume shared across all steps (the custom volume does not appear to be accessible within the Dockerfile scope.

Is it possible in cloudbuild to progressively build content in a series of steps including docker steps? If so, how could this be done?

Update

Actually it appears the files do remain in a workspace environment. My problem was in not being able to use the content of the file in the build command as follows.

  # Build the container image
  - name: gcr.io/cloud-builders/docker
    args: ['build',
           '--build-arg', 'GITHUB_AUTH_TOKEN=$( < sample.txt)',
           '-t', '$_REPO_PATH/$_PROJECT_ID/$_REPO_NAME:$_TAG_NAME',
           '.']

In the first step I am actually using gcloud to get a secret from the secret manager, I then wanted to pass this as an argument to the docker build process. I could not get the above build step to work, and then I read that command substition does not work on the docker argument?

1

There are 1 best solutions below

1
On BEST ANSWER

The problem comes from the args interpretation. Written like this, there isn't any arg evaluation. Use script mode like this

  # Build the container image
  - name: gcr.io/cloud-builders/docker
    entrypoint: "bash"
    args: 
      - "-c"
      - |
          docker build --build-arg GITHUB_AUTH_TOKEN=$( < sample.txt) -t $_REPO_PATH/$_PROJECT_ID/$_REPO_NAME:$_TAG_NAME .