Create a GitHub Pages deployment

Create a GitHub Pages deployment for a repository. Users must have write permissions. GitHub Apps must have the `pages:write` permission to use this endpoint.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 366 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Create a GitHub Pages deployment
6
 * Create a GitHub Pages deployment for a repository.
7

8
Users must have write permissions. GitHub Apps must have the `pages:write` permission to use this endpoint.
9
 */
10
export async function main(
11
  auth: Github,
12
  owner: string,
13
  repo: string,
14
  body: {
15
    artifact_url: string;
16
    environment?: string;
17
    oidc_token: string;
18
    pages_build_version: string;
19
    [k: string]: unknown;
20
  }
21
) {
22
  const url = new URL(
23
    `https://api.github.com/repos/${owner}/${repo}/pages/deployment`
24
  );
25

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