Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional variables in gitlab-ci.yml

Depending on branch the build comes from I need to use slightly different command line arguments. Particularly I would like to upload snapshot nexus artifacts when building from a branch, and release artifact when building off master.

Is there a way to conditionally alter variables?

I tried to use except/only keywords like this

stages:
    - stage


variables:
    TYPE: Release


.upload_common:
    stage: stage
    tags: ["Win"]
    script: 
        - echo Uploading %TYPE%


.upload_snapshot:
    variables:
        TYPE: "Snapshot"
    except:
        - master


upload:
    extends: 
        - .upload_common
        - .upload_snapshot

Unfortunately it skips whole upload step when building off master.

The reason I am using 'extends' pattern here is that I have win and mac platforms which use slightly different variables substitution syntax ($ vs %). I also have a few different build configuration - Debug/Release, 32bit/64bit.

The code below actually works, but I had to duplicate steps for release and snapshot, one is enabled at a time.

stages:
    - stage


.upload_common:
    stage: stage
    tags: ["Win"]
    script: 
        - echo Uploading %TYPE%


.upload_snapshot:
    variables:
        TYPE: "Snapshot"
    except:
        - master

.upload_release:
    variables:
        TYPE: "Release"
    only:
        - master


upload_release:
    extends: 
        - .upload_common
        - .upload_release


upload_snapshot:
    extends: 
        - .upload_common
        - .upload_snapshot

The code gets much larger when snapshot/release configuration is multiplied by Debug/Release, Mac/Win, and 32/64bits. I would like to keep number of configurations at minimum.

Having ability to conditionally altering just a few variables would help me reducing this code a lot.

like image 318
Oleksandr Masliuchenko Avatar asked Aug 13 '19 12:08

Oleksandr Masliuchenko


People also ask

How do you declare variables in Gitlab?

In GitLab, CI/CD variables can be defined by going to Settings » CI/CD » Variables, or by simply defining them in the . gitlab-ci. yml file. Variables are useful for configuring third-party services for different environments, such as testing , staging , production , etc.

How do I inherit a group variable in Gitlab?

If you have GitLab 9.4+, you can set up group-level environment variables like this: Navigate to your Group. Go to Settings > CI/CD > Variables . Group variables will be inherited by the group's projects and sub-groups.

What is CI_API_V4_URL?

CI_API_V4_URL is an environment variable that is set by GitLab only when you use GitLab CI. Those serve different purpose. All reactions. pvdlg closed this as completed on Aug 13, 2019. Author.


1 Answers

Another addition in GitLab 13.7 are the rules:variables. This allows some logic in setting vars:

job:
  variables:
    DEPLOY_VARIABLE: "default-deploy"
  rules:
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
      variables:                              # Override DEPLOY_VARIABLE defined
        DEPLOY_VARIABLE: "deploy-production"  # at the job level.
    - if: $CI_COMMIT_REF_NAME =~ /feature/
      variables:
        IS_A_FEATURE: "true"                  # Define a new variable.
  script:
    - echo "Run script with $DEPLOY_VARIABLE as an argument"
    - echo "Run another script if $IS_A_FEATURE exists"
like image 75
Robert Cutajar Avatar answered Sep 28 '22 02:09

Robert Cutajar