How can I dynamically select branches for a scheduled pipeline?

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: 

  1. Create a Scheduled Pipeline (e.x. Nightly tests)
  2. Create a second scheduled pipeline for updating the Nightly tests ("Nightly test updater")
  3. 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:
    1. 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
    2. 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"]
      }
      }'
  4. From here, our Nightly tests will be updated to run on the latest branch
Was this article helpful?
1 out of 1 found this helpful

Comments

0 comments

Article is closed for comments.