mikemay
mikemay

Reputation: 4657

GitHub Actions: how can I run a workflow created on a non-'master' branch from the 'workflow_dispatch' event?

For actions working on a third party repository, 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 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?

Upvotes: 173

Views: 147271

Answers (7)

The Fool
The Fool

Reputation: 20547

If you already modify your workflow, as shown in the other answers (suggesting to add PR) and then use the GitHub CLI (executable gh), you might as well do this.

on:
  workflow_dispatch: {}
  push:
    branches:
    - features/myfeature

This way you don’t need to use the CLI. The pipeline will run once you push. Then you have to remove that branch trigger again, as you would do with the pr trigger.

So after all it's still not ideal, but at least you don’t jump through more hoops using the GitHub CLI.

Upvotes: 0

code11
code11

Reputation: 2329

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

Upvotes: 38

Alexander R.
Alexander R.

Reputation: 1895

You can run a workflow that is still in development in a branch branch-name from the command line, with the 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-name --ref branch-name

To list valid workflow names, use gh workflow list.

To add input parameters, run it like this:

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

Upvotes: 138

Joman68
Joman68

Reputation: 2850

Whether you use on push: or on pull:, you will likely need to provide default values for your input parameters (as they will all be set to null when the workflow is triggered).

For example:

name: 'My Workflow'
on:
  workflow_dispatch:
    inputs:
      logLevel:
        description: Logging level
        type: string
        required: true
  push:

env:
  LOG_LEVEL: ${{ inputs.logLevel || 'DEBUG' }}

In the above example inputs.logLevel is null when run on push so the || expression defaults it to DEBUG.

See this answer for more details on how to set default values for input parameters.

Upvotes: -1

piotrekkr
piotrekkr

Reputation: 3206

UPDATE: @hayesgm answer may be better choice since using push/pull_request workflow trigger will register new workflow in GitHub and then you can just remove unneeded push/pull_request events trigger and run workflow using gh command. It works without merging anything to default branch.

UPDATE 2: Seems like using @hayesgm way may not work for you if you expect to see "Run workflow" dropdown in GitHub UI for your not yet merged workflow. It will be available to see on workflow list in Actions tab and will be available to run using gh but not by using GitHub UI.

UPDATE 3: I could not find it documented anywhere but I think workflow is registered only if it exist on default branch or if it was run at least once on non-default branch. In case of non default branch, GitHub knows about workflow existence only when it has workflow run logs available. If you remove all workflow logs for it, then it will get unregistered and you cannot run it any more. This is not the case for workflows that exist on default branch. Those are always available even without any workflow run logs.


  • 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

Upvotes: 39

hayesgm
hayesgm

Reputation: 9096

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.

Upvotes: 86

Nickolay Olshevsky
Nickolay Olshevsky

Reputation: 14160

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.

Upvotes: 0

Related Questions