Update a variable for an environment

Update a deployment environment level variable.

Script bitbucket Verified

by hugo697 ยท 10/24/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 375 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Update a variable for an environment
7
 * Update a deployment environment level variable.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  workspace: string,
12
  repo_slug: string,
13
  environment_uuid: string,
14
  variable_uuid: string,
15
  body: { type: string; [k: string]: unknown } & {
16
    uuid?: string;
17
    key?: string;
18
    value?: string;
19
    secured?: boolean;
20
    [k: string]: unknown;
21
  }
22
) {
23
  const url = new URL(
24
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/deployments_config/environments/${environment_uuid}/variables/${variable_uuid}`
25
  );
26

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