41

I am setting up Github Actions for a project repository.

The workflow consists of the following steps:

  • Building a docker image
  • Pushing the image to a container registry
  • Rollout a Kubernetes deployment.

However, I have two different Kubernetes deployments: one for development, and one for production. Hence, I have also two Github Action workflows.

The Github Action workflow for development is triggered everytime that a commit is pushed:

on:
  push:
    branches:
    - master

But I don't want that for my production workflow. I would need a manual trigger, like a Send to production button. I didn't see anything close to that in the docs.


Is there a way to trigger a workflow manually in Github Actions?

How can I split my development and my production workflows to achieve what I want, either on Github Actions, Docker or Kubernetes?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Antoine C.
  • 3,730
  • 5
  • 32
  • 56

7 Answers7

44

Is there a way to trigger a workflow manually in Github Actions?

You might consider, from July2020:

GitHub Actions: Manual triggers with workflow_dispatch

(Note: or multiple workflows, through the new Composite Run Steps, August 2020)

You can now create workflows that are manually triggered with the new workflow_dispatch event.
You will then see a 'Run workflow' button on the Actions tab, enabling you to easily trigger a run.

https://i2.wp.com/user-images.githubusercontent.com/1865328/86147571-2de93700-babf-11ea-8a08-e4beffd3abe9.png?ssl=1

You can choose which branch the workflow is run on.

philippe adds in the comments:

One thing that's not mentioned in the documentation: the workflow must exist on the default branch for the "Run workflow" button to appear.
Once you add it there, you can continue developing the action on its own branch and the changes will take effect when run using the button

The documentation goes on:

In addition, you can optionally specify inputs, which GitHub will present as form elements in the UI. Workflow dispatch inputs are specified with the same format as action inputs.

For example:

on: 
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'     
        required: true
        default: 'warning'
      tags:
        description: 'Test scenario tags'  

The triggered workflow receives the inputs in the github.event context.

For example:

jobs:
  printInputs:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "Log level: ${{ github.event.inputs.logLevel }}"
        echo "Tags: ${{ github.event.inputs.tags }}" 

shim adds in the comments:

You can add workflow_dispatch to a workflow that also has other triggers (like on push and / or schedule)

For instance:

on:
 workflow_dispatch:
 push:
   branches:
     - master
 pull_request:
   types: [opened, synchronize, reopened]
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 31
    One thing that's not mentioned in the documentation: the workflow must exist on the default branch for the "Run workflow" button to appear. Once you add it there, you can continue developing the action on its own branch and the changes will take effect when run using the button. – philippe Jul 21 '20 at 15:53
  • 3
    @philippe Thank you for that feedback. I have included your comment in the answer for more visibility. – VonC Jul 21 '20 at 19:01
  • Can this be used with collaborator or maintainer permissions only? – A. Wentzel Apr 06 '21 at 20:17
  • @A.Wentzel I believe collaborator or above but I haven't tested it. Related: https://github.community/t/who-has-permission-to-workflow-dispatch/133981/3 – VonC Apr 06 '21 at 20:19
  • 4
    Can you add `workflow_dispatch` to a workflow that also has other triggers (like on push and / or schedule)? Can't quite figure out the syntax – shim Aug 13 '21 at 18:50
  • @shim Not sure. That could be a good question on its own. – VonC Aug 13 '21 at 19:01
  • Just realized I was just missing a colon, even though it looks kind of weird. [Here's an example in a gist](https://gist.github.com/simonbromberg/d9dfbbd3fcf481dd59dc522f0d12cdf6) – shim Aug 13 '21 at 19:02
  • 2
    @shim Well done. I have included your comment and example in the answer for more visibility. – VonC Aug 13 '21 at 19:05
  • Does anyone know if it is possible to filter only a specific branch in the branch selection of the workflow_dispatch form? – Hugo Leonardo Mar 28 '22 at 14:46
  • @HugoLeonardo Bot sure: you could ask a separate question, with a clear illustration of "filter only a specific branch in the branch selection of the workflow_dispatch form", for us to asnwer. – VonC Mar 28 '22 at 14:49
23

EDITED :

Great tweet explaining the use of workflow dispatch : https://twitter.com/github/status/1321859709075394563?s=19


Is there a way to trigger a workflow manually in Github Actions?

I've got a little hack to do so...

With the watch event, you can manually trigger an action by star or unstar the repo. The code for the event in your workflow is :

on:
  watch
    types: [started]

I know it's weird but it works! Nevertheless, it's not the best way if it's a public repo with potential stars.


How can I split my development and my production workflows to achieve what I want, either on Github Actions, Docker or Kubernetes?

In Github Actions I mean, you can do multiple workflows / jobs and filter by targeted branches or events. You can combine multiple events for example trigger a workflow for push and with a cron on midnight.

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Sarah Abderemane
  • 596
  • 5
  • 14
  • 11
    Haha, this is great :> `repository_dispatch` aside, one can combine `watch` with `if: github.actor == 'hackerman'` to filter out random strangers. Or better yet - `if: github.actor == github.event.repository.owner.login` for extra "security" :D – Samira Nov 21 '19 at 07:13
  • 2
    Haha thanks! Yes good idea, I've to try this out when I got time ! :D – Sarah Abderemane Nov 21 '19 at 07:51
  • 2
    Perfect I think this is the best method while there isn't something officially implemented. – Antoine C. Nov 21 '19 at 09:41
9

Update: For a slash command style "ChatOps" solution see slash-command-dispatch action. This can allow you to trigger workflows with slash commands (e.g. /deploy) from issue and pull request comments.

Here is a basic example for a deploy slash command. REPO_ACCESS_TOKEN is a repo scoped Personal Access Token

name: Slash Command Dispatch
on:
  issue_comment:
    types: [created]
jobs:
  slashCommandDispatch:
    runs-on: ubuntu-latest
    steps:
      - name: Slash Command Dispatch
        uses: peter-evans/slash-command-dispatch@v1
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          commands: deploy

The command can be processed in this workflow.

name: Deploy Command
on:
  repository_dispatch:
    types: [deploy-command]

There are many more options and different setups. See slash-command-dispatch for full usage instructions.

Original Answer: A repository_dispatch workflow can be manually triggered by a call to the GitHub API as follows.

on:
  repository_dispatch:
    types: [production-deploy]
  • [username] is a GitHub username
  • [token] is a repo scoped Personal Access Token
  • [repository] is the name of the repository the workflow resides in.
curl -XPOST -u "[username]:[token]" \
  -H "Accept: application/vnd.github.everest-preview+json" \
  -H "Content-Type: application/json" \
  https://api.github.com/repos/[username]/[repository]/dispatches \
  --data '{"event_type": "production-deploy"}'
peterevans
  • 34,297
  • 7
  • 84
  • 83
  • 1
    For anyone interested, it's possible to use single workflow for multiple dispatches. What's sent as `event_type` is available to workflow as `github.event.action`, so specific jobs/steps can be enabled/disabled when needed. PS: PAT is not really needed, starting curl with `-u "[username]:[password]"` or even `-u "[username]"` works as well (in second case curl prompts user for password); easier to use in some cases (for example when writing scripts which takes username as input, or scripts intended to be used by less tech-savvy users). – Samira Nov 21 '19 at 21:22
6

Another way to resolve this with the current Github Action offering is to create a production branch from master when a deploy is needed & trigger deploy action on the production branch. The production branch is essentially a mirror of the master.

on:
  push:
    branches:    
      - master

Dev builds/push can happen whenever there is a commit to the master.

on:
  push:
    branches:    
      - production

At some point in the release schedule, you can raise the PR to the production branch. This will take care of the prod build/deploy.

user1064504
  • 573
  • 6
  • 16
5

Although Sarah's post was the closest and simplest answer to the original question, it is somewhat hacky so we eventually ended up by creating a dev branch to use the following triggers:

  • Development workflow: triggered when a push is made on the dev branch:

    on:
      push:
        branches:    
          - dev
    
  • Production workflow: triggered when a pull request / merge is made from dev to master:

    on:
      pull_request:
        branches:    
          - master
    
Antoine C.
  • 3,730
  • 5
  • 32
  • 56
5

Edited for more detail/explanation.

One thing that you can do is call to repository_dispatch. You can view the GitHub documentation for using a repository_dispatch here.

For example, if you have a GitHub Actions workflow that looks like this:

on:
  repository_dispatch:
    types: [run_tests]
name: Run tests
jobs:
  test:
    name: Run your tests
    runs-on: ubuntu-latest
    steps:
      - run: |
          echo "I just ran all your tests!"

You can create a repository dispatch event by following the steps that are explained on the GitHub v3 API Documentation.

First, create a personal access token (PAT) on GitHub for authentication.

Then, you can run curl like so:

curl \
  -H "Authorization: token $YOUR_PAT" \
  --request POST \
  --data '{"event_type": "run_tests"}' \
  https://api.github.com/repos/$USER/$REPOSITORY/dispatches

At the same time, I also wanted to share a small project that I've been working on with a buddy that solves this exact problem.

https://www.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.

Based on user requests and feedback, we've built in features to specify which branch to send the repository_dispatch to, and we've even built in a way to inject parameters when you want to execute the action.

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.

aaronbatilo
  • 51
  • 1
  • 4
3

What GitHub cryptic documentation fails to clarify is that you can have multiple workflow files under .github/workflows, each with its own trigger. For instance, I've a workflow that builds and runs tests on every push and pull request, and another that is triggered manually to publish the artifact.

(ci.yml)

name: CI Pipeline
on: [push, pull_request]

---
(publish.yml)

name: Publish
on:
  workflow_dispatch:
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219