Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 581
mikemay Avatar asked Aug 11 '20 16:08

mikemay


People also ask

Do GitHub Actions need to be on main branch?

Github actions can only trigger on master or the default branch.

How do I trigger a workflow from another workflow in GitHub Actions?

If you do want to trigger a workflow from within a workflow run, you can use a personal access token instead of GITHUB_TOKEN to trigger events that require a token. You'll need to create a personal access token and store it as a secret.

How do I run a workflow branch?

Under your repository name, click Actions. In the left sidebar, click the workflow you want to run. Above the list of workflow runs, select Run workflow. Use the Branch dropdown to select the workflow's branch, and type the input parameters.


2 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

like image 128
Alexander R. Avatar answered Sep 25 '22 12:09

Alexander R.


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.

like image 39
ghayes Avatar answered Sep 26 '22 12:09

ghayes