azure-devopscroncontinuous-integrationcontinuous-deploymentazure-pipelines-yaml

Azure YAML Pipeline schedule to run on the last Sunday of the month at 4 AM


I want my Azure YAML pipeline to run on every last Sunday of the month at 4 AM. I currently have the following cronjob/schedule in my pipeline: current cron job

But this one runs every Sunday at 4 AM, which is way too often.

I have tried a couple of cronjobs already but none of them seem to do what I want:

I used Crontab.guru to check when these jobs will run. For example, 0 4 25-31 1-12/2 Sun, generates the following text: Crontab.guru description

To me it sounds like it will run based on two conditions:

Is it even possible? Or does it require any scripting to implement this in an Azure YAML Pipeline?

Thank you!


Solution

  • Cron syntax does not support “last Sunday of the month” directly. You can add a script at the start of your pipeline that checks if today is the last Sunday of the month. If not, it exits the pipeline early. Here’s an example for your reference:

    trigger:
    - none
    
    schedules:
    - cron: "0 4 * * Sun"
      displayName: Monthly Pipeline Run
      branches:
        include:
        - main
      always: true
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
    - script: |
        # Get the last day of the month
        last_day=$(date -d "$(date +'%Y%m01') +1 month -1 day" "+%d")
        # Get today's date
        today=$(date +'%d')
        # Check if today is the last Sunday of the month
        if [ $((10#$today + 7)) -gt $((10#$last_day)) ]; then
          echo "Today is the last Sunday of the month. Continue with the pipeline."
          exit 0
        else
          echo "Today is not the last Sunday of the month. Stop the pipeline."
          exit 1
        fi
      displayName: 'Check if today is the last Sunday of the month'
    

    The pipeline will be triggered every Sunday at 4:00 AM UTC, but the steps you define will only be run on the last Sunday of each month.