How to use different variable values specific to GITLAB branch
Asked Answered
F

2

7

I`m trying to migrate our application code from Jenkins to Gitlab. Here, I would like to play around variables specific to branch. For ex: 1) Master branch code will be deployed to production 2) Other branch code is eligible to deploy in Non-Prod environments.

Where in Jenkins, the variables are set according to the different pipeline they have created. How can I do the same in by using the gitlab variables features.

I would need something like this.

AWS_KEY variable for master branch should be set as "XXX", where in AWS_KEY variable for non-master branch should be set as "YYY". I would like to configure the same in gitlab UI itself (so that I can mask the values in the GITLAB).

variables:
 aws_credentials: $AWS_KEY

The above code snippet will be used in the gitlab-ci.yml file of my repository. AWS_KEY value should change according to the branch name.

Foreskin answered 12/11, 2019 at 10:15 Comment(1)
There's no such functionality at the moment. See why and how to overcome this.Varve
S
2

Can be done like this:

stages:
  - deploy

# use the variable assigned for a branch
.test-job:
  stage: deploy
  script:
    - echo "${SOME_VAR}"

# assign variables depending on the branch
test-branch-job:
  variables:
    SOME_VAR: "fb_branch_name variable"
  rules:
    - if: $CI_COMMIT_BRANCH == "fb_branch_name"
  extends: .test-job
Superscription answered 16/11, 2022 at 20:47 Comment(0)
F
1

There is a different hack which can be used in gitlab. Have env/branch specific variables in CI/CD vars. For an example - If the branch is dev and prd, have the variables dev_aws_credentials and prd_aws_credentials respectively configured in the gitlab.

The job definition pointing to dev branch can export the dev_aws_credentials to aws_credentials and run the job accordingly.

DEV - Deploy Job:
.variables:
  aws_credentials: ${dev_aws_credentials}

PRD - Deploy Job:
.variables:
  aws_credentials: ${prd_aws_credentials}

This is how the usage of two different variables can be used in different jobs / environment / branch specific as well.

Foreskin answered 28/6, 2021 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.