Manual workflow triggers in Github Actions

11/19/2019

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?

-- Antoine C.
devops
docker
github-actions
kubernetes

5 Answers

11/21/2019

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 a weird shit 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.

-- Sarah Abderemane
Source: StackOverflow

11/19/2019

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
Source: StackOverflow

1/6/2020

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
Source: StackOverflow

1/7/2020

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
Source: StackOverflow

11/21/2019

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.
Source: StackOverflow