GoogleCloudPlatform/cloud-build-local

Variable interpolation in options:env: not working

ryanchapman opened this issue · 2 comments

Hi,

I'm trying to specify environment variables for the whole build instead of for each build step, like so:

steps:
- id: 'echo_env_vars'
  name: 'gcr.io/cloud-builders/git'
  entrypoint: '/bin/bash'
  args: ["-c", "env"]

- id: 'build_docker_image'
  name: 'gcr.io/cloud-builders/docker'
  waitFor: ['build_number']
  entrypoint: '/bin/bash'
  args: ["-c", "./make.bash all ${REPO_NAME}"]

options:
 env:
  - 'BUILD_ID=$BUILD_ID'
  - 'BRANCH_NAME=$BRANCH_NAME'
  - 'TAG_NAME=$TAG_NAME'
  - 'REPO_NAME=$REPO_NAME'
  - 'REVISION_ID=$REVISION_ID'
  - 'PROJECT_ID=$PROJECT_ID'
  - 'SHORT_SHA=$SHORT_SHA'

But variables are not interpolated, so in the 'echo_env_vars' step, my environment looks like this:

Already have image (with digest): gcr.io/cloud-builders/git
HOSTNAME=a3319a25f68f
BUILD_ID=$BUILD_ID
BRANCH_NAME=$BRANCH_NAME
SHORT_SHA=$SHORT_SHA
PATH=/builder/google-cloud-sdk/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/builder/google-cloud-sdk/bin/
PWD=/workspace
PROJECT_ID=$PROJECT_ID
REPO_NAME=$REPO_NAME
SHLVL=1
HOME=/builder/home
REVISION_ID=$REVISION_ID
DEBIAN_FRONTEND=noninteractive
BUILDER_OUTPUT=/builder/outputs
TAG_NAME=$TAG_NAME
_=/usr/bin/env

If I put the vars in for each build step, like so:

steps:
- id: 'echo_env_vars'
  name: 'gcr.io/cloud-builders/git'
  entrypoint: '/bin/bash'
  args: ["-c", "env"]
  env:
    - 'BUILD_ID=$BUILD_ID'
    - 'BRANCH_NAME=$BRANCH_NAME'
    - 'TAG_NAME=$TAG_NAME'
    - 'REPO_NAME=$REPO_NAME'
    - 'REVISION_ID=$REVISION_ID'
    - 'PROJECT_ID=$PROJECT_ID'
    - 'SHORT_SHA=$SHORT_SHA'

- id: 'build_docker_image'
  name: 'gcr.io/cloud-builders/docker'
  waitFor: ['build_number']
  entrypoint: '/bin/bash'
  args: ["-c", "./make.bash all ${REPO_NAME}"]
  env:
    - 'BUILD_ID=$BUILD_ID'
    - 'BRANCH_NAME=$BRANCH_NAME'
    - 'TAG_NAME=$TAG_NAME'
    - 'REPO_NAME=$REPO_NAME'
    - 'REVISION_ID=$REVISION_ID'
    - 'PROJECT_ID=$PROJECT_ID'
    - 'SHORT_SHA=$SHORT_SHA'

Then the variables are interpolated.

Already have image (with digest): gcr.io/cloud-builders/git
HOSTNAME=99ca06779b08
BRANCH_NAME=ryan/push-on-merge2
SHORT_SHA=341bb94
PATH=/builder/google-cloud-sdk/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/builder/google-cloud-sdk/bin/
PWD=/workspace
PROJECT_ID=ci
REPO_NAME=assetserver
BUILD_ID=bd63276b-2201-4525-be2c-33aa7f1a87e9
SHLVL=1
HOME=/builder/home
REVISION_ID=341bb944b371fc5602e4d961912939fd876f09f3
DEBIAN_FRONTEND=noninteractive
BUILDER_OUTPUT=/builder/outputs
TAG_NAME=
_=/usr/bin/env

Unfortunately, specifying env variables multiple times in each build step is error prone. I'd like a way to do it once and for it to apply to all steps.

Wanted to add that I reported this internally and I think it was fixed in the cloud version.

The internal bug number for that I'm told was 122901552.

In both the hosted service and local binary, you can now specify global env vars in the build.options. Information is in the REST API documentation: https://cloud.google.com/cloud-build/docs/api/reference/rest/Shared.Types/Build#BuildOptions

I've ping'd appropriate channels to get documentation coverage in our main tutorial pages. Thanks for following up!