Take Control of Your Bitbucket Pipeline Schedule with Cron

Rajan Tiwari
2 min readOct 13, 2023

--

If you are an Atlassian stack, chances are you are using Bitbucket Pipeline Scehdules to perform timely cron task.

Schedules in Bitbucket provide following options for Scheduling the Pipeline.

To learn more about how to create schedules please following this link
Scheduled and manually triggered pipelines

If you use Bitbucket Pipelines, you may be frustrated by the limited scheduling options available: hourly, daily, or weekly. What if you need to run a pipeline every 30 minutes, or once a month?

There should have been a way right ?

Fortunately, there is a workaround that is not documented in the Bitbucket documentation. You can use the Bitbucket Pipelines API to create custom cron schedules.

Creating a custom cron schedule

To create a custom cron schedule, you will need to use the Bitbucket Pipelines API. You can use the following payload:

{
"target": {
"type": "pipeline_ref_target",
"selector": {
"type": "custom",
"pattern": "<PIPELINE_NAME>"
},
"ref_name": "<BRANCH_NAME>",
"ref_type": "branch"
},
"cron_pattern": "CRONTIME",
"enabled": true
}

Replace the following values:

  • <PIPELINE_NAME> with the name of the pipeline you want to schedule.
  • <BRANCH_NAME> with the name of the branch where the pipeline is defined.
  • CRONTIME with the cron expression for the desired schedule.

if you notice the API doc, it says

so the expression needs to be 7 field long with second precision.

For example, the following payload would create a schedule to run the my-pipeline pipeline every 15 minutes: `0 */15 * * * ? *`

{
"target": {
"type": "pipeline_ref_target",
"selector": {
"type": "custom",
"pattern": "my-pipeline"
},
"ref_name": "master",
"ref_type": "branch"
},
"cron_pattern": "0 */15 * * * ? *",
"enabled": true
}

Cron expression syntax

A cron expression is a string that defines a schedule for a job or task. It consists of seven fields, which represent the following:

  • Seconds (0–59)
  • Minutes (0–59)
  • Hours (0–23)
  • Day of the month (1–31)
  • Month (1–12)
  • Day of the week (0–7, where 0 and 7 represent Sunday)
  • Year (0–11)

The * symbol in the hours, day of the month, and month fields means that the cron expression will match any value for those fields. The ? symbol in the day of the week field is used to specify that no particular value is expected

Ref:

https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-config-schedules-post

Cron Expressions (oracle.com)

https://support.atlassian.com/bitbucket-cloud/docs/pipeline-triggers/

--

--