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

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

问题描述

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

- 任务:AzureCLI@2displayName: '列出 DEV 中读取策略的信息'名称:我的输出输入:azureSubscription:我的订阅脚本类型:ps脚本位置:inlineScriptinlineScript: az servicebus 主题授权规则密钥列表 --resource-group myRG --namespace-name mySB --topic-name myTopic --name Listen

在 powershell 中运行这个 az 命令时,我得到了这个回报:

<代码>{"aliasPrimaryConnectionString": null,"aliasSecondaryConnectionString": null,"keyName": "听","primaryConnectionString": "Endpoint=sb://someKey","primaryKey": "somePrimaryKey","secondaryConnectionString": "Endpoint=sb://another key","secondaryKey": "someSecondaryKey"}

我也在管道的日志中得到了这个输出.我确实期望,根据

<小时>

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

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

调用{你的命令}>tmpFile1设置/p myvar= <文件1echo "##vso[task.setvariable variable=testvar;]%myvar%"

请参阅此线程:

另外,不知道你是否熟悉上面的脚本,这里我对这些脚本做了一些改动,让你可以使用:

SETLOCAL ENABLEDELAYEDEXPANSION设置计数=1FOR/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`)做 (SET var!count!=%%FSET/a count=!count!+1)回声%var5%echo "##vso[task.setvariable variable=testvar;]%var5%"本地

你想传递给下一个任务的是primaryConnectionString,它的数字是5,所以这里i的值为5.

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

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

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)

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:windowssystem32cmd.exe" /D /E:ON /V:OFF /S /C "CALL "d:a\_temp3d1130bd-f7f7-4e30-8ae2-31e6f9d0800e.cmd""
$(myOutput.primaryConnectionString)
Finishing: CmdLine

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

解决方案

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%"

Or

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

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


Update:

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

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 Pipeline 中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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