在 azure 任务上设置变量以传递 Azure 管道的另一个任务

Set variable on azure task to pass another task of Azure pipeline

我可以在 powershell 或 bash 脚本上设置变量,并使用 ##vso[task.setvariable variable=abc;]xyz 将变量传递给另一个任务。但是找不到任何文档来设置 azure 任务的变量,例如 azure webapp 部署任务或 SqlAzureDacpacDeployment 任务。我想通过传递变量值来捕获错误。有什么有效的方法可以捕获下一个任务的azure任务错误日志吗?

- task: SqlAzureDacpacDeployment@1
  displayName: 'Insertion SQL Task'
  inputs:
    azureSubscription: 'Org (xxxxx-xxxx-xxxx-xxxx-xxxx)'
    ServerName: 'tcp:abc.database.windows.net'
    DatabaseName: test_db
    SqlUsername: '$(user)'
    SqlPassword: $(pass)
    deployType: SqlTask
    SqlFile: 'SQL/test.sql'
  enabled: true

- task: AzureWebApp@1
  displayName: 'Azure Web App Deploy: $(AppName)'
  inputs:
    azureSubscription: 'Org (xxxxx-xxxx-xxxx-xxxx-xxxx)'
    appType: webApp
    ResourceGroupName: 'Test'
    appName: '$(AppName)'
    package: '$(Build.ArtifactStagingDirectory)\app/*.zip'
    deploymentMethod: zipDeploy
  enabled: true

您需要为任务设置一个名称。

对于Azure SQL Database Deployment task,您可以使用SqlDeploymentOutputFile输出变量名。

- task: SqlAzureDacpacDeployment@1
  displayName: 'Insertion SQL Task'
  name: sqlInsert
  inputs:
    ...
  enabled: true

- script: echo "$(sqlInsert.SqlDeploymentOutputFile)"

Azure Web App task does not provide the same mechanism. You could always call the Pipelines - Get Logs API得到你所需要的:

pool:
  vmImage: ubuntu-latest

steps:
- powershell: Write-Host "Hello World"
  name: 'HelloWorld'

- powershell: |
    Write-Host "This is a script that could use $env:SYSTEM_ACCESSTOKEN"
    
    # Construct the REST URL to obtain Build ID
    $uri = "$(System.CollectionUri)$(System.TeamProject)/_apis/pipelines/$(System.DefinitionId)/runs/$(Build.BuildId)/logs?api-version=6.0-preview.1"
    Write-Host "$uri"

    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "system", $env:SYSTEM_ACCESSTOKEN)))
    $logs = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)