使用AzureCLI@2的输出作为Azure DevOps Pipeline中的变量

7
我在git项目的根目录下有一个azure-pipelines.yml文件。 在这个文件中,我想在下一个任务中使用前一个任务的输出作为变量。 我的任务是:
- task: AzureCLI@2
            displayName: 'List info on read policy in DEV'
            name: myOutput
            inputs:
              azureSubscription: mySub
              scriptType: ps
              scriptLocation: inlineScript
              inlineScript: az servicebus topic authorization-rule keys list --resource-group myRG --namespace-name mySB --topic-name myTopic --name Listen

在 PowerShell 中运行此 az 命令时,我会得到以下返回结果:
{
  "aliasPrimaryConnectionString": null,
  "aliasSecondaryConnectionString": null,
  "keyName": "Listen",
  "primaryConnectionString": "Endpoint=sb://someKey",
  "primaryKey": "somePrimaryKey",
  "secondaryConnectionString": "Endpoint=sb://another key",
  "secondaryKey": "someSecondaryKey"
}

我在流水线日志中得到了这个输出结果。 根据文档的说明,我期望可以在下一步骤中使用它。 例如:

- script: echo $(myOutput.primaryConnectionString)

与其获取 primaryConnectionString 的值,日志显示的是:

Starting: CmdLine
==============================================================================
Task         : Command line
Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version      : 2.151.2
Author       : Microsoft Corporation
Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
Script contents:
echo $(myOutput.primaryConnectionString)
========================== Starting Command Output ===========================
"C:\windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "d:\a\_temp\3d1130bd-f7f7-4e30-8ae2-31e6f9d0800e.cmd""
$(myOutput.primaryConnectionString)
Finishing: CmdLine

为什么变量名没有被primaryConnectionString的值所替换?
2个回答

6
在Linux的shell上,您可以像这样操作:
- task: AzureCLI@2
  displayName: 'List info on read policy in DEV'
  name: myOutput
  inputs:
    azureSubscription: mySub
    scriptType: ps
    scriptLocation: inlineScript
    inlineScript: |
      output=$(az servicebus topic authorization-rule keys list --resource-group myRG --namespace-name mySB --topic-name myTopic --name Listen)
      echo $output | jq .
      echo "##vso[task.setvariable variable=testvar;isoutput=true]$output"
echo $output | jq . 确保命令的输出仍然包含在日志中。
稍后可以按以下方式访问内容:
- task: Bash@3
  displayName: 'Do something with the variable'
  inputs:
    targetType: 'inline'
    script: |
      echo "$(myOutput.testvar)"
      the_id="$(echo $(myOutput.testvar) | jq -r '.id')"

(假设代理已安装 jq 的分布。)

很好,你对这个写了解释:“echo $output | jq。” - Oliver Nilsen

3
因为您错过了在Azure Cli任务中设置的变量。
在指定任务执行期间生成的变量的生命周期仅限于任务执行阶段。这意味着,一旦任务完成,变量将消失。如果您希望它在下一个任务中可用,则需要使用脚本将其作为输出变量。这就是您错过的部分。
实际上,在您提到的doc中,它使用上下文来提到这一点:

enter image description here


要将命令输出设置为变量并由下一个任务使用,请使用以下脚本:

FOR /F "tokens=* USEBACKQ" %%F IN (`{your command}`) DO (
SET var=%%F
)
echo "##vso[task.setvariable variable=testvar;]%var%"

或者

call {your command}>tmpFile1
set /p myvar= < tmpFile1 
echo "##vso[task.setvariable variable=testvar;]%myvar%"

请查看此主题:在VSTS中设置Azure CLI任务的输出变量。另有一位用户提出了类似的要求。


更新:

根据评论,是的,请将上述脚本输入到input:inlinescript中。就像这样:

enter image description here

此外,不确定您是否熟悉上述脚本,我在这里对这些脚本进行了一些修改,以使其适用于您。
SETLOCAL ENABLEDELAYEDEXPANSION
SET count=1
FOR /F "tokens=* USEBACKQ" %%F IN (`call az servicebus topic authorization-rule keys list --resource-group Wicresoft-Support --namespace-name merlin1120 --topic-name mytopic --name myname`) DO (
  SET var!count!=%%F
  SET /a count=!count!+1
)
ECHO %var5%
echo "##vso[task.setvariable variable=testvar;]%var5%"
ENDLOCAL

你想要传递给下一个任务的是primaryConnectionString,其编号为5。因此,在这里i的值为5。

请查看下一个任务中我的输出:

enter image description here


我一直以为Azure CLI任务实际上是根据az命令返回的json设置输出变量。我想我错了。你能否分享更多关于如何将这些示例输入到Azure CLI任务中的细节?我是否需要将所有这些行都放入input:inlineScript中? - Christer Berglund
@ChristerBerglund,是的,你应该将上面的脚本放入内联中。请查看我的更新。 - Mengdi Liang
太棒了!谢谢。现在我得到了字符串。我只需要稍微清理一下就能得到值了。非常感谢! - Christer Berglund
@ChristerBerglund 欢迎 - Mengdi Liang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接