Hello, I’m trying to set up my pre_build hook in Docker so that if a build is triggered by a release branch, certain dependencies are updated. Our release branches follow a pretty standard release/<release_name> pattern, however the following conditional is not being triggered by our release branches:
if [[ "$SOURCE_BRANCH" = "develop" ]]; then do something elif [[ "$SOURCE_BRANCH" == "release/.*" ]]; then do something fi
Based on the sed commands I’ve used in Docker hooks previously, i’m not sure why this regex wouldn’t work. Any ideas?
Thanks for the pointer, the second conditional worked. The check for develop has been working for a while now, is there a case in which it wouldn’t? And yeah I guess I’m not as familiar with this stuff as I’d thought. I tested on my machine by running echo release/<release name> | grep release/.*, and it matched the sample branch names. Any idea why it would work in a bash shell but not on Docker Hub?
That’s easy: the way you test for the condition is simply different. While you used grep’s pattern matching, the example I provided uses bash’s buildin regex matching for conditions.
This is not related to beeing executed in the bash shell or in a bash script, this is related to using the right "tool " for the right situation.
Create a bsh script with following content and save it as test.sh:
#!/bin/bash
if [[ "$SOURCE_BRANCH" == "develop" ]];then
echo "develop: $SOURCE_BRANCH"
elif [[ "$SOURCE_BRANCH" =~ release/.* ]];then
echo "release: $SOURCE_BRANCH"
fi
Then test it yourself:
test1: export SOURCE_BRANCH=develop && bash test.sh (I use bash to execute test.sh to skip “chmod +x test.sh”)
test2: export SOURCE_BRANCH=release/foo && bash test.sh
This approach does work on Dockerhub automated builds - I am using a similar snippets in pre_hooks myself.