Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change Gitlab CI variable value after pipeline has started?

I'm trying to create a dynamic gitlab pipeline based on it's own execution progress. For example I have 2 environments and deployment to each of them will be enabled/disabled based on the execution of the script in before_script. It doesn't work for me, seems that pipeline variable value can't be changed after pipeline has started. Any suggestions? (please see my gitlab-ci.yml below)

variables:
  RELEASE: limited

stages:
  - build
  - deploy


before_script:
  - export RELEASE=${check-release-type-dynamically.sh}

build1:
  stage: build
  script:
    - echo "Do your build here"

## DEPLOYMENT
deploy_production_ga:
  stage: update_prod_env
  script:
  - echo "deploy environment for all customers"
  allow_failure: false
  only:
  - branches
  only:
   variables:
   - $RELEASE == "general_availability"


deploy_production_limited:
  stage: update_prod_env
  script:
  - echo "deploy environment for limited customers"
  allow_failure: false
  only:
  - branches
  only:
   variables:
   - $RELEASE == "limited"
like image 936
Roman Dolgoter Avatar asked Jun 13 '18 17:06

Roman Dolgoter


People also ask

Is it possible to override GitLab CI default variables?

gitlab-ci. yml file. The variables are used by the runner any time the pipeline runs. You can also override variable values manually for a specific pipeline.

How do I set environment variables in GitLab CI?

Go to Settings > CI/CD. Click Expand in the Variables section. Select the State and Masked values you want for your variable. Click Save variables.

How do I set 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.


1 Answers

Variables can't be evaluated in the definition. If you really want to use a shell script to decide what get's deployed you could use an bash if clause:

stages:
  - build
  - update_prod_env

build1:
  stage: build
  script:
    - echo "Do your build here"

deploy_production_ga:
  stage: update_prod_env
  script:
  - if [ "$(./check-release-type-dynamically.sh)" == "general_availability" ]; then
      echo "deploy environment for all customers"
    fi
  only:
  - branches    

deploy_production_limited:
  stage: update_prod_env
  script:
  - if [ "$(./check-release-type-dynamically.sh)" == "limited" ]; then
      echo "deploy environment for all customers"
    fi
  only:
  - branches    

However this is really bad design. Both jobs will get executed on every commit, but only one will do something. It is better to distinguish them by branch. Only commit things to the branch you want to deploy to:

stages:
  - build
  - update_prod_env

build1:
  stage: build
  script:
    - echo "Do your build here"

deploy_production_ga:
  stage: update_prod_env
  script:
  - echo "deploy environment for all customers"
  only:
  - branches-general_availability    

deploy_production_limited:
  stage: update_prod_env
  script:
  - echo "deploy environment for all customers"
  only:
  - branches-limited

This way only the build job gets executed that you want to be executed.

A couple of other things I noticed:

export RELEASE=${check-release-type-dynamically.sh} use () instead of {} for subshells. Also if the shell script is in the same directory you must prepend an ./. It should look like: export RELEASE=$(./check-release-type-dynamically.sh)

allow_failure: false this is the default in gitlab-ci and not necessary.

variables:
- $RELEASE == "general_availability"

Wrong syntax for variables, use:

variables:
  VARIABLE_NAME: "Value of Variable"

Have a look at https://docs.gitlab.com/ee/ci/yaml/

like image 89
mles Avatar answered Oct 03 '22 23:10

mles