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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With