azure - 无法在 Devops 步骤中设置 powershell 输出变量
问题描述
我正在尝试使用 powershell 脚本中的输出变量。我正在使用经典 UI 在线使用 Devops,并在发布管道中尝试了 powershell 4.* 和 Powershell 5.* 任务。
我正在使用一个自托管代理,它可以正常工作并执行许多其他构建和发布 powershell 的东西。Azure Powershell 模块版本 3.5.0(现在不使用 4.x 是有原因的)。
为了简化它,这是我的测试内联脚本总共......:
Write-Host '##vso[task.setvariable variable=MobileAppInsightsKey;isOutput=true;]thisisthekey'
Write-Host "This is host"
Write-Output '##vso[task.setvariable variable=MobileAppInsightsKey;isOutput=true;]thisisthekey'
Write-Output "This is output"
这是 Azure powershell 任务的输出。(4.*)
2020-07-01T00:06:57.2970494Z ##[section]Starting: Azure PowerShell script: InlineScript
2020-07-01T00:06:57.3335882Z
==============================================================================
2020-07-01T00:06:57.3336692Z Task : Azure PowerShell
2020-07-01T00:06:57.3337292Z Description : Run a PowerShell script within an Azure environment
2020-07-01T00:06:57.3337566Z Version : 4.171.1
2020-07-01T00:06:57.3338039Z Author : Microsoft Corporation
2020-07-01T00:06:57.3338575Z Help : https://aka.ms/azurepowershelltroubleshooting
2020-07-01T00:06:57.3338930Z
==============================================================================
2020-07-01T00:06:58.5902105Z ## Validating Inputs
2020-07-01T00:06:58.5915067Z ## Validating Inputs Complete
2020-07-01T00:06:58.5924850Z ## Initializing Az module
2020-07-01T00:06:59.0747435Z ##[command]Import-Module -Name C:\Program
Files\WindowsPowerShell\Modules\Az.Accounts\1.9.0\Az.Accounts.psd1 -Global
2020-07-01T00:07:00.0802372Z ##[command]Clear-AzContext -Scope Process
2020-07-01T00:07:01.5597330Z ##[command]Clear-AzContext -Scope CurrentUser -Force -ErrorAction SilentlyContinue
2020-07-01T00:07:01.9691282Z ##[command]Connect-AzAccount -Identity @processScope
2020-07-01T00:07:03.1860248Z ##[command] Set-AzContext -SubscriptionId 5ec8ec06-XXXX-XXXX-XXXX- c0ff86c50e4 -TenantId ***
2020-07-01T00:07:03.9196710Z ## Az module initialization Complete
2020-07-01T00:07:03.9203692Z ## Beginning Script Execution
2020-07-01T00:07:03.9674782Z ##[command]& 'C:\DevOps\_work\_temp\1b1b130b-4306-448b-b4b2-e7daefc2382e.ps1'
2020-07-01T00:07:03.9974844Z This is host
2020-07-01T00:07:04.0101140Z This is output
2020-07-01T00:07:04.0517610Z ##[command]Disconnect-AzAccount -Scope Process -ErrorAction Stop
2020-07-01T00:07:04.4795714Z ##[command]Clear-AzContext -Scope Process -ErrorAction Stop
2020-07-01T00:07:04.9468120Z ## Script Execution Complete
2020-07-01T00:07:04.9857991Z ##[section]Finishing: Azure PowerShell script: InlineScript
请注意,“This is Host”和“This is Output”都显示,但“##vso[....”不显示。
我在后续步骤中尝试读取的 MobileAppInsightsKey 也是空的(未初始化)。
希望有人能指出我正确的方向。
谢谢,马克。
解决方案
并对这个问题做一个清晰的描述:
要在您的场景中定义作业范围的变量,我们不需要添加isOutput=true;
1.对于工作范围的变量(变量仅在当前工作中有效):
Write-Host '##vso[task.setvariable variable=MobileAppInsightsKey]thisisthekey'
足够的。$(MobileAppInsightsKey)
我们可以在 CMD 任务中通过格式输出它的值。
2.对于多作业输出变量(变量在多作业中有效):
我们应该使用Write-Host '##vso[task.setvariable variable=MobileAppInsightsKey;isOutput=true;]thisisthekey'
.
在当前工作中:您可以使用$(referencename.variablename)
它来获取它的价值。(支持经典管道和yaml管道)
后续作业中:使用以下格式访问变量,此格式仅支持yaml管道!!!
- job: B
dependsOn: A
pool:
vmImage: 'ubuntu-16.04'
variables:
myVarFromJobA: $[ dependencies.A.outputs['setvarStep.myOutputVar'] ] # map in the variable
# remember, expressions require single quotes
steps:
- script: echo $(myVarFromJobA)
name: echovar
因此,对于您想要在同一作业中访问变量的场景,只需删除 isOutput=true;
(没有必要)。或者$(referencename.variablename)
如果您isOutput=true;
在语句中添加,请使用格式。(没有必要,不推荐,但它也应该适用于当前工作)
此外:
有关$(referencename.variablename)
格式的详细信息。
对于经典管道:(在 Powershell 任务中将名称设置为测试)
$(Test.MobileAppInsightsKey)
表示变量的值。
对于 yaml 管道:
- powershell: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]this is the value"
name: Test
- script: echo $(Test.myOutputVar)
推荐阅读
- kubernetes - 如何找出 Kubernetes 中可用的端口?
- html - 移除 HTML 元素的蓝色轮廓
- jquery - 客户端可以从 $.get() jQuery 调用中获取 URL 吗?
- python - 传递单击的按钮以在 Tkinter 中运行
- xml - 在 XSLT 中显示 URL
- sql - 计算往返通勤中停靠站之间的时间
- react-native - 如何根据状态更新反应导航 5 headerRight 中的文本?
- java - Spring Data JPA - javax.persistence.TransactionRequiredException:执行更新/删除查询
- python - Django-admin 自定义命令不存储缓存值
- mysql - 由于外键无法创建表