azure - Azure DevOps 任务如何使用先前 Azure DevOps 任务的输出

标签 azure azure-devops azure-pipelines

执行 DevOps 发布管道时:

当前执行任务 2 时,我想引用任务 1 的输出。(任务 1 从 REST api 调用检索 json。)

它们都在同一个无代理作业中(它们都是 REST api 调用)。基本上我试图将它们“链接”在一起和/或将 json 输出从任务 1 传递到任务 2

我看过有关变量的文档,但我不确定是否可以通过任务 1 的输出设置变量,或者如果有内置的东西(例如 ${PreviousStep.Output})就更好了

这可能吗?

最佳答案

我们可以在管道中创建一个临时文件,并将响应正文保存到该文件中,然后在下一个 power shell 中读取该文件。

在我的示例中,我在第一个 Power shell 任务中获取发布管道定义,并将输出保存到临时文件,然后在下一个 Power shell 任务中读取它。

管道定义示例

trigger:
- none

pool:
  vmImage: 'windows-latest'


steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      New-Item $(Build.SourcesDirectory)\test1.txt
      $outfile = "$(Build.SourcesDirectory)\test1.txt"
      Write-Host $outfile
      $url = "https://vsrm.dev.azure.com/{Org name}/{Project name}/_apis/release/definitions/{Definition ID}?api-version=6.0-preview.4"
      Write-Host "URL: $url"
      $connectionToken="{PAT}"
      $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
      $pipelineInfo = Invoke-RestMethod -Uri $url -Headers @{authorization = "Basic $base64AuthInfo"} -Method Get
      $pipelineInfo | Out-File -FilePath $outfile
      
      

      

- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      $json = Get-Content -Path $(Build.SourcesDirectory)\test1.txt
      Write-Host $json

结果:

enter image description here

关于azure - Azure DevOps 任务如何使用先前 Azure DevOps 任务的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64852457/

相关文章:

terraform - 命令行参数过多 Terraform 计划

c# - Azure 事件中心 - 如何在 .Net Core WebAPI 中实现使用者?

c# - Azure函数无法读取应用程序设置和连接字符串

Azure DevOps : Cannot Build an Image using NPM private registry even after setting NPM Authenticate

python - 保持azure管道作业中的postgres docker容器运行

Azure devops 分支控制不应验证模板文件的分支

Azure Durable Functions 扇出规模有限制吗?

azure - 使用 ADF 的数据 block 或函数?

azure - Azure DevOps 构建中的多个通配符触发器

asp.net-core - VSTS - 构建 ASP.NET Core 2.0,编译错误 : Could not locate the assembly "Microsoft.AspNetCore.Mvc.ViewFeatures"