DEV Community

Sarah Abd
Sarah Abd

Posted on • Updated on

Manual trigger with Github Actions

Hi there, this blog post explains how to manually trigger builds of GitHub actions.

Why would you want to do that?

Instead of just waiting for a code change or a GitHub workflow change? Well, for example the last time your build on master ran, maybe some of the servers used by your build were down (example: npm registry, some API you're using for tests, etc..).

For now GitHub says that there's no way to manually trigger a build, while this question was asked on StackOverflow and some people are even using CURL to trigger manual builds.

But I have found a better way! With my solution, once you'll have it set up, the ⭐️ button of your GitHub repository will trigger a build every time YOU (and only you) will star the repository.
You can do that as many times as you want.

Yes, it's a VERY hacky solution but it works pretty well. ✨

How to do manual trigger

Let's dig into it.

Alt Text
The ⭐️ button is used to launch the workflow if it's possible, like this.

Update your workflow file to include:


on:
  watch:
    types: [started]

Enter fullscreen mode Exit fullscreen mode

So the workflow will be executed each time you star, or unstar and star again the repo.

If we combine that by a useful advice from Samira in Stack Overflow, you can launch your workflow only if it's the owner who trigger the action.


    if: github.actor == github.event.repository.owner.login

Enter fullscreen mode Exit fullscreen mode

It prevents to execute the action by others and only by maintainers for example.

Next you can add your job(s) to do and additional services.

At the end, we have the following workflow...

name: Test

on:
  watch:
    types: [started]

jobs:
  build:
    runs-on: ubuntu-latest

    if: github.actor == github.event.repository.owner.login

    steps:
       - name: Checkout repository
         uses: actions/checkout@v2
        #  add more ...
Enter fullscreen mode Exit fullscreen mode

It's a starter, so you can adapt it for an existing workflow or start a new one with this.

Few notes

There is the possibility to combine two events.

For example you can set your workflow with the watch event and on each push, to have the both ways.

name: Test

on:
  push:
    branches:
      - master
  watch:
    types: [started]


Enter fullscreen mode Exit fullscreen mode

Here it run the process for each push on master branch and on star of the repo.

Don't forget that if you want to re-run a workflow which have failed,
you can re-run all the checks with a UI button set for a failed workflow just like that...

Alt Text

It depends on what you really want to do.

Moreover, you can launch the full workflow with the watch event or only some particular steps if you add a conditional if on a step like this:

name: Test

on:
  watch:
    types: [started]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
       - name: Checkout repository
         uses: actions/checkout@v2
       - name: Step 2
         if: github.actor == github.event.repository.owner.login

Enter fullscreen mode Exit fullscreen mode

In this case, it will run step 2 only if the repo is starred by the owner.

Don't forget that the ⭐️ button launch the workflow from the default branch (master) only.

Notice that there is permissions to avoid actions in the repository or to avoid the third party to run your actions in the settings of the repo.

Alt Text

Update: Suggested by Emilien in comments, we can also configure the workflow only for collaborators by creating another job which will be needed to check if it's a collaborator of the project or the workflow will fail.

jobs:
  authorize: 
    runs-on: ubuntu-latest

    steps:
    - uses: octokit/request-action@v2.0.0
      with:
        route: GET /repos/:repository/collaborators/${{ github.actor }}
        repository: ${{ github.repository }}
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  build:
    needs: [authorize]

    runs-on: ubuntu-latest

   steps: 
     - ...

Enter fullscreen mode Exit fullscreen mode

Good to know...

GitHub just released their action APIs which should allow soon either GitHub or browser extensions to add "trigger workflow" buttons on their UI.


I hope you liked this post and special thanks to Vincent who pushed me to write it :)

Top comments (14)

Collapse
 
m157q profile image
M157q

GitHub has added workflow_dispatch event for manually trigger.
FYI: github.blog/changelog/2020-07-06-g...

Collapse
 
jefftriplett profile image
Jeff Triplett (he/him)

Another alternative is to add an event hook for the on: respository_dispatch event which should let you trigger builds. See the docs here: help.github.com/en/actions/automat...

Collapse
 
sabderemane profile image
Sarah Abd

Oh yes, thanks !
My solution use simplicity with the star button which already here and nothing to implement more than the workflow.
The repository_dispatch event has advantage to target other branch than master contrary to the watch event with the star.
Spot on.

Collapse
 
drewmullen profile image
drewmullen

thanks @jefftriplett i put together an easy example repo based on this: github.com/drewmullen/actions-play...

Collapse
 
neilime profile image
Emilien Escalle • Edited

Get a solution to allows to all repository collaborators:

jobs:

  authorize: 
    runs-on: ubuntu-latest

    steps:
    - uses: octokit/request-action@v2.0.0
      with:
        route: GET /repos/:repository/collaborators/${{ github.actor }}
        repository: ${{ github.repository }}
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  build:
    needs: [authorize]

    runs-on: ubuntu-latest

   steps: 
     - ...
Collapse
 
sabderemane profile image
Sarah Abd • Edited

Nice ! I add it on the post :)

Collapse
 
karfau profile image
Christian Bewernitz

Thx, even though I'll most likely not use the "star method", your article is a good reference collection regarding github action triggers.

Do you have a reference/example at hand how to write the condition to check if the stargazer is in the team of an org (or in a list of users?)

Collapse
 
sabderemane profile image
Sarah Abd • Edited

Thanks, my article hasn't vocation to deliver the best method and as I said, it's really hacky, but good to know and also there are other options.

I haven’t an example right now but you can easily do a step with the logic of checking if the user is part of a list of the users in the language you prefer.
Execute the file and save the result as variable and return a boolean if it's true or false. And the following steps will be conditioned according to the result.

Maybe this example can helps you :

name: Extract branch name
      id: extract_github
      run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"

In this case, in your workflow, you will be able to reuse the name of the branch if you want to do a step only on this branch :

if: steps.extract_github.outputs.branch == ’staging'
Collapse
 
abatilo profile image
Aaron Batilo • Edited

Hi all!

I'm hoping posting here won't be a problem.

I wanted to share a small project that I've been working on with a buddy that solves this problem.

actionspanel.app/

ActionsPanel uses this same repository_dispatch API but does so with a GitHub App token so that you don't need to worry about managing your own PAT. This also makes it much easier to trigger your actions across teams with multiple people. Then you don't need to share the PAT with each other or each create your own PATs.

You configure your buttons with a declarative yaml file that you leave in the repo, and ActionsPanel will read that file and dynamically create your UI for you to trigger your actions.

We'd love to get your feedback on this project. It's very simple still but solves the core problem of triggering your actions.

If you do have feedback or any questions, feel free to post in this thread, or email us directly at support (at) actionspanel (dot) app

Looking forward to your feedback!

Collapse
 
hugojgoncalves profile image
Hugo José Gonçalves

Is it open source?
Asking for repo write permissions from a closed source code project is a little too much for me.

Collapse
 
doggie52 profile image
Douglas Stridsberg

I needed to add : to the end of watch to get this to work.

on:
  watch:
    types: [started]

Did nobody actually try this code, or is there some stricter Action language interpretation on my end?

Collapse
 
sabderemane profile image
Sarah Abd • Edited

It was already tested by me because I add a gif demo of the watch event. GitHub Actions is based on yaml language. It's a typo, I update the article thanks.

Collapse
 
gautamkrishnar profile image
Gautam Krishna R

Looks good but it will spam your followers feed, if you have a large amount of followers right?

Collapse
 
sabderemane profile image
Sarah Abd • Edited

Good question, in fact no, because it's only notifying on discussions about :

  • Issues and their comments
  • Pull Requests and their comments
  • Comments on any commits

of the starred repository.

Further details just here :)