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

**URL:** <https://discuss.circleci.com/t/can-i-design-a-workflow-so-it-can-be-triggered-both-on-push-to-branch-via-the-api/48029>\
**Category:** Pipelines\
**Tags:** circle-yml\
**Created:** [May 10, 2023, 9:44am UTC](https://discuss.circleci.com/t/can-i-design-a-workflow-so-it-can-be-triggered-both-on-push-to-branch-via-the-api/48029 "2023-05-10T09:44:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oheyadam](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/oheyadam/32/32960_2.png) [@oheyadam](https://discuss.circleci.com/u/oheyadam)\
**Post date:** [May 10, 2023, 9:44am UTC](https://discuss.circleci.com/t/can-i-design-a-workflow-so-it-can-be-triggered-both-on-push-to-branch-via-the-api/48029/1 "2023-05-10T09:44:01Z")

</div>

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!

```auto
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/

```

---

<div class="post-metadata">

**Author:** ![zmarkan](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/zmarkan/32/26505_2.png) [@zmarkan](https://discuss.circleci.com/u/zmarkan)\
**Post date:** [May 10, 2023, 10:21am UTC](https://discuss.circleci.com/t/can-i-design-a-workflow-so-it-can-be-triggered-both-on-push-to-branch-via-the-api/48029/2 "2023-05-10T10:21:51Z")

</div>

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

If so you could use [pipeline.parameters.branch](https://circleci.com/docs/variables/#pipeline-values) in the when-or block to look for a logical match. Something like:

```yaml
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](https://circleci.com/docs/configuration-reference/#logic-statements)

Hope this helps!

Edit: fixed regex syntax error

---

<div class="post-metadata">

**Author:** ![oheyadam](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/oheyadam/32/32960_2.png) [@oheyadam](https://discuss.circleci.com/u/oheyadam)\
**Post date:** [May 10, 2023, 10:59am UTC](https://discuss.circleci.com/t/can-i-design-a-workflow-so-it-can-be-triggered-both-on-push-to-branch-via-the-api/48029/3 "2023-05-10T10:59:55Z")

</div>

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?

```auto
  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/

```

---

<div class="post-metadata">

**Author:** ![zmarkan](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/zmarkan/32/26505_2.png) [@zmarkan](https://discuss.circleci.com/u/zmarkan)\
**Post date:** [May 11, 2023, 10:12am UTC](https://discuss.circleci.com/t/can-i-design-a-workflow-so-it-can-be-triggered-both-on-push-to-branch-via-the-api/48029/4 "2023-05-11T10:12:11Z")

</div>

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](https://github.com/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.

---

<div class="post-metadata">

**Author:** ![oheyadam](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/oheyadam/32/32960_2.png) [@oheyadam](https://discuss.circleci.com/u/oheyadam)\
**Post date:** [May 11, 2023, 10:53am UTC](https://discuss.circleci.com/t/can-i-design-a-workflow-so-it-can-be-triggered-both-on-push-to-branch-via-the-api/48029/5 "2023-05-11T10:53:18Z")

</div>

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
