Define different workflow on different branches with workflow_dispatch

Hi there folks,

What I’m trying to achieve is to have 2 different protected branches with different workflows.

The first one has workflow foo that runs on: pull_request: branches: [ branchA ]
I want to have a similar workflow on branchB and I wouldn’t mind calling it foo as well since they perform the same function, but I understand this could be confusing so I called it bar.

The thing is, I’m adding the workflow file only to the branch it applies to, so branchA has the file for workflow foo and branchB has the file for workflow bar. I think this is the correct approach since I only want each workflow to run on that specific branch. Is this a fair assumption?

Also I want to trigger workflow bar manually so I only set on: workflow_dispatch:. But then after merging it I can’t see the action the the GitHub UI. Is there something else I need to do to add the Action properly other than commit the file? When testing bar with a PR trigger it runs fine.

Thanks in advance

@caiofaustino,

About your first question,

I think this is the correct approach since I only want each workflow to run on that specific branch. Is this a fair assumption?

Sure, you can set up and run different workflows on different branches.
However, you also need to know that some events only trigger workflow runs if the workflow files are on the default branch, such as the events repository_dispatch, check_run, check_suite, etc. . These events are not available on other branches.


About your second question,

Also I want to trigger workflow bar manually so I only set on: workflow_dispatch: . But then after merging it I can’t see the action the the GitHub UI. Is there something else I need to do to add the Action properly other than commit the file?

Normally, when the workflow file is not on the on the default branch, the workflow will not be listed under All workflows on the Actions tab, unless the workflow has been triggered at least once.

For the workflow_dispatch event, if you want to manually trigger the workflow_dispatch event on GitHub UI, the workflow file must be also on the default branch. If the workflow file only on other branches and not on the default branch, you need to use the “Create a workflow dispatch event” API to trigger the workflow_dispatch event on these branches. For more details, you can see “Manually running a workflow”.

1 Like

Thanks a lot! I’ll give this a try and come back to accept your answer as soon as I have it working :slight_smile:

Hi!

According to the docs for creating a workflow dispatch event the payload of the POST has a ref attribute that should define the source branch/tag for the workflow. However, that ref only needs to point to an existing branch, and the workflow will only be dispatched if it is present in the default branch.

I’ve created a test repo with a PR that shows this behaviour https://github.com/miguelaferreira/test-gha-workflow-dispatch/pull/1

The docs and the implementation don’t seem to align here.

1 Like