Schedule a build in Azure DevOps using CRON Expressions

This article helps you with an easy way to trigger a build or pipeline using Cron Expression or Jobs in Azure DevOps.

Schedule a build in Azure DevOps using CRON Expressions
Schedule a build in Azure DevOps using CRON Expressions

Dive into the dynamic world of Azure DevOps pipelines, where triggering actions is just the beginning.

Picture this: You're crafting pipelines with precision, and suddenly the need arises to schedule them seamlessly.

Enter CRON expressions—the ultimate solution for streamlining your pipeline execution on a defined timetable.

This blog post unveils the power of CRON expressions, guiding you through the process of effortlessly scheduling builds and pipelines in Azure DevOps. Say goodbye to complexity and hello to efficiency as you master the art of timed triggers.

Can you schedule Azure DevOps Pipelines?

Yes! You can easily schedule them using a CRON expression as the trigger in your pipeline file.

What is CRON?

CRON is a command line utility that is used to schedule jobs. You'll hear it referred to as CRON jobs or CRON tasks.

The CRON syntax can sometimes be confusing when first encountered. Five sections can be configured within the CRON syntax. You can specify the day of the week, month, day of the month, hour and minute.

This isn't something you need to commit to memory, but being able to read the syntax is useful. Below is a great diagram to show you how the syntax is broken down.

# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of the week (0 - 6)
# │ │ │ │ │                       
# │ │ │ │ │
# │ │ │ │ │
# * * * * * <command to execute>

I love to use the website https://crontab.guru/. You can put the syntax into the website, and it will decipher what that schedule would do, or equally, you can use it to build the right syntax for the schedule you want to execute.

💡
I also have a YouTube channel! Do not forget to subscribe here.

What is the Azure DevOps CRON syntax?

I want to show you an example of an Azure DevOps Pipeline that runs on a schedule.

trigger:
# YAML file in the release branch
schedules:
- cron: "0 0 * * 1-5"
  displayName: Daily build at midnight (Monday-Friday)
  branches:
    include:
    - main

The above is a part of my pipeline that will trigger at midnight every Monday, Tuesday, Wednesday, Thursday and Friday and complete what the pipeline is designed to build.

You should notice that it will open a trigger for the main branch, not any other branches. This allows for granular control over different branches and your needs.

💡
It's important to note that you encapsulate the CRON schedule in double quotes within Azure DevOps. With other platforms, such as GitHub Actions you use single quotes.

The full pipeline looks like this:

# Basic pipeline that runs at midnight every day of the working week for the main branch. 

trigger:
# YAML file in the release branch
schedules:
- cron: "0 0 * * 1-5"
  displayName: Daily build at midnight (Monday-Friday)
  branches:
    include:
    - main

pool:
  vmImage: 'windows-latest'
  demands:
  - msbuild
  - visualstudio

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: DotNetCoreCLI@2
  displayName: Restore
  inputs:
    command: restore
    projects: '**/*.csproj'

- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    projects: '**/*.csproj'
    arguments: '--configuration $(BuildConfiguration)'

- task: DotNetCoreCLI@2
  displayName: Publish
  inputs:
    command: publish
    publishWebProjects: false
    projects: '$(Build.SourcesDirectory)\dotnet-core-tutorial.csproj'
    arguments: --configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)\output
    zipAfterPublish: false