Changing variable value based on environment

I am using Gitlab CI along with Git flow to deploy to S3, and I have an AWS user key that access to a dev environment S3 bucket, and I have another AWS user key that has permission to the prod environment S3 bucket. My gitlab_ci.yml file is working for one bucket at a time by reading the keys from the environment variables. However, I’d like for pushes to the “develop” branch to use the AWS user with access to the dev S3 bucket, and pushes to the “master” branch to use the AWS user with access to the prod S3 bucket.

I also have code that uses environment variables during configuration steps, and they are different values for dev vs prod. When I got to Settings -> CI/CD, I can set environment variables, but they do not vary based on the branch.

What is the best way to configure CI so that the values of the environment variables are different when building the develop branch vs the master branch?


One possible approach would be to have two or more environment variables with a _{branchname} suffix i.e. ENVVAR_MASTER and ENVVAR_DEVELOP. You can set ENVVAR in the script section based upon which branch you are building from.