You can trigger Azure DevOps pipelines to run on a number of different events.
When building pipelines, there maybe a case when you want to run them on a defined schedule. This is where CRON expressions come in handy.
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 CRON jobs or CRON tasks.
The CRON syntax can sometimes be confusing when first encountered. There are five sections that can be configured within the CRON syntax. You can specify day of 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 in to 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.
What is the Azure DevOps CRON syntax?
I want to show you an example of a 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.
What you should notice is it will open trigger for the main branch, not any other branches. This allows for granular control over different branches and the needs you might have.
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