且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将AzureCLI @ 2的输出用作Azure DevOps管道中的变量

更新时间:2023-11-04 11:02:28

因为您错过了 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: