3

I have a simple request but can't find any sample code for it.

Suppose I want to set an environment variable depending on the branch name. Something like this (though I know this code doesn't work)

variables:
  rules:
    - if: '$CI_COMMIT_BRANCH != "master"'
      variables:
        env: "dev"
    - if: '$CI_COMMIT_BRANCH == "master"'
      variables:
        env: "prod"
  stackName: projectA-${env}

So the stackName or other variables can use ${env} as suffix in the name and I can also use ${env} in jobs , scripts or stages

How can I set it?

4 Answers 4

2

The variable env or stackName can be set in two different job, which run with a rule according the branch. And you can define a template job, to make same thing for both environment. For example, define two jobs like this with a template :

.template-job:
  script:
    - echo $STACK_NAME

prod-job:
  variables:
    STACK_NAME: projectA-prod
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
  extends: .template-job

dev-job:
  variables:
    STACK_NAME: projectA-dev
  rules:
    - if: '$CI_COMMIT_BRANCH != "master"'
  extends: .template-job

Using this, you have two jobs but they extends the same template-job, which is using the variable $STACK_NAME. You can of course name it ENV or whatever.

4
  • 2
    thanks. But suppose, not only one job need this variable to be set, but 10+ jobs, and not only one variable, but 10+ variables. do i have to add them in each job? my purpose, on top, set all variables, but for different enviroments.
    – Bill
    Commented May 31, 2022 at 13:14
  • and what's the purpose of "rules` in your codes, remove them, has no difference.
    – Bill
    Commented May 31, 2022 at 13:22
  • Rules are here to define when the job should run, so what value should take the variable. This is a way to set all variables for different environment according the branch name, so yes, you have to duplicate your variables between all jobs (environments) and then you can use them. Commented May 31, 2022 at 13:35
  • 1
    @Bill note the extends. You can have common parts of the workflow defined in a template. Also you can define multiple variables within each variables block here. But yes, defining lots of conditional build steps gets a little redundant. But this is the answer. Commented May 31, 2022 at 14:47
0

You can use it in this way.

  environment:
    name: your-env-dev
  rules:
    - if: '$CI_COMMIT_REF_NAME == "master" && "your-env-dev" =~ $YOUR_INSTANCES'

Set the variable value in settings under CI/CD

1
  • I can understand the usage of environment, but not familar the usage of rules.can you share details on it?
    – Bill
    Commented Jun 2, 2022 at 7:03
0

Answer my question after about 1 year. Here is my solution, and a simple solution.

I refered the idea from @Time recommended.

gitlab pipeline's environments are similar concept of CircleCI's context , if you are familar it.

  • create environments first, such as develop, staging, production, etc
  • create variables but assigned to different environments

enter image description here

  • you can set the variables with same names, but assigned to different environments.

When use it, you can add below codes to each task

staging_build:
  stage: build
  environment:
    name: staging
  script:
    - echo ${AWS_ACCESS_KEY_ID}
  only:
    - staging

production_build:
  stage: build
  environment:
    name: production
  script:
    - echo ${AWS_ACCESS_KEY_ID}
  only:
    - main

0

You can do likely this in the workflow section.

variables: 
  stackName: projectA-${env}

workflow:
  rules:
   - if: '$CI_COMMIT_BRANCH == "master"'
     variables:
       env: prod
   - when: always # only evaluates this if the above was false
     variables:
       env: dev

For long and complicated sets, I'd put them in separate files and include them.

include: branch_config/$CI_COMMIT_BRANCH.yaml

Not the answer you're looking for? Browse other questions tagged or ask your own question.