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

2024-03-24

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

使用 InvokeRestAPI 任务调用 API 工作正常,但现在我想使用在对此 API 调用的响应中发送的信息。文档here https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/http-rest-api?view=azure-devops表示此任务可用于调用 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

我可能可以用“常规”Powershell 脚本替换 InvokeRestAPI,但 InvokeRestAPI 任务似乎更容易设置。

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


最后一部分描述the task https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/http-rest-api?view=azure-devops:

使用此任务调用 HTTP API 并解析响应.

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

定义何时通过任务的标准。没有标准意味着响应内容不会影响结果。示例:- 对于响应 {"status" : "successful"},表达式可以是 eq(root['status'], 'successful')。更多信息 https://go.microsoft.com/fwlink/?linkid=842996

因此,如果您需要在下一个任务中使用响应,则需要使用 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"}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐