Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab CI - Shorten variable value

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?

like image 201
Alexander Avatar asked Feb 21 '18 20:02

Alexander


1 Answers

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
[...]
like image 137
wjentner Avatar answered Nov 24 '22 20:11

wjentner