Mar*_*rkD 2 azure azure-powershell azure-devops
我正在尝试使用 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"
Run Code Online (Sandbox Code Playgroud)
以下是 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
Run Code Online (Sandbox Code Playgroud)
请注意,“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
Run Code Online (Sandbox Code Playgroud)
因此,对于您想要在同一作业中访问变量的场景,只需删除 isOutput=true;
(这不是必需的)。或者如果您在声明中$(referencename.variablename)
添加,请使用格式。isOutput=true;
(没有必要,不推荐,但也应该适用于当前的工作)
此外:
有关$(referencename.variablename)
格式的详细信息。
对于经典管道:(在 Powershell 任务中将名称设置为 Test)
$(Test.MobileAppInsightsKey)
代表变量的值。
对于 yaml 管道:
- powershell: echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]this is the value"
name: Test
- script: echo $(Test.myOutputVar)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1478 次 |
最近记录: |