Bash has indirect expansion with the following format:
echo ${!VARIABLE}
However it doesn't 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. I'm 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 don't see any reason why it wouldn't support it. Can anyone shed some light on this?
Since this seems to be a rare issue, I figured that bash scripts inside of Cloud build steps don't 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.