GitHub Actions On Demand

Date Published: 27 September 2021

GitHub Actions On Demand

I use GitHub Actions a lot. They're a great tool for performing continuous integration (CI) and continuous deployment (CD) for your applications. However, by default they typically only are triggered by changes to your application's source code in the form of commits and/or merges/rebases. This means that to trigger a particular workflow, you usually need to make a commit, and obviously this can be a hassle in a number of situations.

Fortunately, GitHub supports triggering GitHub Actions on demand. To trigger any GitHub Action on demand, you simply need to add a keyword to its YML file, called workflow_dispatch. Personally, I have a hard time remembering the term "workflow dispatch", which is why I usually end up searching for "on demand" and not immediately finding anything. I'm actually publishing this just so that I can google "ardalis github actions on demand" and find this next time I need it. :)

The documentation for GitHub Actions' "workflow dispatch" trigger show how to create a manually triggered workflow. However, they don't show how you add a workflow_dispatch trigger to a GitHub action that already has other triggers, which may not be obvious.

I've figured this out with some of my various Ardalis NuGet Packages, so here is an example from my most popular NuGet Package, Ardalis.GuardClauses:

name: .NET Core

on:
  workflow_dispatch:
    branches: [ main ]
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:

    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.402
    - name: Install dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --configuration Release --no-restore
    - name: Test
      run: dotnet test --no-restore --verbosity normal

You'll find the latest full GitHub action for GuardClauses here. While you're there, be sure to add a ⭐ to the repo! Thanks!

Workflow Dispatch and On Demand GitHub Actions

If you have multiple triggers, you can just add workflow_dispatch to the list in the YAML file. This will let you trigger the workflow manually. What does that look like? You just go to the actions tab, choose the workflow, and you should see an option to "Run workflow". You can further choose which branch to run it against, if you like.

GitHub Actions Run Workflow Screenshot

That's all you need to be able to run your actions any time you like, without the need to add bogus commits!

Steve Smith

About Ardalis

Software Architect

Steve is an experienced software architect and trainer, focusing on code quality and Domain-Driven Design with .NET.


Ardalis

Copyright © 2021