Repository navigation
177 lines (168 loc) · 6.22 KB
/
Copy path_finalize-release.yaml
File metadata and controls
177 lines (168 loc) · 6.22 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
name: Finalize Release
on:
workflow_call:
inputs:
ref:
description: Git ref containing the published package versions
type: string
default: ""
manifest:
description: Package names and versions included in the release
type: string
required: true
release_name:
description: GitHub release name prefix
type: string
default: CCC Release
jobs:
finalize:
name: Finalize GitHub release
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
ref: ${{ inputs.ref || github.sha }}
- uses: actions/setup-node@v7
with:
node-version: 26
- name: Collect release metadata
id: release
env:
RELEASE_MANIFEST: ${{ inputs.manifest }}
run: |
node -e '
const { execFileSync } = require("node:child_process");
const fs = require("node:fs");
const path = require("node:path");
const manifest = JSON.parse(process.env.RELEASE_MANIFEST);
if (!Array.isArray(manifest) || manifest.length === 0) {
throw new Error("Release manifest is empty");
}
const packageDirectories = new Map();
for (const entry of fs.readdirSync("packages", { withFileTypes: true })) {
if (!entry.isDirectory()) {
continue;
}
const directory = path.join("packages", entry.name);
const packageJsonPath = path.join(directory, "package.json");
if (!fs.existsSync(packageJsonPath)) {
continue;
}
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
packageDirectories.set(packageJson.name, directory);
}
const releases = [];
for (const { name, version } of manifest) {
if (typeof name !== "string" || typeof version !== "string") {
throw new Error("Release manifest contains an invalid package");
}
const directory = packageDirectories.get(name);
if (!directory) {
throw new Error(`Unable to find package ${name}`);
}
const changelogPath = path.join(directory, "CHANGELOG.md");
const lines = fs.readFileSync(changelogPath, "utf8").split("\n");
const start = lines.findIndex((line) => line.trim() === `## ${version}`);
if (start === -1) {
throw new Error(`Unable to find ${name}@${version} in ${changelogPath}`);
}
let end = start + 1;
while (end < lines.length && !lines[end].startsWith("## ")) {
end++;
}
const changelog = lines.slice(start + 1, end).join("\n").trim();
if (!changelog) {
throw new Error(`Changelog for ${name}@${version} is empty`);
}
releases.push({ name, version, changelog });
}
releases.sort((a, b) => a.name.localeCompare(b.name));
const sha = execFileSync("git", ["rev-parse", "HEAD"], { encoding: "utf8" }).trim();
const committedAt = execFileSync(
"git",
["show", "-s", "--format=%cI", "HEAD"],
{ encoding: "utf8" },
).trim();
const releaseTime = `${new Date(committedAt).toISOString().slice(0, 16).replace("T", " - ")} UTC`;
fs.appendFileSync(
process.env.GITHUB_OUTPUT,
`sha=${sha}\nrelease_time=${releaseTime}\nreleases=${JSON.stringify(releases)}\n`,
);
'
- name: Create aggregate GitHub release
uses: actions/github-script@v9
env:
RELEASE_NAME: ${{ inputs.release_name }}
RELEASES: ${{ steps.release.outputs.releases }}
RELEASE_TIME: ${{ steps.release.outputs.release_time }}
RELEASE_SHA: ${{ steps.release.outputs.sha }}
with:
retries: 3
script: |
const releases = JSON.parse(process.env.RELEASES);
const releaseName = process.env.RELEASE_NAME;
const releaseTime = process.env.RELEASE_TIME;
const sha = process.env.RELEASE_SHA;
const tag = `release-${sha.slice(0, 12)}`;
for (const { name, version } of releases) {
const packageTag = `${name}@${version}`;
try {
await github.rest.git.getRef({
...context.repo,
ref: `tags/${packageTag}`,
});
} catch (error) {
if (error.status !== 404) {
throw error;
}
await github.rest.git.createRef({
...context.repo,
ref: `refs/tags/${packageTag}`,
sha,
});
}
}
try {
await github.rest.git.getRef({
...context.repo,
ref: `tags/${tag}`,
});
} catch (error) {
if (error.status !== 404) {
throw error;
}
await github.rest.git.createRef({
...context.repo,
ref: `refs/tags/${tag}`,
sha,
});
}
try {
await github.rest.repos.getReleaseByTag({
...context.repo,
tag,
});
return;
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
const body = releases
.flatMap(({ name, version, changelog }) => [
`## \`${name}@${version}\``,
"",
changelog,
"",
])
.join("\n")
.trim();
await github.rest.repos.createRelease({
...context.repo,
tag_name: tag,
name: `${releaseName} ${releaseTime}`,
body,
prerelease: releases.some(({ version }) => version.includes("-")),
});