Start Building for Free
CircleCI.comAcademyBlogCommunitySupport

Schedule pipelines with multiple workflows

7 months ago1 min read
Cloud
On This Page

Scheduled pipelines are simply triggered pipelines, and by default, for each trigger, every workflow that has been configured will run. You can filter workflows in your .circleci/config.yml file with conditionals. The conditionals you set will control which workflows will run when a scheduled pipeline is triggered. These conditions can be set with built-in pipeline values, or through custom pipeline parameters. The examples below demonstrate various ways to set conditionals in your .circleci/config.yml file.

Schedule using built-in pipeline values

One way to implement workflows filtering is by using pipeline values. The example below uses the built-in pipeline values pipeline.trigger_source and pipeline.schedule.name. The examples use daily_build and nightly_build as the pipeline.schedule.name, however, the value of pipeline.schedule.name can be whatever you would like when using scheduled pipelines.

version: 2.1
...
daily-run-workflow:
# run workflow only when the daily_build pipeline is triggered
  when:
    and:
      - equal: [ scheduled_pipeline, << pipeline.trigger_source >> ]
      - equal: [ daily_build, << pipeline.schedule.name >> ]
  jobs:
    - test
    - build

nightly-run-workflow:
# run workflow only when the nightly_build pipeline is triggered
  when:
    and:
      - equal: [ scheduled_pipeline, << pipeline.trigger_source >> ]
      - equal: [ nightly_build, << pipeline.schedule.name >> ]
  jobs:
    - build
    - deploy
...

Note that in the above example, the second equal under when is not strictly necessary. The pipeline.schedule.name is an available pipeline value when the pipeline is triggered by a schedule.

You may also add filtering for workflows that should not run when a schedule is triggered:

version: 2.1
...
daily-run-workflow:
# run workflow only when the daily_build pipeline is triggered
  when:
    and:
      - equal: [ scheduled_pipeline, << pipeline.trigger_source >> ]
      - equal: [ daily_build, << pipeline.schedule.name >> ]
  jobs:
    - test
    - build

nightly-run-workflow:
# do NOT run workflow if a scheduled pipeline is triggered
  when:
    not:
      equal: [ scheduled_pipeline, << pipeline.trigger_source >> ]
  jobs:
   - build
   - deploy
...

For a full list of pipeline values, visit the Pipeline values and parameters page.

Schedule using pipeline parameters

In the example below, a parameter is created called run-schedule, which is set as type: boolean and default: false. This allows the workflows section to use when to specify conditionals about when the pipeline should run. If you use the when conditional, you will also need to set a when: not:, like in the example below.

version: 2.1
...
# set pipeline parameters
parameters:
  run-schedule:
    type: boolean
    default: false

workflows:
  # do not run the scheduled pipeline if build-test-deploy
  build-test-deploy:
    when:
      not: << pipeline.parameters.run-schedule >>
    jobs:
      - test
      - build
  # run the scheduled pipeline if nightly-snapshot
  nightly-snapshot:
    when: << pipeline.parameters.run-schedule >>
    jobs:
      - build
      - deploy
...

Set up multiple workflows with multiple schedules

The example below demonstrates three workflows scheduled with pipeline values. Two workflows will run on the same schedule (daily_build), and one will run on a different schedule (weekly_build).

version: 2.1
...
workflows:
# run workflow only when the daily_build pipeline is triggered
  daily-run-workflow:
    when:
      equal: [ daily_build, << pipeline.schedule.name >> ]
    jobs:
      - job-one

  nightly-run-workflow:
  # run workflow only when the daily_build pipeline is triggered
    when:
      equal: [ daily_build, << pipeline.schedule.name >> ]
    jobs:
      - job-two

  weekly-run-workflow:
  # run workflow only when the weekly_build pipeline is triggered
    when:
      equal: [ weekly_build, << pipeline.schedule.name >> ]
    jobs:
      - job-three
...

Suggest an edit to this page

Make a contribution
Learn how to contribute