我的“AzureContainerApps@1”正在运行和部署。但每次都会重置环境变量(我理解)。
我还有一个变量组,该组的键/值需要传递到容器中,Node.js 应用程序才能运行。
我下面的代码设置了构建ID,但是变量组有50个值需要使用相同的键/值传入。
有没有一种聪明的方法可以做到这一点,以便在将值添加到变量组时不必编辑 pipeline.yml 文件?
我的任务:
variables:
- group: cms-aeh-poc
task: AzureContainerApps@1
inputs:
azureSubscription: ${{parameters.container_service_resource_manger_connection}}
containerAppName: ${{parameters.container_app_name}}
resourceGroup: ${{parameters.resource_group}}
imageToDeploy: "${{parameters.container_reg_login_server}}/${{parameters.repository}}:$(Build.BuildId)"
environmentVariables: BUILD_ID=$(Build.BuildId)
例如变量组具有以下值:
APP_NAME = 我可爱的应用程序 地区 = 英国 BASE_DIR = /可爱/东西
我需要这些和传入的 BUILD_ID
恐怕没有现成的方法可以读取变量组中的所有变量并将它们传递给AzureContainerApps任务的environmentVariables。
为了满足您的需求,我建议您可以使用PowerShell脚本来运行Rest API:Variablegroups - Get列出变量组中的所有变量名称并将它们添加到字符串列表中。
然后您可以定义 Pipeline 变量来保存变量列表并将其传递给 AzureContainerApps 任务。
例如:
variables:
- group: cms-aeh-poc
steps:
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
$token = "$(PAT)"
$url="https://dev.azure.com/{orgname}/{projectname}/_apis/distributedtask/variablegroups/{variablegroupid}?api-version=7.1-preview.2"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json
$string=""
foreach($variablename in $response.variables.psobject.properties.name)
{
$env = "$variablename=`$($variablename`)"
$string= -join("$string", " ", "$env")
}
echo $string
echo "##vso[task.setvariable variable=variablelist]$string"
- task: AzureContainerApps@1
inputs:
azureSubscription: 'xx'
xxxx
environmentVariables: 'BUILD_ID=$(Build.BuildId) $(variablelist)'