I'm trying to enable Review Apps for my project with automatic deploy to Heroku for branches. Each deployment should have the following address:
https://prefix-<branch-name>.herokuapp.com
Heroku requires app names to be no longer than 30 characters, so my jobs should shorten the branch name, if it's too long.
I've tried to do this in a common Unix way, like this:
variables:
REVIEW_APP_NAME: "prefix-${CI_COMMIT_REF_SLUG:0:23}"
But it resolved to "prefix-".
I have also found the following solution, which allowed me to use the shortened branch name in the script section. But it still can't be used in environment url parameter. And this leads to app being deployed to Heroku, but doesn't tracked by GitLab at all (no deployment in the Environments list and no action on branch deletion).
variables:
REVIEW_APP_NAME: "prefix-$${CI_COMMIT_REF_SLUG:0:23}"
before_script:
- eval export REVIEW_APP_NAME=${REVIEW_APP_NAME}
Are there any other ways to achieve proper behavior?
We use a simple bash script for that purpose:
#!/bin/bash
set -e
out=${1:0:40}
if [[ $out =~ ^.*-$ ]]; then
out=${out:0:-1}
fi
echo $out
The if statement checks that the string does not end with a trailing dash.
We then use our script in the CI like this:
[...]
script:
- export CI_COMMIT_REF_NAME=$(./infrastructure/utils/shorten_branchname.sh $CI_COMMIT_REF_NAME)
[...]
For images that use docker or alpine you need to install bash before:
[...]
before_script:
- apk add --update bash
[...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With