我正在尝试在一个任务中动态设置变量,并在后续任务的条件下使用它。我完全可以使用它。正在设置var,但模板未执行。
powershell
步骤设置变量,以下步骤旨在有条件地在此变量上运行
variables:
- group: Global
trigger:
branches:
include:
- master
paths:
include:
- blah1/*.csv
- blah2/*.csv
resources:
repositories:
- repository: Templates
name: Templates/Templates
type: git
pool:
vmImage: vs2017-win2016
demands: azureps
steps:
- powershell: |
$CSV_File = Get-ChildItem -Recurse -Include "*.csv" | sort LastWriteTime | select -last 1
$Subscription = [regex]::Matches(($CSV_File | select -ExpandProperty DirectoryName), "([^\\]+)$").Value
#Set Variable for Pipeline
Write-Host "##vso[task.setvariable variable=Subscription]$Subscription"
displayName: 'PowerShell - Set Subscription'
name: 'SetSubscription'
- ${{ if eq(variables['SetSubscription.Subscription'], 'DEV1') }}:
- template: Template/Template.yml@Templates
parameters:
AzureSubscription: 'DEV1 (GUID)'
- ${{ if eq(variables.Subscription, 'PROD1') }}:
- template: Template/Template.yml@Templates
parameters:
AzureSubscription: 'PROD1 (GUID)'
- ${{ if eq(variables['Subscription'], 'DEV2') }}:
- template: Template/Template.yml@Templates
parameters:
AzureSubscription: 'DEV2 (GUID)'
- ${{ if eq(variables['SetSubscription.Subscription'], 'PROD2') }}:
- template: Template/Template.yml@Templates
parameters:
AzureSubscription: 'PROD2 (GUID)'
答案 0 :(得分:2)
我认为这不会起作用,因为在步骤运行之前就已经确定了构建流程,因此您不能真正使用构建时间变量来确定是否要运行该步骤
答案 1 :(得分:1)
编辑:我在同一版本中找到了一种方法
方法1-相同版本
jobs:
- job: PreTasks
steps:
- powershell: |
$Subscription = Get Var Command here
#Set Variable for Pipeline
Write-Host "##vso[task.setvariable variable=Subscription;isOutput=true]$Subscription"
name: SetSubscription
displayName: 'PowerShell - Set Subscription'
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
- job: 1
dependsOn: PreTasks
condition: eq(dependencies.PreTasks.outputs['SetSubscription.Subscription'], 'DEV1')
steps:
- template: Template/Template.yml@Templates
parameters:
AzureSubscription: 'DEV1 (GUID)'
- job: 2
dependsOn: PreTasks
condition: eq(dependencies.PreTasks.outputs['SetSubscription.Subscription'], 'PROD1')
steps:
- template: Template/Template.yml@Templates
parameters:
AzureSubscription: 'PROD1 (GUID)'
- job: 3
dependsOn: PreTasks
condition: eq(dependencies.PreTasks.outputs['SetSubscription.Subscription'], 'DEV2')
steps:
- template: Template/Template.yml@Templates
parameters:
AzureSubscription: 'DEV2 (GUID)'
- job: 4
dependsOn: PreTasks
condition: eq(dependencies.PreTasks.outputs['SetSubscription.Subscription'], 'PROD2')
steps:
- template: Template/Template.yml@Templates
parameters:
AzureSubscription: 'PROD2 (GUID)'
方法2-单独构建 感谢@ 4c74356b41。我最终不得不将任务分成2个单独的版本,并将上述过程转换为Jobs而不是Tasks。我将变量组绑定到构建,构建1更新了变量组中的变量,并从构建1更新了构建2触发器。
Build 1
- powershell: |
echo $env:AZURE_DEVOPS_EXT_PAT | az devops login
az devops configure -d organization=https://dev.azure.com/<Organisation>/project=<project>
az pipelines variable-group variable update --id <VariableGroupID> --name Subscription --value $Subscription
displayName: 'PowerShell - Set Subscription'
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)
内部版本2-从内部版本1触发
jobs:
- job: 1
condition: eq(variables.Subscription, 'DEV1')
steps:
- template: Template/Template.yml@Templates
parameters:
AzureSubscription: 'DEV1 (GUID)'
- job: 2
condition: eq(variables.Subscription, 'PROD1')
steps:
- template: Template/Template.yml@Templates
parameters:
AzureSubscription: 'PROD1 (GUID)'
- job: 3
condition: eq(variables.Subscription, 'DEV2')
steps:
- template: Template/Template.yml@Templates
parameters:
AzureSubscription: 'DEV2 (GUID)'
- job: 4
condition: eq(variables.Subscription, 'PROD2')
steps:
- template: Template/Template.yml@Templates
parameters:
AzureSubscription: 'PROD2 (GUID)'