Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YAML pipeline - Set variable and use in expression for template

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.

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

like image 814
Bevan Avatar asked Oct 02 '19 10:10

Bevan


2 Answers

EDIT: I found a way to do this in the same Build

Method 1 - Same Build

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

Method 2 - Separate Builds Thanks @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)

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)'
like image 187
Bevan Avatar answered Oct 18 '22 12:10

Bevan


I dont think this will work, because build flow is being decided before the steps are being run, so you cannot really use build time variables to determine if the step is to be run

like image 44
4c74356b41 Avatar answered Oct 18 '22 12:10

4c74356b41