Using bash substitution parameter in gitlab-ci.yml

Hello,

I would like to make some substitution in pre-defined environment variables before running a job (using Bash’s Shell Parameter Expansion feature)

Example:

build_pkg:
  image: myuser/myimage:${CI_COMMIT_TAG%.*}
  script:
   - make

I was not able to make it working and didn’t find anything in docs related to that feature.

If this feature doesn’t exist yet, I will find helpul to have it.

Hello @nqb,

I am not sure if changing the the pre-defined environment is feasible but maybe it’s worth looking to changing it in .pre stage with a bash script/command since all the changes in .pre stage are available to every other pipeline.

I hope that helps.
Rotanak

Hello @rotanak,

I am not sure if changing the the pre-defined environment is feasible

Already tried, it’s not possible and that’s make sense.

My idea is not to modify them, just play with their values to fit my needs:

$ CI_COMMIT_TAG=v0.0.1
$ echo ${CI_COMMIT_TAG%.*}
v0.0
$ echo $CI_COMMIT_TAG
v0.0.1

In fact, I want a feature that let me use modified values of pre-defined environments variables to define jobs parameters without having to “eval” the variables. Because evaluate variables can only occur when job has started (see example in documentation).

That really simplify jobs definition and avoid DRY.

Regarding .pre stage, I don’t think you can evaluate a variable in this stage and export it to other stages.

@nqb,

In that case, would you mind sharing your thought?
what your goal is in term of modifying their values?
It would allow us to have a better overview and provide answer that tailor to your need.

Thanks,
Rotanak

Digging this thread out of the past while trying to fix a similar problem. This is now quite easy to achieve since gitlab 13.x thanks to variable inheritance. Note that as declared in the documentation, those vars can only be used in scripts inside a stage and not to control the pipeline itself.

Reference: GitLab CI/CD variables | GitLab

Here is a simple example calculating a docker image name replacing chars in the reference branch. I took advantage of the .pre stage which will run prior to any other

---
stages:
  - build
 
image: debian:latest

variables:
  DOCKER_REGISTRY: my.private.repo
  DOCKER_NAMESPACE: /my_namespace
  DOCKER_IMAGE_SPACE: "${DOCKER_REGISTRY}${DOCKER_NAMESPACE}"

setup:
  stage: .pre
  script:
    - echo "DOCKER_IMAGE=${DOCKER_IMAGE_SPACE}/${CI_PROJECT_NAME}:${CI_COMMIT_REF_SLUG/${CI_DEFAULT_BRANCH}/latest}" >> build.env
  artifacts:
    reports:
      dotenv: build.env

my_test:
  stage: build
  script:
    - echo "We are on branch ${CI_COMMIT_BRANCH}"
    - echo "We are building ${DOCKER_IMAGE}"

Here is the relevant part of the output of the build job running against the default test project branch

$ echo "We are on branch ${CI_COMMIT_BRANCH}"
We are on branch master
$ echo "We are building ${DOCKER_IMAGE}"
We are building my.private.repo/my_namespace/test:latest