0
Create or update an environment secret
One script reply has been approved by the moderators Verified

Creates or updates an environment secret with an encrypted value.

Created by hugo697 407 days ago Viewed 9028 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 407 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Create or update an environment secret
6
 * Creates or updates an environment secret with an encrypted value.
7
 */
8
export async function main(
9
  auth: Github,
10
  repository_id: string,
11
  environment_name: string,
12
  secret_name: string,
13
  body: { encrypted_value: string; key_id: string; [k: string]: unknown }
14
) {
15
  const url = new URL(
16
    `https://api.github.com/repositories/${repository_id}/environments/${environment_name}/secrets/${secret_name}`
17
  );
18

19
  const response = await fetch(url, {
20
    method: "PUT",
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.json();
32
}
33