如何从Azure DevOps管道中的另一个作业访问InvokeRestAPI任务的响应? [英] How to access the response of the InvokeRestAPI task from another job in an Azure DevOps pipeline?

查看:11
本文介绍了如何从Azure DevOps管道中的另一个作业访问InvokeRestAPI任务的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过从Azure DevOps管道内调用他们的睡觉API,自动在弹性云中部署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

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

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

推荐答案

说明的最后一部分the task

使用此任务可调用HTTP API并分析响应

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

定义何时传递任务的条件。无条件表示响应内容不影响结果。示例:-对于响应{&Quot;Status&Quot;:&Quot;Successful&Quot;},表达式可以是eq(root[‘status’],‘Successful’)。More information

因此,如果您需要在下一个任务中使用Response,则需要使用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"}

这篇关于如何从Azure DevOps管道中的另一个作业访问InvokeRestAPI任务的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆