-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathupdate-major-tag.js
More file actions
45 lines (40 loc) · 1.16 KB
/
update-major-tag.js
File metadata and controls
45 lines (40 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// @ts-check
/** @param {import('@actions/github-script').AsyncFunctionArguments} args */
export default async function updateMajorTag({ github, context }) {
const tag = context.payload.release?.tag_name;
if (!tag) {
throw new Error("No release tag found in event payload");
}
const match = tag.match(/^(v\d+)\.\d+\.\d+/);
if (!match) {
throw new Error(`Tag "${tag}" does not match semver pattern vX.Y.Z`);
}
const majorTag = match[1];
const sha = context.sha;
const tagsToUpdate = [majorTag, "latest"];
for (const tagName of tagsToUpdate) {
const ref = `tags/${tagName}`;
try {
await github.rest.git.updateRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref,
sha,
force: true,
});
console.log(`Updated ${ref} to ${sha}`);
} catch (error) {
if (error?.status === 404 || error?.status === 422) {
await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/${ref}`,
sha,
});
console.log(`Created ${ref} at ${sha}`);
} else {
throw error;
}
}
}
}