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
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 inkey=value
format.--secure-environment-variables
: A list of secure environment variables for the container. Space-separated values inkey=value
format.Example – running the CLI in a Bash shell or Azure Cloud Shell:
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:Please refer to the command’s documentation for more details.
The syntax below is used to DevOps pipeline yaml not Azure container deployment.yaml.
To set
ConnectionStrings__DefaultDbConnection
in deployment.yaml fordifferent 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:
The variable group:
The DevOps yaml sample:
For each stage, it will have
different value
forconnectionstring
for deployment.