简体   繁体   中英

how to run different steps in a yml file depending on the branch trigger

How can I make the YAML file to trigger and check out a branch and run diferrent steps?

I am working on a YAML file for azure, and I want run run certain step in my master branch and other steps in a QA branch.

trigger:
- master
pool:
  vmImage: 'Ubuntu-16.04'
steps:
- script: ls $(Build.Repository.LocalPath)
  displayName: 'printing the ls

I want to check out the master and run a step, but if something change on QA branch I want to trigger, checkout the QA branch and run other steps. What should the YAML look like?

On each step you can put a condition: to each task/script:

 condition: and(succeeded(), and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), ne(variables['Build.Reason'], 'PullRequest')))

This will trigger the task for master branch build, except when the build was triggered for pull request validation. A complete example:

task: SnykTask@1
  condition: and(succeeded(), and(eq(variables['Build.SourceBranch'], 'refs/heads/master'), ne(variables['Build.Reason'], 'PullRequest')))
  displayName: 'Dependency scan'
  inputs:
    file: xxxxx
    test: true
    monitor: false
    authType: endpoint
    endpoint: xxx
    severityThreshold: high
    failBuild: false

You can also define a stage in your yaml file. Stages can contain a set of steps and can also be made conditional:

stages: 
- stage: string # name of the stage, A-Z, a-z, 0-9, and underscore 
    displayName: string # friendly name to display in the UI 
    dependsOn: string | [ string ] 
    condition: string variables: { string: string } | [ variable | variableReference ] 
    jobs: [ job | templateReference]

In the most extreme case you can create multiple yaml files and commit them to source control. Then go into the Azure Pipelines UI and create a pipeline for each yaml file. To make them fully seperate.

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM