Scheduled Tasks
About 668 wordsAbout 2 min
Introduction
Pipelines are typically triggered actively, such as through Git operations, page interactions, or API calls. They can also be configured as scheduled tasks to run at specified times.
Adding Scheduled Tasks
Below is an example configuration of a scheduled task pipeline.
# Scheduled task pipeline configuration on the main branch
# When the scheduled task is triggered, it fetches the code from the main branch for building
main:
# User A submits the scheduled task pipeline configuration, with A as the triggerer when the scheduled task is triggered
"crontab: 30 5,17 * * *": # Scheduled task triggers according to the configured time
- name: crontab-a
stages:
- name: test
script: echo 1
Configuration Breakdown
This section will explain in detail the format and meaning of each part of the scheduled pipeline configuration.
main
Similar to regular pipelines, you can specify an existing branch in the repository. This indicates the code to be used when the scheduled task is executed. For example, if set to main
, the code at the HEAD
of the main
branch will be used when the pipeline runs.
To improve resource utilization, scheduled task branch names do not support
glob
expressions and must be explicit branch names.
crontab: 30 5,17 * * *
In contrast to other pipelines triggered by specific events like push
, web_trigger
, or api_trigger
, scheduled tasks are triggered based on a cron
rule. The format for specifying the trigger time is crontab: ${CRON}
.
crontab:
is a fixed prefix for scheduled pipeline events.${CRON}
represents the actual execution time in standardPOSIX cron
syntax.
For example, the above configuration will trigger execution twice daily at 5:30 and 17:30 in the Asia/Shanghai
time zone.
When a scheduled task pipeline runs, the person responsible for the scheduled task configuration (addition or modification) is considered the triggerer of the pipeline.
To provide a more reliable service, we restrict the minimum time interval for scheduled tasks to 5 minutes. Scheduled tasks with intervals less than 5 minutes (e.g.,
* * * * *
) cannot be successfully added.
Modifying Scheduled Tasks
Modifying a scheduled task configuration involves changing three main parts:
- Trigger time: e.g.,
30 5,17 * * *
->0 9 * * *
, changing the trigger time. - Pipeline configuration: modifying
stages
andenv
contents, etc. - Responsible person: changing the operator of the scheduled task, who is considered the triggerer of the pipeline when the scheduled task runs.
main:
# User B updated the scheduled task pipeline configuration, with B as the triggerer when the scheduled task is triggered
"crontab: 30 5,17 * * *": # Modified the execution time, scheduled task triggers according to the new time
- name: crontab-b
stages:
- name: test
script: echo 2
If the person responsible for a scheduled task is removed from the repository, the scheduled task pipeline will fail. You can update the responsible person by modifying the scheduled task configuration and pushing it again.
Deleting Scheduled Tasks
Editing the pipeline configuration file under a branch to correspond to a different cron
expression effectively deletes the original scheduled task and adds a new one. If there are no cron
expressions in the pipeline configuration file, it means all scheduled tasks under that branch are deleted.
main:
# Deleted the scheduled task mentioned earlier
When a remote repository branch is deleted, all scheduled tasks under that branch are also deleted. Repository deletion removes all scheduled tasks within it.
Comparison
Using a push
event as an example, the table below compares scheduled tasks and push
events, helping you understand the differences between the two.
Feature | Scheduled Tasks | Push Events |
---|---|---|
Trigger | Based on time cron rule | Triggered by specific operations (e.g., code push) |
Branch Config | Uses explicit branch names | Supports glob expressions for branch names |
Responsible Person | Triggered by the task configurator | Triggered by the event initiator |
Minimum Interval | Minimum interval for task scheduling is 5 minutes | No specific minimum interval requirement |
Modification | Modify trigger time, pipeline configuration, responsible person | Directly modify pipeline configuration |
Deletion | Modify cron expression to delete or add tasks | Deleting pipeline configuration removes the pipeline |