执行 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
结果:
关于azure - Azure DevOps 任务如何使用先前 Azure DevOps 任务的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64852457/