本文介绍了YAML管道-设置变量并在表达式中用于模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个任务中动态设置变量,并在后续任务的条件下使用它.我完全可以使它工作.正在设置var,但模板未执行.

I'm trying to dynamically set a variable in one task and use it in a condition on a following task. I can get this to work at all. The var is being set but the templates aren't executing.

powershell步骤设置变量,以下步骤旨在有条件地在此变量上运行

The powershell step sets the variable, the following steps are meant to run conditionally on this var

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)'

推荐答案

我找到了一种在同一版本中进行此操作的方法

I found a way to do this in the same Build

方法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触发器.

Method 2 - Separate BuildsThanks @4c74356b41. I ended up having to split the tasks into 2 separate builds and convert the above process into Jobs instead of Tasks. I bound a variable group to both builds and Build 1 updates the Variable in Variable Group, and Build 2 triggers from Build 1.

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触发

Build 2 - triggered from build 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)'

这篇关于YAML管道-设置变量并在表达式中用于模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 20:24