Create a deployment

Deployments offer a few configurable parameters with certain defaults.

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 a deployment
6
 * Deployments offer a few configurable parameters with certain defaults.
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  body: {
13
    auto_merge?: boolean;
14
    description?: string;
15
    environment?: string;
16
    payload?: { [k: string]: unknown } | string;
17
    production_environment?: boolean;
18
    ref: string;
19
    required_contexts?: string[];
20
    task?: string;
21
    transient_environment?: boolean;
22
    [k: string]: unknown;
23
  }
24
) {
25
  const url = new URL(
26
    `https://api.github.com/repos/${owner}/${repo}/deployments`
27
  );
28

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