Remove slashes from a variable defining an image name

Hello,

I have a gitlab-ci pipeline that creates and publishes a docker image on docker hub named with the current branch name:

  • docker buildx build -t “team/repo:$CI_COMMIT_BRANCH-latest” -f ./Dockerfile --platform linux/amd64 --push --progress=plain .

If the commit branch contains a slash (eg: fix/the_fix), I can use $CI_COMMIT_REF_SLUG instead and that works.

Now I have a job that runs on merge requests and merges the destination and source branches. I want this job to use the latest published $CI_MERGE_REQUEST_SOURCE_BRANCH-latest image.

But if the source branch contains slashes it won’t work and there is no $CI_MERGE_REQUEST_SOURCE_BRANCH_SLUG variable.

I tried substituting in a global variables section:

variables:
 CI_MERGE_REQUEST_SOURCE_BRANCH_NAME_SAFE: ${CI_MERGE_REQUEST_SOURCE_BRANCH_NAME//\//\-}

but this doesn’t work, CI_MERGE_REQUEST_SOURCE_BRANCH_NAME_SAFE is empty.

I also tried specifying the image name as:

image:
 name: team/repo:$(echo $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME | sed -e 's|/|-|g')-latest

it also doesn’t work, the resulting variable is empty.

Do you have any suggestion ?

Thank you for your time.