Chr*_*und 7 azure azure-devops
我的 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
Run Code Online (Sandbox Code Playgroud)
在 powershell 中运行此 az 命令时,我得到以下回报:
{
"aliasPrimaryConnectionString": null,
"aliasSecondaryConnectionString": null,
"keyName": "Listen",
"primaryConnectionString": "Endpoint=sb://someKey",
"primaryKey": "somePrimaryKey",
"secondaryConnectionString": "Endpoint=sb://another key",
"secondaryKey": "someSecondaryKey"
}
Run Code Online (Sandbox Code Playgroud)
我也在管道的日志中得到了这个输出。根据文档,我确实希望能够在下一步中使用它。例如:
- script: echo $(myOutput.primaryConnectionString)
Run Code Online (Sandbox Code Playgroud)
日志给我的不是获取 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
Run Code Online (Sandbox Code Playgroud)
为什么变量名没有替换为primaryConnectionString 的值?
在带有 shell 的 Linux 上,你可以这样做:
- 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"
Run Code Online (Sandbox Code Playgroud)
这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')"
Run Code Online (Sandbox Code Playgroud)
jq(假设代理上安装了发行版。)
因为您错过了Azure Cli任务中设置的变量。
指定任务执行过程中生成的变量的生命周期仅在任务执行阶段。这意味着,一旦任务完成,变量就会消失。如果您希望它可用于下一个任务,则需要使用脚本将其作为输出变量。这就是你错过的。
事实上,在您指出的文档中,它使用上下文来指出这一点:
要将命令输出设置为变量并供下一个任务使用,请使用以下脚本:
FOR /F "tokens=* USEBACKQ" %%F IN (`{your command}`) DO (
SET var=%%F
)
echo "##vso[task.setvariable variable=testvar;]%var%"
Run Code Online (Sandbox Code Playgroud)
或者
call {your command}>tmpFile1
set /p myvar= < tmpFile1
echo "##vso[task.setvariable variable=testvar;]%myvar%"
Run Code Online (Sandbox Code Playgroud)
请参阅此线程:在 VSTS 上的 Azure CLI 任务中设置输出变量。还有一位用户提出了类似的要求。
更新:
根据评论,是的,请将以上脚本输入到input:inlinescript. 像这样:
另外,不知道您是否熟悉上面的脚本,这里我对这些脚本进行了一些更改以供您使用:
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
Run Code Online (Sandbox Code Playgroud)
您要传递给下一个任务的是primaryConnectionString,其编号为 5。所以这里 的值为i5。
在下一个任务中查看我的输出:
| 归档时间: |
|
| 查看次数: |
9722 次 |
| 最近记录: |