How to trigger repository_dispatch event for non-default branch?

The Github Actions documentation mentions the repository_dispatch event in the External Events section. The table in this sections says that the GITHUB_REF points to the “Branch that received dispatch”. My question now is: How can I specify the branch when sending the repository_dispatch event? The API documentation does not mention any branch related input argument.

Thanks!

14 Likes

Hi @phil-opp,

Thank you for being here! I wish I had better news for you, the repository_dispatch only triggers on the master branch, and it is not possible to specify a branch. We have received several pieces of feedback about this limitation and have an internal issue tracking a feature request, to which I’ve added your feedback. 

5 Likes

Thanks for clarifying! Perhaps the documentation at https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#external-events-repository_dispatch should be updated to reflect this (the GITHUB_REF column says “Branch that received dispatch” instead of “default branch”).

5 Likes

Also, when the documentation links “Webhook Event Payload: repository_dispatch” through to the repositorydispatchevent type on the v3 version of the documentation, it makes it seem like I can use the payload body there, but trying to use branch gives:

{
  "message": "Invalid request.\n\n\"branch\" is not a permitted key.",
  "documentation_url": "https://developer.github.com/v3"
}

Both are a bit confusing, would be good to update if only default branch is possible.

1 Like

My problem with the repository dispatch is that it needs a token with repo permission. Isn’t it possible to severely limit the scope of the token in order to avoid consequences if it gets compromised?

1 Like

This seems like a different problem, so it’s probably better to create a new topic for it.

Since we need this a lot…

two questions:

  • any news on this since last November?
  • could I mimic the branch by passing it’s name in the client_payload and switching to that branch in my action script? (will this build be registered by github as a build on  master or on the requested branch?)
  • is there any other webhook I could (mis)-use to signal a rebuild on a certain branch?

Our situation is as follows:

  • repository “lib” contains our library and publishes a package
  • repository “app” uses the published package to make our app
  • we use synchronous branches on both repositories
  • regularly we push to both repos at the same time after bumping the version number of the package (in lib) and its use (in app).
  • because they both start building at the same time the app will not find the new version of the lib and it fails.
  • we would like the lib to re-trigger the app as soon as a new lib-package is published, which means the second build of app would find the indicated version of the lib and will succeed
  • currently we have to manually baby-sit the app-build to start after lib was published
  • if lib could directly trigger a build of app on whatever branch it is publishing we would automate this.

This is a simplified explaination, in reality we now have 4 repos that inter-depend and that number will probably grow. This will make the baby-sitting very time-consuming.

10 Likes

is there any update on this issue? it makes the repository_dispatch useless if only triggered on master (or default) branch

2 Likes

@andreagriffiths11 Any update on this?

This is available in GitLab and allows creating powerful workflows and easy iteration on them (as you don’t have to commit temp workflows to master while developing on feature branch)

Clarification: which branch a repository dispatch is triggered for :memo:

That’s great feedback. Our team recently updated the documentation to include a note below the repository_dispatch documentation:

Note: This event will only trigger a workflow run if the workflow file is on the master or default branch.


Workaround: specifying a branch using the repository dispatch event :construction_worker_man:

Here’s an example repository that showcases using the repository dispatch event and actions/checkout@v2 specifying a branch in the client_payload field:

In the workflow file, it’s possible to leverage that input to checkout a specific branch (like what’s defined on these lines of the workflow file).

However, the Actions tab will associate the workflow run triggered by a repository dispatch to the default branch (in this case, master) rather than the specified branch as evidenced in this query.


Update: the latest news on the repository dispatch feature :newspaper:

:wave: Hello! Neither @andreagriffiths11 or I are able to speak to upcoming product updates. However, any new ships are either published in the form of a changelog entry or an article in The GitHub Blog.

Today, the best place to share requests for new or updated features is through our official product feedback form. Our product team working on Actions monitors that channels and considers each entry, but they may not be able to respond to every submission.

3 Likes

Thanks for your reply!

Unfortunately, the workaround you mentioned has a big drawback: It runs the workflow file specified on the default branch, not the workflow file of the checked out branch. So this only really works if the two branches have the same build/test instructions.

Some examples where this doesn’t work:

  • If we have a docs branch with a workflow file that converts some markdown documentation to HTML. Using your workaround, GitHub Actions would try to run the normal build instructions of the default branch (e.g. compile some code) on the markdown sources, which of course doesn’t work.
    • To prevent this issue, you could copy the markdown build instructions into the workflow file of the default branch. However, now you have to manually keep the two workflow files in sync when the markdown build instructions change at some point (and you need to remember to do it). This also means that any change to the markdown build instructions has to be coordinated across multiple branches, which will make things much more complicated.
  • Even with branches that have similar build instructions, it is very easy to get things wrong with that workaround. For example, consider some build instructions that use the current branch name for conditionally skipping checks. Another risk is that you probably won’t notice if there were slight differences in the two workflow files, which might result in inconsistent build results depending on how the build was triggered. It could even lead to corrupted release artifacts if the workaround is used to trigger a release of a different branch with slightly different build instructions.

So this workaround only works in the very specific case where both branches have exactly the same build instructions and use no advanced features such as skipping steps based on the current branch name.

To solve this problem for more use case, we need some way to trigger a workflow run on a different branch using the workflow file of that branch. I’ll send a feature request for that through the form you linked and I encourage others to do so too.

1 Like

A new workflow_dispatch event can be used to trigger workflow for non-default branch.
https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch

1 Like

@nitive - Ah, thanks for mentioning that! Our team also published a changelog entry for this feature that might be worth checking out as well. :v: