skip to Main Content

I want to set Azure Container Instance (ACI) custom environment variables for a .net 8 application using release pipeline YAML and AzDevOps group variable.

I want to link the respective AzDevops variable group for DEV, QA, PROD to the respective AzDevops Release pipeline stages as the same variables may have different values for DEV/QA/PROD. The idea is for Container app runtime override the values of appsettings.json from environmentVariables if defined.

Microsoft link example here only uses hardcoded values, that works but I need to use variables.

For example appsettings.json:

"ConnectionString":
{
 "DefaultConnection":""
}

YAML would be

environmentVariables:
    -name: "ConnectionString__DefaultConnection"
     value: $(ConnectionString.DefaultConnection) #$(ConnectionString_DefaultConnection)

When I link the AzDevops variable group to release pipeline stage using the dot notation like ConnectionString.DefaultConnection the release pipeline log shows the proper values and prints it as [ConnectionString_DefaultConnection] but for some reason the value is not recognized when I put it in YAML. Am I doing the value variable syntax wrong?

Do I need to add variable group like so in the deployment.yaml?

variables:
- group: ${{ if eq(parameters.environment, 'dev') }}:
    - dev-variables
  ${{ if eq(parameters.environment, 'qa') }}:
    - qa-variables

The deployment.yaml file, pls note the dot in variable name

apiVersion: '2021-10-01'
location: northcentralus
name: aci-cc-dev
type: Microsoft.ContainerInstance/containerGroups
properties:
  imageRegistryCredentials: # Credentials to pull a private image
  - server: myacrname.azurecr.io
    username: myusername
    password: ###ACR_PASSWORD###
  osType: Linux
  subnetIds:
   - id: "/subscriptions/1111111111111111111/resourceGroups/rg-prod-network-001/providers/Microsoft.Network/virtualNetworks/vnet/subnets/apps"
     name: apps
  ipAddress:
    type: Private
    ports:
    - protocol: tcp
      port: '8080'
  restartPolicy: Always
  containers:
    - name: myaci-api
      properties:
        image: myacrname.azurecr.io/mycontainerapp-api:###BUILD_ID###
        environmentVariables:
          - name: PORT
            value: '8080'
          #- name: ConnectionStrings__DefaultDbConnection
          #  value: $(CONNECTIONSTRINGS_DEFAULTDBCONNECTION)
          - name: ConnectionStrings__DefaultDbConnection
            value: $(ConnectionStrings.DefaultDbConnection)
        ports:
          - port: 8080
        resources:
          requests:
            cpu: 1.0
            memoryInGB: 2.0
    

2

Answers


  1. One way to dynamically set environment variables when using az container create command in an Azure DevOps pipeline is to use the following options:

    • --environment-variables: A list of environment variables for the container. Space-separated values in key=value format.
    • --secure-environment-variables: A list of secure environment variables for the container. Space-separated values in key=value format.

    Example – running the CLI in a Bash shell or Azure Cloud Shell:

    az create ... 
    --environment-variables 'PORT'='8080' 
    --secure-environment-variables 'ConnectionStrings__DefaultDbConnection'='$(ConnectionStrings.DefaultDbConnection)'
    

    If you use the Windows Command Prompt, specify the variables with double-quotes, such as --environment-variables "PORT"="8080"

    The above example probably doesn’t work with the --file option, though. You might need to set the whole container configuration such as ports, requests etc using the corresponding options:

    az container create --resource-group
                        [--acr-identity]
                        [--add-capabilities]
                        [--allow-escalation]
                        [--assign-identity]
                        [--azure-file-volume-account-key]
                        [--azure-file-volume-account-name]
                        [--azure-file-volume-mount-path]
                        [--azure-file-volume-share-name]
                        [--cce-policy]
                        [--command-line]
                        [--cpu]
                        [--dns-name-label]
                        [--drop-capabilities]
                        [--environment-variables]
                        [--file]
                        [--gitrepo-dir]
                        [--gitrepo-mount-path]
                        [--gitrepo-revision]
                        [--gitrepo-url]
                        [--image]
                        [--ip-address {Private, Public}]
                        [--location]
                        [--log-analytics-workspace]
                        [--log-analytics-workspace-key]
                        [--memory]
                        [--name]
                        [--no-wait]
                        [--os-type {Linux, Windows}]
                        [--ports]
                        [--priority]
                        [--privileged]
                        [--protocol {TCP, UDP}]
                        [--registry-login-server]
                        [--registry-password]
                        [--registry-username]
                        [--restart-policy {Always, Never, OnFailure}]
                        [--role]
                        [--run-as-group]
                        [--run-as-user]
                        [--scope]
                        [--seccomp-profile]
                        [--secrets]
                        [--secrets-mount-path]
                        [--secure-environment-variables]
                        [--sku]
                        [--subnet]
                        [--subnet-address-prefix]
                        [--vnet]
                        [--vnet-address-prefix]
                        [--vnet-name]
                        [--zone]
    

    Please refer to the command’s documentation for more details.

    Login or Signup to reply.
  2. The syntax below is used to DevOps pipeline yaml not Azure container deployment.yaml.

    variables:
    - group: ${{ if eq(parameters.environment, 'dev') }}:
        - dev-variables
      ${{ if eq(parameters.environment, 'qa') }}:
        - qa-variables
    

    To set ConnectionStrings__DefaultDbConnection in deployment.yaml for different pipeline stage, you can use a placeholder in deployment.yaml, and use DevOps task replacetokens@6 to replace it with target value.

    The deployment.yaml sample:

    enter image description here

    The variable group:

    enter image description here

    The DevOps yaml sample:

    trigger: none
    
    pool:
      vmImage: ubuntu-latest
    
    stages:
      - stage: dev
        variables:
        - group: dev-variables
        jobs:
         - job: job1
           steps:
             - task: replacetokens@6
               inputs:
                 sources: '**/deployment.yaml'
            
             - script: cat deployment.yaml  
             # add you deployment task...
      
      - stage: prod
        variables:
        - group: prod-variables
        jobs:
         - job: job2
           steps:
             - task: replacetokens@6
               inputs:
                 sources: '**/deployment.yaml'
            
             - script: cat deployment.yaml
             # add you deployment task...
    

    For each stage, it will have different value for connectionstring for deployment.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search