Search code examples
azure-devopsenvironment-variablesdotnet-aspire

Add a value from an Azure DevOps Pipeline Library variable to an Aspire .NET Container's Env Var


There is an Azure DevOps Pipeline that has variables set in a Library. There is a Library group per environment, for example, 'dev' and 'uat'. The pipeline uses YAML to create the resources for an Aspire .NET application, an Azure Container App, using Bicep templates.

Say there's a variable named email_address in the library group. This needs to be read into a container's env vars.

Firstly, it needs to be set in the pipeline as an env var:

          - task: AzureCLI@2
            displayName: Deploy Application
            inputs:
              azureSubscription: $(azureSubscription)
              scriptType: bash
              scriptLocation: inlineScript
              keepAzSessionActive: true
              inlineScript: |
                azd deploy --no-prompt
            env:
              EMAIL_ADDRESS: $(email_address)

So far, so good. The env var is created. Then there's a section in the "webfrontend.tmpl.yaml" doing this for the container:

  template:
    containers:
      - image: {{ .Image }}
        name: webfrontend
        env:
          - name: email-address
            value: {{ .Env.EMAIL_ADDRESS }}

I can't work out the syntax for getting the library variable through the pipeline yaml and into the container's env var.

ERROR: failed deploying service 'webfrontend': failed executing template file: template: manifest template:34:26: executing "manifest template" at <.Env.EMAIL_ADDRESS>: map has no entry for key "EMAIL_ADDRESS"

How do you do this?


Solution

  • You can use extension Replace Tokens to replace tokens in text based files with variable values.

    For example,

    In your webfrontend.tmpl.yaml file:

      template:
        containers:
          - image: {{ .Image }}
            name: webfrontend
            env:
              - name: email-address
                value: {{ EMAIL_ADDRESS }}
    

    Add a replacetokens@6 task before azd deploy in your pipeline YAML file:

    - task: replacetokens@6
      inputs:
        sources: '{Path to your webfrontend.tmpl.yaml file}'
        tokenPattern: 'doublebraces'
    

    In your variable group, add a variable named EMAIL_ADDRESS, then the value of email-address will be replaced with the value of variable EMAIL_ADDRESS.