0

Create a workflow dispatch event

Published Oct 25, 2023

You can use this endpoint to manually trigger a GitHub Actions workflow run.

Script github
  • Submitted by hugo697 Typescript (fetch-only)
    Created 560 days ago
    1
    type Github = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Create a workflow dispatch event
    6
     * You can use this endpoint to manually trigger a GitHub Actions workflow run.
    7
     */
    8
    export async function main(
    9
      auth: Github,
    10
      owner: string,
    11
      repo: string,
    12
      workflow_id: string,
    13
      body: { inputs?: { [k: string]: string }; ref: string; [k: string]: unknown }
    14
    ) {
    15
      const url = new URL(
    16
        `https://api.github.com/repos/${owner}/${repo}/actions/workflows/${workflow_id}/dispatches`
    17
      );
    18
    
    
    19
      const response = await fetch(url, {
    20
        method: "POST",
    21
        headers: {
    22
          "Content-Type": "application/json",
    23
          Authorization: "Bearer " + auth.token,
    24
        },
    25
        body: JSON.stringify(body),
    26
      });
    27
      if (!response.ok) {
    28
        const text = await response.text();
    29
        throw new Error(`${response.status} ${text}`);
    30
      }
    31
      return await response.text();
    32
    }
    33