Solution for AWS CodeBuild, buildspec.yml “bad substitution” error
is Given Below:
I’m using AWS CodeBuild, and I need to manipulate an environment variable. I originally tried using a bash pattern substitution, like this, in the buildspec.yml
:
build:
on-failure: ABORT
commands:
- env="${CODEBUILD_WEBHOOK_TRIGGER/tag//}"
CODEBUILD_WEBHOOK_TRIGGER
should be something like tag/my-tag-name
, and I want to remove the tag/
part of it. This command works fine from a local bash shell, but when performned in CodeBuild, this is the output:
[Container] 2021/08/02 21:29:28 Running command env="${CODEBUILD_WEBHOOK_TRIGGER/tag//}"
/codebuild/output/tmp/script.sh: 4: Bad substitution
...
[Container] 2021/08/02 21:29:28 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: env="${CODEBUILD_WEBHOOK_TRIGGER/tag//}". Reason: exit status 2
I ended up replacing the pattern substitution with an awk
command just to get it working, but it makes for more complex code. And I don’t understand why the pattern substitution doesn’t work?
Here’s the awk command I ended up using, which is working fine:
build:
on-failure: ABORT
commands:
- env="`echo $CODEBUILD_WEBHOOK_TRIGGER | awk -F/ '$1=="tag" {print $2;}'`"
CodeBuild might not be using bash. Try this :
env="${CODEBUILD_WEBHOOK_TRIGGER#tag/}"