Use variables per branch in gitlab

I have set up my GitLab pipeline and I’m using GitLab CI variables to generate my configuration file during the build phase. Now we’ve set up a couple new environments, with each having its own database and other credentials, so I need to generate my configuration file using each environment’s variables based on branch. I’ve already seen:

Some users suggested sourcing environment variables from files but that isn’t really a solution because we want to limit access to these variables. Is there a solution or workaround to this problem in .gitlab-ci.yaml ?