Search code examples
bashgoogle-cloud-platformgoogle-cloud-build

Bash Indirect expansion doesnt seem to work in cloud build bash scripts?


Bash has indirect expansion with the following format:

echo ${!VARIABLE} 

However it doesnt seem to work in a google cloud build step using the bash entrypoint. I have set the values of _BAR and _FOO_BAR under substitutions: and I also have automapSubstitutions: true so the substitutions are being set as environment variables in bash. Im trying to follow this answer as an example. I have the following step:

- id: Test Indirect Expansion
  name: "gcr.io/cloud-builders/gsutil"
  entrypoint: 'bash'
  args:
    - -c
    - |
      #!/usr/bin/env bash
      echo "Value of substitution _BAR: ${_BAR}"
      echo "Value of substitution _FOO_BAR: ${_FOO_BAR}"
      export foobar=_FOO${_BAR}
      echo "expansion: ${foobar}"
      echo "indirect expansion: ${!foobar}"

However the output for the step shows indirect expansion as blank:

Value of substitution _BAR: _BAR
Value of substitution _FOO_BAR: google cloud
expansion: _FOO_BAR
indirect expansion:

These commands work on my local bash terminal but not here. I cant find anything mentioning indirect substitution for cloud build. But I dont see any reason why it wouldnt support it. Can anyone shed some light on this?


Solution

  • Since this seems to be a rare issue, I figured that bash scripts inside of Cloud build steps dont support this kind of indirect expansion.

    I was hoping to dynamically set variable names using this. Instead I just used if statements inside the bash script to do what I needed to do, in case anyone finds this in the future.