Create an organization variable

Creates an organization variable that you can reference in a GitHub Actions workflow. You must authenticate using an access token with the `admin:org` scope to use this endpoint. GitHub Apps must have the `organization_actions_variables:write` organization permission to use this endpoint.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 367 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Create an organization variable
6
 * Creates an organization variable that you can reference in a GitHub Actions workflow.
7
You must authenticate using an access token with the `admin:org` scope to use this endpoint.
8
GitHub Apps must have the `organization_actions_variables:write` organization permission to use this endpoint.
9
 */
10
export async function main(
11
  auth: Github,
12
  org: string,
13
  body: {
14
    name: string;
15
    selected_repository_ids?: number[];
16
    value: string;
17
    visibility: "all" | "private" | "selected";
18
    [k: string]: unknown;
19
  }
20
) {
21
  const url = new URL(`https://api.github.com/orgs/${org}/actions/variables`);
22

23
  const response = await fetch(url, {
24
    method: "POST",
25
    headers: {
26
      "Content-Type": "application/json",
27
      Authorization: "Bearer " + auth.token,
28
    },
29
    body: JSON.stringify(body),
30
  });
31
  if (!response.ok) {
32
    const text = await response.text();
33
    throw new Error(`${response.status} ${text}`);
34
  }
35
  return await response.json();
36
}
37