Github Actions: how to run a workflow created on a non-master branch from the workflow_dispatch event?

Viewed 23156

Can someone help me understand the behaviour of the Github Actions tab? As somebody new to Actions working on a third party repo I would like to be able to create an action on a branch and execute it on the workflow_dispatch event. I have not succeeded in doing this but I have discovered the following:

  • The Action tab will change the branch where it finds workflows and action code based on the branch relating to the last executed workflow. e.g. if some workflow is executed from the Action tab using the Run Workflow button and the Use Workflow From dropdown is set to some branch, Branch-A, then the contents of the Workflows panel on the left hand side of the Actions tab will be taken from Branch-A's version of .github/.
  • The This workflow has a workflow_dispatch event trigger. text does not change with the branch. It seems to be taken from master. Alternatively, it may be being taken from the last set of results. I have not tested for that because either way it is not helpful behaviour.

The workaround is the execute on a push event which is OK but that seems out of kilter with Github's high standards of design.

Does the above sound a) about right and b) whichever way you look at it, not optimal behaviour? Or, is there a better approach to building and testing actions?

5 Answers

You can run a workflow that is still in development in a feature-branch from the command line, with Github CLI. The documentation says:

To run a workflow on a branch other than the repository's default branch, use the --ref flag.

gh workflow run workflow --ref branch-name

To add input parameters, run it like this:

gh workflow run workflow --ref branch-name -f myparameter=myvalue

  • Seems like it works as you described
  • Text seems to change when you run workflow on non main branch and on this branch workflow name changed to something new...

This workflow name change is really strange. I couldn't find any docs describing this behavior.

Testing workflows

One thing that needs to be done before testing is to actually add dummy workflow with same filename to main/master. Without this workflow won't appear in actions tab.

How to test:

  1. Create dummy Readme.md and some dummy .github/workflows/workflow.yml to test:
    name: Test run v1
    
    on:
      workflow_dispatch:
    
    jobs:
      test:
        runs-on: ubuntu-18.04
        steps:
          - name: Show environment v1
            run: env | grep ^GITHUB
          - name: Show ref v1
            run: echo "===============> Version from $GITHUB_REF"
    
  2. Push to your default branch (probably main or master)
  3. New action should appear there
  4. You can now run dummy workflow

Testing branch run:

  1. Create new branch test-branch from default repo branch
  2. Modify workflow file .github/workflows/workflow.yml
    name: Test run v2
    
    on:
      workflow_dispatch:
    
    jobs:
      test:
        runs-on: ubuntu-18.04
        steps:
          - name: Show environment v2
            run: env | grep ^GITHUB
          - name: Show ref v2
            run: echo "===============> Version from $GITHUB_REF"
    
  3. Commit and push to test-branch
  4. Go to Actions
  5. Click Test run v1 and set Use workflow from to test-branch.
  6. Click run workflow button

You should see different step names than in default repo branch workflow version and different GITHUB_REF.

The weird thing is that after running workflow on test-branch somehow without merging anything, my previous workflow (from default repo branch) changed name to new version.

enter image description here

You can run your workflow through the GitHub CLI, but you will first need to make sure it's run before.

gh workflow list

If your workflow isn't in that list (by name), then add pull_request: and create a pull request so that the workflow is registered, once.

name: 'My Workflow'
on:
  workflow_dispatch:
    inputs:
      parameter:
        description: My Parameter
  pull_request: -- Add this here
...

Once you've created a pull request, you should see 'My Workflow' when you run gh workflow list. Once that's done, you can remove the added line.

Finally, now run:

gh workflow run 'My Workflow' --ref my-branch -f parameter=value

This should run your workflow dispatch from a feature branch.

I'm seeing a lot of answers with adding pull_request:, but in my organization, we hold off on pull requests until we want review by others.

I think a better suggestion was made by @MEMark.

Add push: instead and just push once to your remote feature branch, then edit that line out. The action will be registered and you don't have to mess with master or any pull requests.

name: 'My Workflow'
on:
  workflow_dispatch:
    inputs:
      parameter:
        description: My Parameter
  push: -- Add this here

As an option you may fork third-party repository to yours, and do PR, merging to your main branch. After that you'll be able to debug workflow on your repository.

Another option is to add on: pull_requst: and test it by creating pull request.

Related