Search code examples
github-actionsgithub-apioctokit-js

Programatic github workflow_dispatch does not run the workflow


I am trying to programatically run a github workflow using the workflow_dispatch event.

There are two workflows on the master branch. Release Matrix and Release Single.

The single workflow looks like this and it working fine.

release-single.yaml

name: Release Single

on:
  workflow_dispatch:
    inputs:
      type:
        description: 'Type'
        required: true
      version:
        description: 'Version'
        required: true
      label:
        description: 'Label'
        required: false
      tag:
        description: 'Tag'
        required: true

jobs:
  [...]

it shows up in the UI and can be triggered by hand:

enter image description here

The matrix workflow has a step that creates the workflow_dispatch event (I've tried different solutions, both following):

release-matrix.yaml

  - uses: actions/github-script@v4
    with:
      github-token: ${{ github.token }}
      debug: true
      script: |
        const workflow = await github.actions.createWorkflowDispatch({
            owner: "owner_name",
            repo: "repo_name",
            workflow_id: "release-single.yaml",
            ref: "${{ github.ref }}",
            inputs: {
              type: "${{ matrix.type }}",
              version: "${{ matrix.version }}",
              label: "${{ matrix.label }}",
              tag: "${{ matrix.tag }}"
            }
        });
        console.log(workflow);

  - uses: benc-uk/[email protected]
    with:
      workflow: Release Single
      repo: ${{ github.repository }}
      token: ${{ github.token }}
      inputs: '{ "type": "${{ matrix.type }}", "version": "${{ matrix.version }}", "label": "${{ matrix.label }}", "tag": "${{ matrix.tag }}" }'

when the matrix workflow runs it actually executes the steps successfully (both solutions respond with a 204 created just as the documentation states), but no workflow runs show up in the github UI.

Result using actions/github-script and octokit enter image description here

Result using benc-uk/workflow-dispatch enter image description here

To finally come to my Question: Am I missing something or doing it wrong? May it be a bug?


Solution

  • You have to use a personal access token by an actual github user. though using the ${{ github.token }} gives a 204 Created response, in fact it is not running the workflow. When passing a personal access token instead, it gives exactly the same 204 response and the workflows runs as expected.

    Thanks for Benjamin for the deeplink: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#triggering-new-workflows-using-a-personal-access-token

    The downside of using a personal access token is, that the workflows are always triggered by an actual user.

    enter image description here

    Alternatively it is possible to create a github app and with that generate an access token. It will then look like this:

    enter image description here