Sar
Sar

Reputation: 299

Deselect Stages By Default

In Azure Devops multistage YAML pipeline we got multiple environments. In stages to run normally we do a build and deploy only in QA, so we need to deselect each stage manually. By default all stages are selected is it possible to have exact opposite, where all stages are deselected by default???

select stages

trigger: none
pr: none
stages:
- stage: 'Build'
  jobs:
  - deployment: 'Build'
    pool:
      name: Default
# testing
    environment: INT
    strategy:
      runOnce:
        deploy:
          steps:
          - checkout: none
          - powershell: |
              echo "Hello Testing"
              Start-Sleep -Seconds 10
- stage: 'Sandbox'
  jobs:
  - job: 'Sandbox'
    pool:
      name: Default
    steps:
    - checkout: none
# testing
    - powershell: |
        echo "Hello Testing"
- stage: 'Test'
  jobs:
  - job: 'DEV'
    pool:
      name: Default
    steps:
    - checkout: none
    - powershell: |
        echo "Hello Testing"
- stage: 'QA'
  dependsOn: ['Test','Test1','Test2']
  jobs:
  - job: 'QA'
    pool:
      name: Default
    steps:
    - checkout: none
      # Testing
    - powershell: |
        echo "Hello Testing"

Upvotes: 15

Views: 6465

Answers (2)

realbart
realbart

Reputation: 3994

I've used this solution to build a nuget-package, and:

  • always push packages from master
  • conditionally push packages from other branches

Using GitVersion ensures that the packages from other branches get prerelease version numbers, e.g. 2.2.12-my-branch-name.3 or 2.2.12-PullRequest7803.4. The main branch simply gets 2.2.12, so the master branch is recognized as a "regular" version.

The reason I'm repeating the answer above, is that I chose to make the stage conditional instead of using an if:

trigger:
  - master

parameters:
- name: pushPackage
  displayName: Push the NuGet package
  type: boolean
  default: false

stages:
  - stage: Build
    jobs:
    - job: DoBuild
      steps:
      - script: echo "I'm building a NuGet package (versioned with GitVersion)"

  - stage: Push
    condition: and(succeeded('build'), or(eq('${{ parameters.pushPackage }}', true), eq(variables['build.sourceBranch'], 'refs/heads/master')))
    jobs:
    - job: DoPush
      steps:
      - script: echo "I'm pushing the NuGet package"

Like the other answer, this results in a dialog:

Run pipeline - conditional 'Push to NuGet feed

But what's different from the (equally valid) solution with '${{ if }}', is that the stage is always shown (even if it's skipped):

stage - skipped

Upvotes: 4

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35544

I am afraid that there is no UI (like stage to run) method that can meet your needs.

You could try to add parameters to your Yaml Sample.

Here is an example:

trigger: none
pr: none

parameters:
- name: stageTest
  displayName: Run Stage test
  type: boolean
  default: false
- name: stageBuild
  displayName: Run Stage build
  type: boolean
  default: false

stages:
- ${{ if eq(parameters.stageBuild, true) }}: 
  - stage: 'Build'
    jobs:
    - deployment: 'Build'
      pool:
        name: Default
      environment: INT
      strategy:
        runOnce:
          deploy:
            steps:
            - checkout: none
            - powershell: |
                echo "Hello Testing"
                Start-Sleep -Seconds 10

- ${{ if eq(parameters.stageTest, true) }}: 
  - stage: Test
    dependsOn: []
    jobs:
    - job: B1
      steps:
      - script: echo "B1"

The parameters are used to determine whether to run these stages. You could add expressions before the stage to check if the parameter value could meet expression.

The default value is false. This means that the stage will not run by default.

Here is the result:

enter image description here

You can select the stage you need to run by clicking the selection box.

Update

Workaround has some limitations. When the select stage has depenencies, you need to select all dependent stages to run.

For example:

  - stage: 'QA'
    dependsOn: ['Test','Test1','Test2']

enter image description here

On the other hand, I have created a suggestion ticket to report this feature request. Here is the suggestion ticket link: Pipeline Deselect Stages By Default You could vote and add comment in it .

Upvotes: 12

Related Questions