Skip to content

Commit e6fd49a

Browse files
committed
Minify JS bundle on release branches
1 parent f4d0a7a commit e6fd49a

3 files changed

Lines changed: 60 additions & 8 deletions

File tree

.github/update-release-branch.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ def run_command(*args):
5656

5757
# Rebuilds the action and commits any changes.
5858
def rebuild_action():
59-
# For backports, the only source-level change vs the source branch is the new version number,
60-
# so we just need to refresh the version embedded in `lib/`.
6159
run_command('npm', 'ci')
60+
# We only expect changes to the JavaScript output, rebuilding e.g. the PR checks is unnecessary.
6261
run_command('npm', 'run', 'build')
6362

6463
run_git('add', '--all')
@@ -450,12 +449,11 @@ def main():
450449
run_git('add', 'CHANGELOG.md')
451450
run_git('commit', '-m', f'Update changelog for v{version}')
452451

453-
if not is_primary_release:
454-
if len(conflicted_files) == 0:
455-
print('Rebuilding the Action.')
456-
rebuild_action()
457-
else:
458-
print(f'Skipping automatic rebuild because the merge produced conflicts in {conflicted_files}.')
452+
if len(conflicted_files) > 0:
453+
print(f'Skipping automatic rebuild because the merge produced conflicts in {conflicted_files}.')
454+
else:
455+
print('Rebuilding the Action.')
456+
rebuild_action()
459457

460458
run_git('push', ORIGIN, new_branch_name)
461459

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ See the [releases page](https://github.com/github/codeql-action/releases) for th
66

77
- _Breaking change_: Bump the minimum required CodeQL bundle version to 2.19.4. [#3894](https://github.com/github/codeql-action/pull/3894)
88
- Add support for SHA-256 Git object IDs. [#3893](https://github.com/github/codeql-action/pull/3893)
9+
- The JavaScript bundle shipped on release branches is now minified, reducing the size of the repository by around 20%. Bundles on `main` remain unminified to avoid merge conflicts between PRs. [#3920](https://github.com/github/codeql-action/pull/3920)
910

1011
## 4.35.5 - 15 May 2026
1112

build.mjs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { execFileSync } from "node:child_process";
12
import { copyFile, readFile, rm, writeFile } from "node:fs/promises";
23
import { basename, dirname, join } from "node:path";
34
import { fileURLToPath } from "node:url";
@@ -13,6 +14,51 @@ const __dirname = dirname(__filename);
1314
const SRC_DIR = join(__dirname, "src");
1415
const OUT_DIR = join(__dirname, "lib");
1516

17+
/**
18+
* Decide whether to minify the bundle.
19+
*
20+
* We deliberately do not minify by default to avoid making every PR's regenerated bundle conflict
21+
* with every other PR. Instead, we minify only when building for a release branch so consumers of
22+
* `github/codeql-action/<action>@vN` get the smaller bundle while day-to-day development on `main`
23+
* stays low-churn.
24+
*
25+
* @returns {boolean}
26+
*/
27+
function shouldMinify() {
28+
const override = process.env.CODEQL_ACTION_MINIFY;
29+
if (override === "true") return true;
30+
if (override === "false") return false;
31+
32+
// In `pull_request` and `merge_group` contexts, we can just look at the base ref.
33+
if (process.env.GITHUB_BASE_REF) {
34+
return process.env.GITHUB_BASE_REF.startsWith("releases/v");
35+
}
36+
37+
// When running locally or in contexts without a base ref (e.g. `push`, `workflow_dispatch`),
38+
// check whether we're running as part of the release automation by looking at the local branch
39+
// name. Mergebacks target `main` and should not be minified, while update and backport branches
40+
// target release branches and should be minified.
41+
const localBranch = getLocalBranchName();
42+
if (localBranch?.startsWith("mergeback/")) return false;
43+
if (localBranch && /^(update|backport)-v\d/.test(localBranch)) return true;
44+
45+
// If we don't seem to be running as part of the release automation, then only minify if we're on
46+
// a release branch.
47+
const refName = process.env.GITHUB_REF_NAME || localBranch;
48+
return !!refName && refName.startsWith("releases/v");
49+
}
50+
51+
function getLocalBranchName() {
52+
try {
53+
return execFileSync("git", ["rev-parse", "--abbrev-ref", "HEAD"], {
54+
encoding: "utf-8",
55+
stdio: ["pipe", "pipe", "ignore"],
56+
}).trim();
57+
} catch {
58+
return undefined;
59+
}
60+
}
61+
1662
/**
1763
* Clean the output directory before building.
1864
*
@@ -201,10 +247,17 @@ const entryPointsPlugin = {
201247
},
202248
};
203249

250+
const minify = shouldMinify();
251+
if (minify) {
252+
// eslint-disable-next-line no-console
253+
console.log("Minification enabled for this build.");
254+
}
255+
204256
const context = await esbuild.context({
205257
entryPoints: [{ in: SHARED_ENTRYPOINT, out: SHARED_ENTRYPOINT }],
206258
bundle: true,
207259
format: "cjs",
260+
minify,
208261
outdir: OUT_DIR,
209262
platform: "node",
210263
external: ["./entry-points"],

0 commit comments

Comments
 (0)