2 Environment variables with same name

I need clarification regarding GitLab environment variables with same name.

There are 4 enviornments - dev, uat, stg and prod.
In Settings → CI/CD → Variables I have defined 2 keys with same name but with different value.
dev, uat and stg has same values. Prod value is different. Example below

key column value column environment column
SOME_SECRET_KEY some value All(default)
SOME_SECRET_KEY some value1 prod

When the application is deployed in prod environment will it take the prod value? I could have easily tested it by deploying to prod but we haven’t got to prod yet so want to make sure before I deploy to prod for the first time.

GitLab Enterprise Edition [v17.5.4-ee]

Hi,

I would not advise you to do this, as it’s not even clear from documentation which value will be picked up in this case (at least what I understood from your description). I would instead suggest you to remap the value in the job that deploys to production.

E.g. in the Settings you may have:
SOME_SECRET_KEY and SOME_SECRET_KEY_PROD.

In your job in .gitlab-ci.yml file, you can do something like:

deploy to prod:
  stage: deploy
  script:
    - $SOME_SECRET_KEY=$SOME_SECRET_KEY_PROD
    - # run your deployment script
1 Like

Thanks for responding with a good solution. Storing secrets in the repo is not allowed in my company. They recommend storing in GitLab UI masked and not visible.

Of course. That is not just in your company, that is (or should be) everywhere. Secrets do not belong in git repo.

Good luck!

2 Likes