Can I design a workflow so it can be triggered both on push to branch & via the API?

Hey folks, quick question about workflow triggers: we have this workflow, and as it’s currently designed, it’s triggered when a pipeline parameter is true and only on branches that match the specified regex. However, I want it to run when the param is true or when someone pushes to a branch that matches the regex–is this doable? See code below, thanks!

upload_to_google_play_workflow:
    when: << pipeline.parameters.run_workflow_upload_to_google_play >>
    jobs:
      - upload_prod_release_to_google_play:
          filters:
            branches:
              only:
                - /^release\/v*.*.0/

Hi Adam, is the intention to trigger the whole workflowm, and not this specific branch?

If so you could use pipeline.parameters.branch in the when-or block to look for a logical match. Something like:

upload_to_google_play_workflow:
  when:
      or: 
        -  << pipeline.parameters.run_workflow_upload_to_google_play >>
        - matches: { pattern: "^release/v*.*.0", value: << pipeline.git.branch >> }
  jobs:
      - upload_prod_release_to_google_play

More on logic statements: https://circleci.com/docs/configuration-reference/#logic-statements

Hope this helps!

Edit: fixed regex syntax error

Hey Zan, ideally we’d trigger the workflow via the parameter, but only when triggered on a release/ branch, and when someone pushes to a release/ branch. Does this look right to you?

  upload_release_to_google_play_workflow:
    when:
      or:
        - << pipeline.parameters.run_workflow_upload_release_to_google_play >>
        - matches: { pattern: "/^release\/v*.*.0/",  value: << pipeline.git.branch >> }
    jobs:
      - upload_prod_release_to_google_play:
          context: aws
          filters:
            branches:
              only:
                - /^release\/v*.*.0/

How do you intend to trigger the workflow & toggle the parameter? Using the API or some other way? If using the API you need to specify what branch you’d like to trigger on anyway and that wouldn’t require you filtering any further on the job level.

BTW my initial script was wrong - I got the regex wrong, updated the sample now.

I made a quick POC to test it out: GitHub - zmarkan-demos/cicd-trigger-and-filter

You see 3 distinct flows:

  • push to main skips the release workflow (filtered out)
  • push to release/v0.1.0 runs the release workflow
  • API call to trigger runs the release workflow if the pipeline param is set.

Thinking about it again, if you trigger only on release/v*.*.0 branches, then the trigger parameter for run_workflow_upload_release_to_google_play would also be redundant.

Oh yea good point about the API, and yep, caught the regex issue and fixed it on my end as well. I ended up with something very similar to your PoC. Thanks a bunch, Zan! Cheers