【问题标题】:Cannot create azure pipeline variable using task.setvariable无法使用 task.setvariable 创建 azure 管道变量
【发布时间】:2021-11-22 12:03:53
【问题描述】:

我一直在尝试为我的密钥保管库设置托管服务标识策略,但我无法在我的 yml.xml 中创建所需的变量。根据管道输出,以下代码中的变量“indentity”被填充,但变量 WEB_APP_MANAGED_IDENTITY 没有。因此,最后一个任务“为函数应用添加设置策略”自然会失败。我做错了什么?

parameters:
  serviceConnector: ''
  appResourceGroupName: ''
  appName: ''
  appPlan: ''
  appInsightsName: ''
  location: ''
  planSkuName: ''
  PostgresServerName: ''
  AdminUserName: ''
  AdminPassWord: ''
  pgSkuName: ''
  subscriptionId: ''
  keyVaultName: ''
  devOpsServicePrincipalObjectId: ''
  verkkoGroupServicePrincipalObjectId: ''

stages:

  - stage: 'CREATE_SERVICE_APP'
    displayName: 'Create Osakeekstra service app'
    jobs:
      - job:

        steps:
          - checkout: none

          - task: AzureCLI@2
            displayName: 'Create and configure web app'
            inputs:
              azureSubscription: '$(serviceConnector)'
              scriptLocation: 'inlineScript'
              scriptType: 'bash'
              failOnStandardError: false
              inlineScript: |
                echo ">>>> Create app service plan"
                az appservice plan create \
                  --name $(appPlan) \
                  --resource-group $(appResourceGroupName) \
                  --location $(location) \
                  --sku $(planSkuName) \
                  --is-linux 2>/dev/null

                # create web app
                echo ">>>> Create web app"
                az webapp create \
                  --resource-group $(appResourceGroupName) \
                  --name $(appName) \
                  --runtime "Python|3.7" \
                  --plan $(appPlan) 
                  

                # enable managed identity
                echo ">>>> Enable managed identity"
                identity=`az webapp identity assign \
                  --name $(appName) \
                  --resource-group $(appResourceGroupName) \
                  --query principalId -o tsv`
                  echo "##vso[task.setvariable variable=WEB_APP_MANAGED_IDENTITY]$identity"
                  echo $WEB_APP_MANAGED_IDENTITY
                  echo $identity
                  echo ">>>> Enabled managed identity"
                  
           
          - task: AzureCLI@2
            displayName: 'Create key vault'
            inputs:
              scriptType: bash
              azureSubscription: '$(serviceConnector)'
              scriptLocation: 'inlineScript'
              inlineScript: |
                echo ">>>> Create key vault"
                az keyvault create \
                  --name $(keyVaultName) \
                  --resource-group $(appResourceGroupName) \
                  --location $(location) \
                  --no-self-perms \
                  --sku standard \
                  --enable-soft-delete true \
                  2>&1

                # add set policy for managed service identities
                echo ">>>> Add set policy for function app"
                az keyvault set-policy \
                  --name $(keyVaultName) \
                  --secret-permissions get \
                  --object-id $(FUNCTION_APP_MANAGED_IDENTITY)

              failOnStandardError: true   
                

我把最后一个命令改成了这个

        # enable managed identity
        echo ">>>> Enable managed identity"
        identity=`az webapp identity assign \
          --name $(appName) \
          --resource-group $(appResourceGroupName) \
          --query principalId -o tsv`

        # add set policy for managed service identities
        echo ">>>> Add set policy for function app"
        az keyvault set-policy \
          --name $(keyVaultName) \
          --secret-permissions get \
          --object-id $(indentity)

我收到错误:indentity: command not found 错误:参数--object-id:需要一个参数

【问题讨论】:

    标签: azure-devops azure-web-app-service azure-keyvault


    【解决方案1】:

    我可以看到这里有一些问题。

    首先,如果没有记错,az webapp identity assign 将返回比principalId 更多的值,因此您需要解析结果以获得正确的值。

    其次,要引用identity作为release变量(使用宏语法$(variablename))而不是环境变量,你需要在里面添加;isOutput=true task.setvariable 语句,如 echo "##vso[task.setvariable variable=identity;isOutput=true]$identity" - 所以你将名为 identity 的发布变量设置为名为 identity 的环境变量的值。

    第三,在您在以下任务中引用identity 时,identity 拼写错误为indentity

    第四,这是最不重要的,你在 task.setvariable 命令之后执行 echo $WEB_APP_MANAGED_IDENTITY - 它不可用 - task.setvariable 是 Azure Pipelines 用于抓取控制台输出的“标记”,然后用于为后续任务和作业设置变量。

    您可以将变量设置为##vso[task.setvariable variable=identity;isOutput=true]$identity,稍后将其称为$(identity),或者使用您的原始名称将变量设置为echo "##vso[task.setvariable variable=WEB_APP_MANAGED_IDENTITY;isOutput=true]$identity",然后将其称为$(WEB_APP_MANAGED_IDENTITY)

    【讨论】:

    • 我没有使用上面的说明进行管理,但在将 --object-id $(indentity) 修复为--object-id $身份。感谢您的提示!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 2021-10-25
    • 2023-01-11
    • 2023-02-09
    • 2020-12-08
    相关资源
    最近更新 更多