0

Create Workflow Dispatch

by
Published today

Trigger a GitHub Actions workflow that has a workflow_dispatch trigger, on a branch or tag. Inputs must match the workflow's declared inputs. OAuth connections need the workflow scope.

Script github Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 1 hour ago
1
//native
2

3
export type DynSelect_workflow_id = string
4

5
// Dropdown of the repository's workflows; fill owner and repo first.
6
export async function workflow_id(
7
  auth: RT.Github,
8
  owner: string,
9
  repo: string
10
) {
11
  if (!auth?.token || !owner || !repo) return []
12
  const response = await fetch(
13
    `https://api.github.com/repos/${owner}/${repo}/actions/workflows?per_page=100`,
14
    {
15
      headers: {
16
        Authorization: `Bearer ${auth.token}`,
17
        Accept: "application/vnd.github+json",
18
        "X-GitHub-Api-Version": "2022-11-28",
19
      },
20
    }
21
  )
22
  if (!response.ok) {
23
    throw new Error(`${response.status} ${await response.text()}`)
24
  }
25
  const { workflows } = (await response.json()) as {
26
    workflows: { id: number; name: string; path: string }[]
27
  }
28
  return workflows.map((w) => ({
29
    value: String(w.id),
30
    label: `${w.name} (${w.path})`,
31
  }))
32
}
33

34
/**
35
 * Create Workflow Dispatch
36
 * Trigger a GitHub Actions workflow that has a workflow_dispatch trigger, on a branch or tag. Inputs must match the workflow's declared inputs. OAuth connections need the workflow scope.
37
 */
38
export async function main(
39
  auth: RT.Github,
40
  owner: string,
41
  repo: string,
42
  workflow_id: DynSelect_workflow_id,
43
  ref: string,
44
  inputs: { [key: string]: any } | undefined
45
) {
46
  const response = await fetch(
47
    `https://api.github.com/repos/${owner}/${repo}/actions/workflows/${workflow_id}/dispatches`,
48
    {
49
      method: "POST",
50
      headers: {
51
        Authorization: `Bearer ${auth.token}`,
52
        "Content-Type": "application/json",
53
        Accept: "application/vnd.github+json",
54
        "X-GitHub-Api-Version": "2022-11-28",
55
      },
56
      body: JSON.stringify({ ref, inputs }),
57
    }
58
  )
59

60
  if (!response.ok) {
61
    throw new Error(`${response.status} ${await response.text()}`)
62
  }
63

64
  return { success: true }
65
}
66