Do you have guidance on how we should use variables and environments together? For example, let’s say one of my deployment steps is to run some database scripts, and in order to do that I need to pass a connection string into the YAML pipeline. Different environments will have different connection strings. What’s the recommended way to do this? As far as I can see, we can’t scope variables to environments currently.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
Thanks for your question. It looks like you're working through an issue with your scenario or implementation, rather than an issue with the documentation. Here are a couple of options where you might consider asking your question:
Hi @WilliamAntonRohm, I would argue that this is an issue with the documentation. Having environment-specific variables in releases is an extremely common thing to do, and there's nothing in the documentation to advise how to do this. It's also possible that this feature is coming soon and isn't there yet, but I think it's a reasonable question to ask about the documentation.
@johndowns -- thank you for replying; you make a good point. I've re-opened this issue and notified the article's author.
Agreed. It would be quite helpful to have documentation on how to achieve this. Or guidance as to what the recommended approach should be.
variables are set at pipeline scope or at stage, job scope. In the case of environment, they are scoped to a specific deployment job. Hence variables at job scope will be equivalent to setting variables at environment scope.
We do have plans to support linking variables/key vaults to an environment (no time lines). This would be an alternate option to handle environment scoped variables.
@clancy (and anyone else who is interested) - I did a little more investigation into this and have found an approach that hopefully is workable. It doesn't use environments at all.
Here's an example that shows all of the above:
Parent template:
trigger:
- master
pr: none
pool:
vmImage: 'ubuntu-latest'
variables:
MySharedVariable: xyz
stages:
- stage: build
jobs:
- job: run_build
pool:
vmImage: 'Ubuntu 16.04'
steps:
- checkout: self
- script: echo Build
- stage: deploy_dev
dependsOn: build
jobs:
- job: deploy_dev
variables:
- group: MySecrets-Dev
- name: EnvironmentName
value: Dev
steps:
- template: pipeline-deploy.yml
- stage: deploy_test
dependsOn: deploy_dev
jobs:
- job: deploy_test
variables:
- group: MySecrets-Test
- name: EnvironmentName
value: Test
steps:
- template: pipeline-deploy.yml
Child template (pipeline-deploy.yml):
steps:
- script: echo Deploying to $(EnvironmentName) with secret $(ExternalApiKey)
And I have two variable groups set up:
MySecrets-DevExternalApiKey = xxx (and configured as a secret)MySecrets-TestExternalApiKey = yyy (and configured as a secret)
Most helpful comment
@clancy (and anyone else who is interested) - I did a little more investigation into this and have found an approach that hopefully is workable. It doesn't use environments at all.
Here's an example that shows all of the above:
Parent template:
Child template (
pipeline-deploy.yml):And I have two variable groups set up:
MySecrets-DevExternalApiKey= xxx (and configured as a secret)MySecrets-TestExternalApiKey= yyy (and configured as a secret)