Can you filter on a workflow level?

Is it possible to do branch-level filtering for a workflow rather than the individual jobs in a workflow?

My current workflow looks like this:

workflows:

  test_and_deploy:
    jobs:
      - run_bare_tests

      - deploy_staging:
          requires:
            - run_bare_tests
          filters:
            branches:
              only: staging

      - qa_staging:
          requires:
            - deploy_staging
          # FILTERING IS REPEATED AND HARD TO READ
          filters:
            branches:
              only: staging

      - merge_staging_to_master:
          requires:
            - qa_staging
          # FILTERING IS REPEATED AND HARD TO READ
          filters:
            branches:
              only: staging

      - deploy_master:
          requires:
            - merge_staging_to_master
          filters:
            branches:
              only: master

But I would ideally like it to look like this:

workflows:

  test_and_deploy_staging:
    # NOTE THE FILTERING HERE AND DECLARED JUST ONCE
    filters:
      branches:
        only: staging
    jobs:
      - run_bare_tests
      - deploy_staging:
          requires:
            - run_bare_tests
            
      - qa_staging:
          requires:
            - deploy_staging
            
      - merge_staging_to_master:
          requires:
            - qa_staging

  # EACH WORKFLOW IS MUCH EASIER TO READ
  test_and_deploy_master:
    filters:
      branches:
        only: master
      jobs:
        - run_bare_tests
        - deploy_master:
            requires:
              - run_bare_tests

Is this possible? I assumed that workflows were going to be different ways of setting up workflows for different scenarios. Without the ability to filter on a workflow level though I get the impression that I should really only think of there as being one workflow.

This single-workflow and per-job filtering syntax is much harder to read. Is there something I’m missing?

Thank you,
Peter

1 Like

I don’t believe you can, no. However, you should be able to do a half-way house - use YAML references to apply the one block to all sub-blocks. There is a good demo on this, of all places, in the Wikipedia article for YAML.

1 Like

nice suggestions thank you

1 Like

No worries. If you get this working, I’d be interested in seeing it. I’ve only done a bit of YAML referencing, and it seems to be mostly voodoo :joy: - you can do interesting stuff like defining a block, using it, and then overwriting keys individually.

All working as expected, good suggestion. At the top of my circle.yml file I put this line

staging_only: &staging_only
  filters:
    branches:
      only: staging

And then the workflows look like this:


  test_and_deploy:
    jobs:
      - run_simulated_tests

      - deploy_dummy:
          requires:
            - run_simulated_tests

      - qa_dummy:
          <<: *staging_only
          requires:
            - deploy_dummy


      - merge_dummy_to_master:
          <<: *staging_only
          requires:
            - qa_dummy

The last two jobs only now run on the staging branch. :facepunch:t2:

5 Likes

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