Scheduled and per commit workflows

Hi,

I found the article about triggering nightly builds however I’m unclear about how I can trigger workflows both on every commit AND on a schedule. My workflow is quite complex, do I need to duplicate the whole workflow jobs section of the yaml to achieve this?

Thanks,

Will

Yes, you’re going to need to create two workflows with the same set of jobs defined. And apply the schedule trigger to one of the workflows. But using YAML anchors should make it easier. Consider the config below.

version: 2.1

reusejobs: &reusejobs
  - build
  - build2:
      requires:
        - build

executors:
  default:
    docker:
      - image: circleci/python

jobs:
  build:
    executor: default
    steps:
      - run: sleep 10 
  build2:
    executor: default
    steps:
      - run: sleep 10 

workflows:
  example:
    jobs:
      *reusejobs
  nightly:
    jobs:
      *reusejobs
    triggers:
      - schedule:
          cron: "0 0 * * *"
          filters:
            branches:
              only:
                - master

We defined the jobs in a YAML anchor called reusejobs, and we can reuse it in both jobs step of the workflows. You can see a working example here. There may be some instances where this won’t work, depending on how complex your workflow is. But it’s the only way i’m aware of at the moment. Otherwise you’ll need to simply duplicate the jobs for the scheduled and non scheduled workflow.