Skip to content

Have a new workflow run from a PR (none main branch) before committing into main branch possible? #25746

Have a new workflow run from a PR (none main branch) before committing into main branch possible? #25746
Aug 16, 2021 · 14 answers

Hi,

I’d like to test a workflow in a PR, but from what I understand it only shows and runs if committed to the “main branch”.

Is there a way to have new workflows show and run in the PR stage?

Just to be clear, a new workflow in .github/workflows/some-workflow.yml, when committed in a new PR, it won’t show up in the actions and I’d like to have it and run it before committing to the main branch.

Thank you!

My apologies, just found that I have the paths, in which the changes at the moment do not full-fill as I’m only working in workflow.

By removing it, the action does show as expected!

Replies

14 suggested answers

You can trigger off of a pull request event in your workflow file:

https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request

0 replies

Thanks for looking!

But I should have worded it differently.

I’m looking to have the workflow peer-reviewed before integrating it into the main branch. This means that the workflow is not yet available or merged with the main branch. Because it is not yet available, displayed, showing in the Github actions, I won’t be able to trigger it: manually, by PR, etc.

I believe that Github does not yet support running workflows that only exist in a none main branch. Am I correct?

Example use-case:

  • Create a new branch for a new workflow
  • Create a new workflow under .github/foobar.yaml
  • Commit the file and changes for foobar.yaml
  • Push the branch to Github
  • Create a new PR for the branch against the main branch (or the same, in which the workflow should be triggered)
  • Finally check actions, where the workflow won’t be available, or shown

cc @mickeygousset

0 replies
heldrida:

I believe that Github does not yet support running workflows that only exist in a none main branch. Am I correct?

It depends on the event type. There are certain event types for which the workflow file must be on the default branch (e.g. schedule), but events associated with a specific branch (e.g. push or pull_request) should work on other branches.

Check GITHUB_REF for different events in Events that trigger workflows - GitHub Docs. 🙂

0 replies

Thanks for the support!

If the PR holding a new workflow hasn’t yet been approved and merged to the “main” branch, is it expected to show in the actions?

Here’s an example, which is not showing (the workflow yaml only exists in the pending PR):

name: Foobar

on:
pull_request:
branches: [ main ]

workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
  ...

My expectation was that at least I could trigger it manually, as you see the workflow_dispatch, or at least have it display in the actions sidebar. But afaik, it’ll only show once I merge into the main branch.

0 replies

My apologies, just found that I have the paths, in which the changes at the moment do not full-fill as I’m only working in workflow.

By removing it, the action does show as expected!

0 replies
Answer selected

for workflow_dispatch, you HAVE to have a version of the file in the main branch.

0 replies

But workflow_dispatch will only run the workflow version that’s checked into the main branch, right?

0 replies

Ahhh, see that is where things get interesting. To initially make it show up on the Actions tab, and so that you can trigger it manually, it has to be checked in to the main branch.

BUT, when you trigger it, you can pick which branch to run it against. The only caveat is that the workflow file (or a version of it) also has to exist IN THAT BRANCH.

0 replies

ok, got it. thanks.

but for repository_dispatch, the workflow that runs must be in the default branch, right? if not, how can a workflow in a non-default branch be triggered by a repository_dispatch?

0 replies

That I don’t know I’d have to test it and see. The docs don’t say. Logically it makes sense that yes it would have to be on the main branch for repository_dispatch to work

0 replies

As far as I can tell with my limited experience with repository_dispatch, it only runs workflows on the default/main branch when triggered by a push on a non-default branch. Would love to be proven wrong. :slight_smile:

0 replies

This is what I’d love:

push to dev branch -> trigger and run workflow on dev branch

0 replies

I was struggling with this too. I followed what I picked up from this thread and did the following:

  1. Make a new workflow with a very incomplete set of actions. Just a skeleton basically.
  2. Add the workflow_dispatch to the config so I can trigger it manually
  3. Commit the new workflow to a branch
  4. Open a pull request for that new branch
  5. Merge it into the default branch (main) (even though it’s not functional)
  6. Now you can access the action from the Actions tab on the repo
  7. You can run it against a specific branch.
0 replies

Fwiw, as long as the repository is public, you can create a public fork and set your public fork’s default branch to the version which has the code you want to demonstrate. (And possibly w/ the same name as the expected destination branch, instead of the name of the feature branch in your fork as used for your PR.)

In your case, removing the branches: [ main ] item of course was sufficient.

In some cases you could just add a second branch, as in:

on:
  pull_request:
    branches:
     - main
     - demo
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants