How to repeat workflow config in multiple trigger types without excessive duplication?


#1

Hi,
I would like to run a workflow on a cron (like once a week) but also have that workflow be the default for the project (so it can trigger off of commits from github). Is there a way to do this without defining 2 workflows with the same jobs inside. I’m trying to avoid copying the jobs section of my config.yml files.

Thanks,
Jake


#2

Yep, I do this. Here’s the trigger section of one of my configs, containing two schedule triggers and one commit trigger:


#3
version: 2
jobs:
  checkout_code:
  test:
  build_image:
  deploy:
  deploy_prod:
workflows:
  version: 2
  build_and_deploy:
    triggers:
      - schedule:
          cron: "0 11 * * 1"
          filters:
            branches:
              only:
                - master
    jobs:
      - checkout_code
      - test:
          requires:
            - checkout_code
      - build_image:
          requires:
            - test_entitled
      - deploy:
          requires:
            - build_image

#4

Unfortunately, this requires a big chunk of duplication in the circle file in the workflows section. I would like to avoid that. I also thought to trigger the cron build via http, but that isn’t supported yet with workflows.


#5

One thing you can do here is to create your config file using a templating approach. I do that for one of my docker-compose.yml files, and it works very well. My templating system starts with a header of:

#
# Do not edit this file - edit the files in /config/* and
# run template.sh instead
#

Then you can DRY up your actual config files, and write out a templated version with all manner of machine-generated duplication. Of course, you have to commit the rewritten config file to your repo, but that’s not much of a bother.


#6

Hi,
I set a reminder for myself to checkin on this in a month. Is there any update on the CircleCI 2.0 API. I would like to trigger this build from a cron job on one of my servers.

Thanks,
Jake


#7

I still think you should use the built-in CircleCI scheduler. Using an external server to avoid a trivial amount of config duplication is swapping a small amount of work for a bigger one.

However, I don’t know why I didn’t mention YAML references before - would that help? It’s a YAML feature rather than a CircleCI specific one, but if your duplicate material is the same in all cases then it should work.


#8

Hi,
I appreciate the suggestion, I will take a look at YAML references and see how it goes.

Thanks,
Jake


#9

Hello Everyone,

@jakepearson I had the same problem but ended up using an anchor to prevent duplication of job workflows like this:

version: 2

jobs:
  checkout_code:
  test:
  build_image:
  deploy:
  deploy_prod:

default_jobs: &default_jobs:
   jobs:
      - checkout_code
      - test:
          requires:
            - checkout_code
      - build_image:
          requires:
            - test_entitled
      - deploy:
          requires:
            - build_image

workflows:
  version: 2
  build_and_deploy:
    <<: *default_jobs

  recurring_build_and_deploy:
    triggers:
      - schedule:
          cron: "0 11 * * 1"
          filters:
            branches:
              only:
                - master
    <<: *default_jobs

I hope someone finds this useful!


#10

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.