如何从 Azure DevOps 管道中的另一个作业访问 InvokeRestAPI 任务的响应?

How to access the response of the InvokeRestAPI task from another job in an Azure DevOps pipeline?

我正在尝试通过从 Azure DevOps 管道中调用他们的 REST API 来自动部署 Elastic 云中的 Elasticsearch 资源。

使用 InvokeRestAPI 任务调用 API 工作正常,但现在我想使用在响应此 API 调用时发送的信息。文档 here 说此任务可用于调用 HTTP API 并解析响应,但它没有提供有关如何执行此操作的信息。

到目前为止,我已尝试根据依赖关系向我的下一份工作添加变量,但这不起作用:

- job: DeployES
  dependsOn: GenerateTipTenantId
  pool: server
  variables:
    tenantid: $[ dependencies.GenerateTipTenantId.outputs['GenerateGuid.faketenantid'] ]
  steps:
    - task: InvokeRESTAPI@1
      name: EsRest
      inputs:
        <InvokeRESTAPI task inputs generated by the assistant>

- job: processDeployment
  dependsOn: DeployES
  pool:
    vmImage: 'ubuntu-latest'
  variables:
    depid: $[ dependencies.DeployES.outputs['EsRest.Response.id'] ]
  steps:
    - script: echo $(depid)
      name: echoid

我可能可以用 'regular' Powershell 脚本替换 InvokeRestAPI,但 InvokeRestAPI 任务似乎更容易设置。

所以问题是:如何访问 API 响应中的 JSON 对象并将其部分传递到管道中的下一个作业?

描述的最后一部分the task

Use this task to invoke an HTTP API and parse the response.

指的是 successCriteria 选项,它允许您使用响应来指示成功或失败:

Criteria which defines when to pass the task. No criteria means response content does not influence the result. Example:- For response {"status" : "successful"}, the expression can be eq(root['status'], 'successful'). More information

因此,如果您需要在下一个任务中使用响应,则需要使用 powershell、bash 或任何其他 shell 任务。例如,这里有 powershell 脚本,我使用 API 获取和修改 ReleaseVersion:

$url = "https://vsrm.dev.azure.com/thecodemanual/$env:SYSTEM_TEAMPROJECTID/_apis/Release/definitions/$($env:RELEASE_DEFINITIONID)?api-version=5.0"

$pipeline = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
Write-Host "Pipeline = $($pipeline | ConvertTo-Json -Depth 100)"

$buildNumber = $env:BUILD_BUILDNUMBER
$pipeline.variables.ReleaseVersion.value = $buildNumber

####****************** update the modified object **************************
$json = @($pipeline) | ConvertTo-Json -Depth 99

$updatedef = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}