带有 scriptPath 选项的 Azure DevOps AzureCLI 任务无法通过身份验证来创建 Azure 容器实例

Azure DevOps AzureCLI task with scriptPath option failed to authenticate to create Azure Container Instance

我们在 Azure DevOps 中使用 YAML 管道。

管道中的任务之一是 AzureCLI,它创建 ACI container
我们在之前的流水线步骤中构建并推送的镜像。

当我们从外部脚本 运行 这个任务时,它失败并显示消息:
The image '***.azurecr.io/image:1.0' in container group '***' is not accessible. Please check the image and registry credential.

当我们将脚本类型更改为 inlineScript 时,它可以正常工作。

以下表格无效(scriptPath)。

- task: AzureCLI@1
  displayName: 'Run tests'
  inputs:
    azureSubscription: $(AZURE_SUBSCRIPTION)
    scriptType: 'bash'
    scriptLocation: 'scriptPath'
    scriptPath: 'run_tests.sh'

这是有效的形式(inlineScript)

- task: AzureCLI@1
    displayName: 'Run tests'
    inputs:
      azureSubscription: '$(AZURE_SUBSCRIPTION)'
      scriptLocation: 'inlineScript'
      inlineScript: 'az container create -g *** --registry-login-server ***.azurecr.io --registry-username *** --registry-password *** --image image -n test --cpu 1 --memory 8 --restart-policy Never --command-line "dotnet test.dll -n testApp -c 1000"'

脚本将超过一行。 关于如何从外部脚本文件 (scriptLocation: 'scriptPath') 创建 azure 容器实例的任何线索?

我以两种方式(scriptinline)创建了容器实例。这是我的配置:

构建定义:


steps:
- task: replacetokens@3
  inputs:
    targetFiles: 'Whosebug/12-container-instance/create-container-instance.sh'
    encoding: 'auto'
    writeBOM: true
    actionOnMissing: 'warn'
    keepToken: false
    tokenPrefix: '#{'
    tokenSuffix: '}#'
    useLegacyPattern: false
    enableTelemetry: true
- task: AzureCLI@2
  displayName: 'Run tests 1'
  inputs:
    azureSubscription: '$(AzureSubscription)'
    scriptType: 'bash'
    scriptLocation: 'scriptPath'
    scriptPath: 'Whosebug/12-container-instance/create-container-instance.sh'

- task: AzureCLI@2
  displayName: 'Run tests 2'
  inputs:
    azureSubscription: '$(AzureSubscription)'
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: 'az container create -g TheCodeManual --name myapp2 --image $(Image) --registry-password $(RegistryPassword) --registry-user $(RegistryUser)'

create-container-instance.sh文件

az container create -g TheCodeManual --name myapp1 --image #{Image}# --registry-password #{RegistryPassword}# --registry-user #{RegistryUser}#

我使用令牌替换任务不暴露源代码管理中的敏感数据。

然而,这两种方式都适合我。

我注意到我没有使用 --registry-login-server ***.azurecr.io,而是我提供了图像的完整路径,如下所示:

az container create -g MyResourceGroup --name myapp --image myAcrRegistry.azurecr.io/myimage:latest --registry-password password --registry-user userName