0
votes

I'm trying to create a yaml pipeline with three stages that can be executed in different orders depending on the Pull Request source branch. For example:

If var = default then

  1. Stage A
  2. Stage B
  3. Stage C

If var != default then

  1. Stage A
  2. Stage C
  3. Stage B

Here's what I have so far: variables:

- name: abcDependsOn
  ${{ if not(contains(variables['System.PullRequest.SourceBranch'], 'hotfix' )) }}:
    value: 'Publish'
  ${{ if contains(variables['System.PullRequest.SourceBranch'], 'hotfix' ) }}:
    value: 'DeployXYZ'
- name: xyzDependsOn
  ${{ if not(contains(variables['System.PullRequest.SourceBranch'], 'hotfix' )) }}:
    value: 'DeployABC'
  ${{ if contains(variables['System.PullRequest.SourceBranch'], 'hotfix' ) }}:
    value: 'Publish'

stages: 
- stage: Publish
  jobs:
  - job: publish

- stage: DeployABC
  dependsOn: ${{ variables.abcDependsOn }}
  jobs:
  - job: deployabc

- stage: DeployXYZ
  dependsOn: ${{ variables.xyzDependsOn }}
  jobs:
  - job: deployxyz

This isn't work because the dependsOn template expression variables are being evaluated before the runtime expression variables (I think). Is this even possible?

1

1 Answers

0
votes

${{ if not(contains(variables['System.PullRequest.SourceBranch'], 'hotfix' )) }}: cannot be parsed at compile time. Therefore, variable cannot dynamically set its value based on source branch conditions

As a workaround , you can add another set of (DeployXYZ,DeployABC) stages and exchange the order of stages. Set as follows:

stages: 
- stage: Publish
  jobs:
  - job: publish

- stage: DeployABC
  condition: contains(variables['System.PullRequest.SourceBranch'], 'hotfix' )) 
  jobs:
  - job: deployabc

- stage: DeployXYZ
  condition: contains(variables['System.PullRequest.SourceBranch'], 'hotfix' )) 
  jobs:
  - job: deployxyz

- stage: DeployXYZ
  condition: not(contains(variables['System.PullRequest.SourceBranch'], 'hotfix' ))
  jobs:
  - job: deployxyz

- stage: DeployABC
  condition: not(contains(variables['System.PullRequest.SourceBranch'], 'hotfix' ))
  jobs:
  - job: deployabc

Select the order of stages to be run according to the condition.