0

Disable Workflow

by
Published today

Disable a GitHub Actions workflow so it stops running on its triggers.

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
 * Disable Workflow
36
 * Disable a GitHub Actions workflow so it stops running on its triggers.
37
 */
38
export async function main(
39
  auth: RT.Github,
40
  owner: string,
41
  repo: string,
42
  workflow_id: DynSelect_workflow_id
43
) {
44
  const response = await fetch(
45
    `https://api.github.com/repos/${owner}/${repo}/actions/workflows/${workflow_id}/disable`,
46
    {
47
      method: "PUT",
48
      headers: {
49
        Authorization: `Bearer ${auth.token}`,
50
        Accept: "application/vnd.github+json",
51
        "X-GitHub-Api-Version": "2022-11-28",
52
      },
53
    }
54
  )
55

56
  if (!response.ok) {
57
    throw new Error(`${response.status} ${await response.text()}`)
58
  }
59

60
  return { success: true }
61
}
62