[英]Is it possible to set variables in the Azure Powershell task in Azure DevOps and pass them to another task?
From reading the Azure DevOps documentation here , it looks like it's possible to define variables in scripts, but only for Bash and Powershell tasks.通过阅读此处的 Azure DevOps 文档,看起来可以在脚本中定义变量,但仅适用于 Bash 和 Powershell 任务。 If I want to use the Azure Powershell task, it does not seem to work.
如果我想使用 Azure Powershell 任务,它似乎不起作用。 Here is my yaml for what I am trying to do.
这是我正在尝试做的事情的 yaml。
- stage: promote_image
displayName: Promote VM Image to Prod
jobs:
- job: VMPush
steps:
- task: AzurePowerShell@5
inputs:
ScriptType: 'InlineScript'
Inline: |
$srcimageversion = get-azgalleryimageversion -ResourceGroupName $srcRG `
-GalleryName $srcgallery `
-GalleryImageDefinitionName $srcimagename | select -last 1
Write-Host "##vso[task.setvariable variable=VersionName;isOutput=true]$srcimageversion.Name"
Write-Host "##vso[task.setvariable variable=SourceId;isOutput=true]$srcimageversion.Id.ToString()"
Write-Host "Version name is $($env:VersionName)"
Write-Host "VM source is $($env:SourceId)"
azurePowerShellVersion: 'LatestVersion'
azureSubscription: $(nonProd_Subscription)
- task: AzurePowerShell@5
inputs:
ScriptType: 'InlineScript'
Inline: |
Write-Host "Version name is $($env:VersionName)"
Write-Host "VM source is $($env:SourceId)"
azurePowerShellVersion: 'LatestVersion'
azureSubscription: $(prod_Subscription)
The VersionName and SourceId variables don't get output in either task. VersionName 和 SourceId 变量不会在任一任务中获得输出。 Is it even possible to set variables like this with the Azure Powershell task?
甚至可以使用 Azure Powershell 任务设置这样的变量吗? I am using the Azure Powershell task since I need to obtain information from a resource in one Azure subscription and then use it to deploy something in another Azure subscription.
我正在使用 Azure Powershell 任务,因为我需要从一个 Azure 订阅中的资源获取信息,然后使用它在另一个 Azure 订阅中部署某些内容。
Yes, this is possible.是的,这是可能的。 You have a mistake in a way how you get variables.
您在获取变量的方式上有错误。 First let's name your fist task and then use just
$(setvarStep.VersionName)
instead of $($env:VersionName)"
you should get what you need首先让我们命名你的第一个任务,然后只使用
$(setvarStep.VersionName)
而不是$($env:VersionName)"
你应该得到你需要的
- task: AzurePowerShell@5
name: setvarStep
inputs:
ScriptType: 'InlineScript'
Inline: |
$value1 = "Value1"
$value2 = "Value2"
Write-Host "##vso[task.setvariable variable=VersionName;isOutput=true]$value1"
Write-Host "##vso[task.setvariable variable=SourceId;isOutput=true]$value2"value"
azurePowerShellVersion: 'LatestVersion'
azureSubscription: 'rg-the-code-manual'
- task: AzurePowerShell@5
inputs:
ScriptType: 'InlineScript'
Inline: |
Write-Host "Version name is $(setvarStep.VersionName)"
Write-Host "VM source is $(setvarStep.SourceId)"
azurePowerShellVersion: 'LatestVersion'
azureSubscription: 'rg-the-code-manual'
Here agree with Krzysztof Madej .这里同意Krzysztof Madej 的观点。 To use the output variable of the task, we need to use the depend name .
要使用任务的输出变量,我们需要使用依赖名称。
Use outputs in the same job:在同一作业中使用输出:
steps:
- task: MyTask@1 # this step generates the output variable
name: ProduceVar # because we're going to depend on it, we need to name the step
- script: echo $(ProduceVar.MyVar) # this step uses the output variable
In addition, use outputs in a different job:此外,在不同的作业中使用输出:
jobs:
- job: A
steps:
- task: MyTask@1 # this step generates the output variable
name: ProduceVar # because we're going to depend on it, we need to name the step
- job: B
dependsOn: A
variables:
# map the output variable from A into this job
varFromA: $[ dependencies.A.outputs['ProduceVar.MyVar'] ]
steps:
- script: echo $(varFromA) # this step uses the mapped-in variable
For detailed guide, please refer to this official documnet .有关详细指南,请参阅此官方文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.