Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to differentiate between branch trigger and the scheduled trigger in azure DevOps build pipeline

I want to run different jobs in build pipeline for branch trigger and scheduled trigger.

branch trigger => run job 1

scheduled trigger => run job 2

Is there any way to differentiate between the triggers? So that i will be running my jobs according to that differentiating condition.

My thought process

I was thinking of setting variable during scheduled trigger, hence i could use that variable in the job condition evaluation. But I was not able set the variable.

# Sample azure-build-pipeline.yml file


variables:

# by default the variable is false
  isScheduledTrigger: false


trigger:
  - develop
  - master

schedules:
  - cron: "0 0 * * *"
    displayName: Daily midnight build
    branches:
      include:
        - develop
    always: true
# somewhere here i want to set the isScheduledTrigger variable to TRUE

jobs:
 - job: Branch trigger job
   condition: or(eq(variables['Build.SourceBranchName'], 'develop'),eq(variables['Build.SourceBranchName'], 'master'))
   steps:
# Multiple steps for branch trigger



- job: Scheduled trigger job
   condition: and(eq(variables['Build.SourceBranchName'], 'develop'),eq(variables['isScheduledTask'], True))
   steps:
# Multiple steps for scheduled trigger


like image 319
DOJI Avatar asked Dec 17 '25 19:12

DOJI


1 Answers

You can differentiate the type of Trigger using the variable named Reason

condition: and(succeeded(), and(not(eq(variables['Build.Reason'], 'PullRequest')), not(eq(variables['Build.Reason'], 'Schedule'))))
like image 172
Sajeetharan Avatar answered Dec 20 '25 11:12

Sajeetharan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!