Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab CI/CD: Setup variables relating to environment

I am working with gitlab version 14.10.5.

In the left panel, there is a "Deployments" icone that allow me to create environments. In a gitlab repository I want to set up pipeline for dev, stage and main branches relating to 3 environments.

In these environments, I want to setup specific variables that will chaneg over environments.

For example, I want the variable NINJA to be setup with the value:

  • "1" for the branch dev
  • "2" for the branch stage
  • "3" for the branch main

So, in my .gitlab-ci.yml I have:

build-testing:
  stage: build
  script:
    - echo "Hello team"
    - echo "$NINJA"
  environment:
    name: testing
  only:
    refs:
      - dev


build-staging:
  stage: build
  script:
    - echo "Hello team"
    - echo "$NINJA"
  environment:
    name: staging
  only:
    refs:
      - stage


build-production:
  stage: build
  script:
    - echo "Hello team"
    - echo "$NINJA"
  environment:
    name: production
  only:
    refs:
      - main

How to setup the variable NINJA in an environment, in gitlab ?

like image 639
PicxyB Avatar asked Apr 27 '26 11:04

PicxyB


1 Answers

You can set variable scope, so a variable with same name will return different values in in different envs.

  1. Navigate to Settings -> CI/CD
  2. In Variables section press "Add Variable"
  3. Set key as NINJA
  4. Set "Environment scope" with proper env, i.e testing
  5. Repeat 2,3 and 4 with other envs (staging, production)

See also: https://docs.gitlab.com/ee/ci/environments/#scope-environments-with-specs

like image 68
Andrew Avatar answered Apr 30 '26 09:04

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!