Rattletrap
Rattletrap

Reputation: 113

Azure Devops - One scheduled pipeline for each branch in the repo

Is it possible to use one pipeline and schedule it for all branches in the repo that begins with "release"?

My scenario:
One repo - TestRepo.
Multiple release branches for TestRepo: release/alpha, release/beta, release/charlie etc.

At any given point I will have multiple release branches existing.

I have a working workaround I use right now, using the YAML file below, where I set the included branch to release/X and update that for each branch. So each branch will have its own YAML file and its own pipeline. In addition to that I also have to change the default branch in the classic editor for each pipeline to use the new release branch.

I have looked at the part concerning branches here but that didn't work for me (as in the "scheduled runs" view only took one branch into account): https://learn.microsoft.com/en-us/azure/devops/pipelines/process/scheduled-triggers?view=azure-devops&tabs=yaml#branch-considerations-for-scheduled-triggers

I appreciate any help, thank you.

trigger: none
pr: none
pool:
  name: <REDACTED>
  demands:
  - agent.name -equals  <REDACTED>

schedules:
- cron: '0 0 * * *' # The time zone for cron schedules is UTC and Sweden is UTC+1.
  displayName: Daily nightly build for Sonarqube (code coverage %, general statistics and the dependency-check report)
  branches:
    include:
    - release/alpha # This needs to be updated for every release branch.
  always: true
  
stages:
- stage: InstallJava
  jobs:
  
    - job:
      workspace:
       clean: all
      steps:
      - task: CmdLine@2
        inputs:
            script: |
              dir
              pwd
              mvn -version
              java -version
              export JAVA_HOME='/usr/lib/jvm/java-11-openjdk-11.0.21.0.9-1.el7_9.x86_64'
          
      - task: Maven@4
        inputs:
          mavenPomFile: 'pom.xml'
          goals: 'dependency-check:check'
          publishJUnitResults: false
          javaHomeOption: 'path'
          jdkDirectory: '/usr/lib/jvm/java-11-openjdk'
          mavenVersionOption: 'Default'
          mavenAuthenticateFeed: false
          effectivePomSkip: false
          sonarQubeRunAnalysis: false
        
      - task: CmdLine@2
        inputs:
            script: |
              dir
              pwd
      - task: SonarQubePrepare@5
        inputs:
          SonarQube: <REDACTED>
          scannerMode: 'Other'
          extraProperties: |
            sonar.branch.name=$(Build.SourceBranchName)
      - task: Maven@4
        inputs:
          mavenPomFile: 'pom.xml'
          goals: 'clean install'
          publishJUnitResults: false
          javaHomeOption: 'path'
          codeCoverageToolOption: 'JaCoCo'
          jdkDirectory: '/usr/lib/jvm/java-11-openjdk'
          mavenVersionOption: 'Default'
          mavenAuthenticateFeed: false
          effectivePomSkip: false
          sonarQubeRunAnalysis: true
      - task: SonarQubePublish@5
        inputs:
          pollingTimeoutSec: '300'

Upvotes: 1

Views: 3218

Answers (2)

Ziyang Liu-MSFT
Ziyang Liu-MSFT

Reputation: 5296

You don't need to create a separate pipeline for each branch, one pipeline is enough. But you need to have the same yaml file in each branch, and in each yaml file, change the included branch to the current branch. Refer to the sample below.

The pipeline is created based on azure-pipelines.yml.

The azure-pipelines.yml in main branch. The included branch is main.

schedules:
- cron: '0 */6 * * *'
  displayName: Trigger every 6 hours
  branches:
    include:
    - main
  always: true

The azure-pipelines.yml in release/a branch. The included branch is release/a.

schedules:
- cron: '0 */6 * * *'
  displayName: Trigger every 6 hours
  branches:
    include:
    - release/a
  always: true

Go to your pipeline, click More actions -> Scheduled runs.

enter image description here

You can see the scheduled runs as shown below: enter image description here

Upvotes: 2

Scott Richards
Scott Richards

Reputation: 766

You can define multiple cron schedules in yaml like so:

trigger: none
pr: none

schedules:
- cron: '*/1 * * * *'
  displayName: Trigger every 1 minutes test
  branches:
    include:
    - main
  always: true

- cron: '*/2 * * * *'
  displayName: Trigger every 2 minutes test
  branches:
    include:
    - testSchedule
  always: true
  
steps:
- script: echo Hello world!
  displayName: Say hello

I have tested this, and there are a few caveats to be aware of when setting it up.

  • Caveat #1: You must not have configured scheduled runs via the classic user interface - this will overwrite the yaml configuration. The classic configuration menu is hidden behind multiple menus. To navigate there, select Edit on your pipeline, then choose the menu and click Triggers. On this page is a section named Scheduled. If any schedules exist here, you must first delete them. You will need to Sync schedules from the Schedules runs screen to get the pipeline to read schedules from yaml if these did exist (see instructions at bottom of this message). If you prefer to manage schedules here instead of yaml, you could also do that, but I would recommend sticking to yaml.

  • Caveat #2: Each branch will need a fresh commit once the pipeline has been created for it to be detected in the schedule.

  • Caveat #3: Each branch should include the yaml file with the same schedule configuration in order for it to be triggered (technically it does not need to same configuration, but it should at least include itself). Read Microsoft docs on configuring schedules for more details.

You can view scheduled runs by selecting the menu and clicking Scheduled runs. There is also a Sync schedules button here to get the pipeline to use schedules from yaml instead of the classic UI.

If configured with multiple branches, it will look something like this: picture showing multiple branches scheduled.

Upvotes: 1

Related Questions