将AzureCLI @ 2的输出用作Azure DevOps管道中的变量 [英] Use output from AzureCLI@2 as variable in Azure DevOps Pipeline

查看:142
本文介绍了将AzureCLI @ 2的输出用作Azure DevOps管道中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在git的项目根目录中有一个azure-pipelines.yml文件. 在此文件中,我想将下一个任务中一个任务的输出用作变量. 我的任务

I have an azure-pipelines.yml file at the root of my project in git. In this file I would like to use the output from one task in the next task, as a variable. My task

- 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命令时,我得到以下回报:

When running this az command in powershell i get this in return:

{
  "aliasPrimaryConnectionString": null,
  "aliasSecondaryConnectionString": null,
  "keyName": "Listen",
  "primaryConnectionString": "Endpoint=sb://someKey",
  "primaryKey": "somePrimaryKey",
  "secondaryConnectionString": "Endpoint=sb://another key",
  "secondaryKey": "someSecondaryKey"
}

我也确实在管道的日志中得到了此输出. 根据

I do get this output in the logs in the pipeline as well. I did expect, according to documentation to be able to use this in the next step. For example:

- script: echo $(myOutput.primaryConnectionString)

日志没有给我带来什么,而不是获取primaryConnectionString的值:

Instead of getting the value of the primaryConnectionString this is what the log gives me:

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://docs.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的值替换?

Why is the variable name not replaced with the value of primaryConnectionString?

推荐答案

因为您错过了 Azure Cli 任务中设置的变量.

Because you missed the variable set in your Azure Cli task.

在指定任务执行期间生成的变量的生命周期仅在任务执行阶段.这意味着,一旦任务完成,变量将消失.如果您希望它可用于下一个任务,则需要使用脚本将其设置为输出变量.这就是你错过的.

The life cycle of the variable which generated during the specified task execution is only in the task execution phase. This means, once task finished, the variable will disappear. If you want it available for next task, you need to use scripts to make it as an output variable. This is what you missed.

实际上,在

In fact, in the doc you point out, it use context to mention out this:

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

To set the command output as variable and used by the next task, please use below script:

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任务中.还有另一个用户提出了类似的要求.

See this thread: Set Output Variable in Azure CLI task on VSTS. There has another user raised similar requirement.

更新:

根据评论,是,请在input:inlinescript中输入上述脚本.像这样:

Based on the comment, Yes, please input above scripts into input:inlinescript. Like this:

此外,不确定您是否熟悉上述脚本,在这里我对这些脚本进行了一些更改,以使其可供您使用:

In addition, not sure whether you are familiar with above script, here I modified some changes into those scripts to make it available for you:

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.

What you want to passed to next task is primaryConnectionString and its number is 5. So here the value of i is 5.

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

See the output of mine in the next task:

这篇关于将AzureCLI @ 2的输出用作Azure DevOps管道中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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