通过阅读Azure DevOps文档here,看起来可以在脚本中定义变量,但仅适用于Bash和Powershell任务。如果我要使用Azure Powershell任务,它似乎无法正常工作。这是我要执行的操作。
- 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)
在任一任务中,VersionName和SourceId变量均未获得输出。甚至可以通过Azure Powershell任务设置像这样的变量吗?我正在使用Azure Powershell任务,因为我需要从一个Azure订阅中的资源获取信息,然后使用它在另一个Azure订阅中部署某些东西。
答案 0 :(得分:1)
是的,这是可能的。您在获取变量的方式上有一个错误。首先,让我们命名您的第一项任务,然后仅使用$(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'
答案 1 :(得分:0)
同意 Krzysztof Madej 。要使用任务的输出变量,我们需要使用依赖名称。
在同一作业中使用输出:
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
此外,在其他作业中使用输出:
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
有关详细指南,请参阅此官方documnet。