How to set variable with default value at the pipeline level?

Hello,

I’m trying to set a variable that has the value of another variable or a default value in the ci.

The defaulting mechanism seems to work fine with : ${MY_VAR:-my-value}

But when trying to store the result at the pipeline (or workflow?) level, I can’t acces the new variable :confused:

here’s the smallest way to reproduce: .gitlab-ci.yml · main · Xavier Bouvard / bug-in-ci · GitLab

variables:
  MY_VAR: ${CI_COMMIT_TAG:-0.0.0}

test:
    script:
        - echo ${CI_COMMIT_TAG:-0.0.0}
        - echo ${MY_VAR}

and the corresponding pipeline : test (#2802576383) · Jobs · Xavier Bouvard / bug-in-ci · GitLab

$ echo ${CI_COMMIT_TAG:-0.0.0}
1.0.0
$ echo ${MY_VAR}
Cleaning up project directory and file based variables 00:01
Job succeeded

I might be missing something either in the documentation or solutions from the forum so dont hesitate to send me the fine manual, I’ll read it :slight_smile:

regards

2 Likes

I guess you have a tagged commit so that $CI_COMMIT_TAG is set to 1.0.0.

No it is not possible to set default value on pipeline level. There is the epic about that though.

1 Like

I found a way to create a kind of default variable scenario using rules using different variable names… but I think it would probably work using the same variable name too. This is how I did it:

dotnet_build:
  variables:
    DEFAULT_IMAGE: mcr.microsoft.com/dotnet/core/sdk:latest
  image: $IMAGE_NAME
  script:
    - dotnet build
  rules:
    - if: $IMAGE_OVERRIDE != null
      variables:
        IMAGE_NAME: $IMAGE_OVERRIDE
    - when: always
      variables:
        IMAGE_NAME: $DEFAULT_IMAGE

See discussion on the forum: Override / set default variable value

Thanks for your solution and link to thread (I’ve learn the defualt keyword :slight_smile:)

regarding your snippet, I find the workaround interesting: didn’t thought of using rules for that!

however from my understanding using the default syntax should work: (I haven’t tried it)

dotnet_build:
  image: ${IMAGE_OVERRIDE:-mcr.microsoft.com/dotnet/core/sdk:latest}
  script:
    - dotnet build

I have the same problem:


image: myimage:latest

stages:
  - rpm-build
  - rpm-deploy

variables:
  VERSION: ${CI_COMMIT_TAG:-1.0.0}
  RELEASE: 1.$CI_PIPELINE_ID
  REF: $CI_COMMIT_REF_NAME
  PROJECT: $CI_PROJECT_NAME
  TIMESTAMP: $(date +%s)

my VERSION variable is empty if no tags used