Dynamically Updating a Scheduled Pipeline
Scheduled Pipelines are a useful way to regularly and automatically run jobs for a specific config and branch. With the CircleCI API, we can make this process more dynamic by automatically updating which branch to run the Scheduled Pipeline on. In this example, we are updating our Nightly tests to run the latest branch, but there are many other possible applications:
- Create a Scheduled Pipeline (e.x. Nightly tests)
- Create a second scheduled pipeline for updating the Nightly tests ("Nightly test updater")
- In the Nightly test updater, we will have a simple config that will use a custom script to get the most recently created branch. The custom script should run the following:
- Get a list of the branches for your repo, and gather the latest branch: https://stackoverflow.com/questions/5188320/how-can-i-get-a-list-of-git-branches-ordered-by-most-recent-commit
- Once we have the desired branch, we can then make an update to our Nightly tests via the API:
curl --location --request PATCH "https://circleci.com/api/v2/schedule/{schedule-id}" \
--header "circle-token: <PERSONAL_API_KEY>" \
--header "Content-Type: application/json" \
--data-raw '{
"name": "Nightly tests",
"description": "some description",
"attribution-actor": "system",
"parameters": {
"branch": "<LATEST_BRANCH>"
<additional pipeline parameters can be added here>
},
"timetable": {
"per-hour": 1,
"hours-of-day": [1],
"days-of-week": ["MON", "TUE, "WED", "THU", "FRI"]
}
}'
- From here, our Nightly tests will be updated to run on the latest branch
Comments
Article is closed for comments.