Skip to content

Commit 62262f4

Browse files
Pehesi97SimenB
andauthored
Automating docker-node part of the official Node Docker images release process (#1646)
Co-authored-by: Simen Bekkhus <[email protected]> Co-authored-by: Pedro Henrique <[email protected]>
1 parent 652749b commit 62262f4

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Automatically update Docker image versions
2+
3+
on:
4+
schedule:
5+
- cron: "*/15 * * * *"
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
if: github.repository_owner == 'nodejs'
11+
12+
steps:
13+
- uses: actions/checkout@v3
14+
15+
- name: Run automation script
16+
uses: actions/github-script@v6
17+
id: updt
18+
with:
19+
script: |
20+
const { default: script } = await import(`${process.env.GITHUB_WORKSPACE}/build-automation.mjs`);
21+
await script(github);
22+
23+
- name: Create update PR
24+
id: cpr
25+
uses: peter-evans/create-pull-request@v3
26+
with:
27+
token: ${{ secrets.GITHUB_TOKEN }}
28+
branch: update-branch
29+
base: main
30+
commit-message: "Update to ${{ steps.updt.outputs.updated-versions }}"
31+
title: "Update to ${{ steps.updt.outputs.updated-versions }}"
32+
delete-branch: true
33+
team-reviewers: |
34+
@nodejs/docker
35+
36+
- name: Check CI status periodically
37+
uses: actions/github-script@v6
38+
with:
39+
script: |
40+
const { default: script } = await import(`${process.env.GITHUB_WORKSPACE}/check-pr-status.mjs`);
41+
await script(github, '${{ github.repository }}', ${{ steps.cpr.outputs.pull-request-number }});
42+
43+
- name: Auto-approve the PR
44+
uses: juliangruber/approve-pull-request-action@v1
45+
with:
46+
# Cannot use `GITHUB_TOKEN` as it's not allowed to approve own PR
47+
github-token: ${{ secrets.GH_API_TOKEN }}
48+
number: ${{ steps.cpr.outputs.pull-request-number }}
49+
50+
- name: Merge PR
51+
uses: juliangruber/merge-pull-request-action@v1
52+
with:
53+
github-token: ${{ secrets.GITHUB_TOKEN }}
54+
number: ${{ steps.cpr.outputs.pull-request-number }}

build-automation.mjs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { promisify } from "util";
2+
3+
import child_process from "child_process";
4+
5+
const exec = promisify(child_process.exec);
6+
7+
// a function that queries the Node.js release website for new versions,
8+
// compare the available ones with the ones we use in this repo
9+
// and returns whether we should update or not
10+
const checkIfThereAreNewVersions = async (github) => {
11+
try {
12+
const { stdout: versionsOutput } = await exec(". ./functions.sh && get_versions", { shell: "bash" });
13+
14+
const supportedVersions = versionsOutput.trim().split(" ");
15+
16+
let latestSupportedVersions = {};
17+
18+
for (let supportedVersion of supportedVersions) {
19+
const { stdout } = await exec(`ls ${supportedVersion}`);
20+
21+
const { stdout: fullVersionOutput } = await exec(`. ./functions.sh && get_full_version ./${supportedVersion}/${stdout.trim().split("\n")[0]}`, { shell: "bash" });
22+
23+
console.log(fullVersionOutput);
24+
25+
latestSupportedVersions[supportedVersion] = { fullVersion: fullVersionOutput.trim() };
26+
}
27+
28+
const { data: availableVersionsJson } = await github.request('https://nodejs.org/download/release/index.json');
29+
30+
// filter only more recent versions of availableVersionsJson for each major version in latestSupportedVersions' keys
31+
// e.g. if latestSupportedVersions = { "12": "12.22.10", "14": "14.19.0", "16": "16.14.0", "17": "17.5.0" }
32+
// and availableVersions = ["Node.js 12.22.10", "Node.js 12.24.0", "Node.js 14.19.0", "Node.js 14.22.0", "Node.js 16.14.0", "Node.js 16.16.0", "Node.js 17.5.0", "Node.js 17.8.0"]
33+
// return { "12": "12.24.0", "14": "14.22.0", "16": "16.16.0", "17": "17.8.0" }
34+
35+
let filteredNewerVersions = {};
36+
37+
for (let availableVersion of availableVersionsJson) {
38+
const [availableMajor, availableMinor, availablePatch] = availableVersion.version.split("v")[1].split(".");
39+
if (latestSupportedVersions[availableMajor] == null) {
40+
continue;
41+
}
42+
const [_latestMajor, latestMinor, latestPatch] = latestSupportedVersions[availableMajor].fullVersion.split(".");
43+
if (latestSupportedVersions[availableMajor] && (Number(availableMinor) > Number(latestMinor) || (availableMinor === latestMinor && Number(availablePatch) > Number(latestPatch)))) {
44+
filteredNewerVersions[availableMajor] = { fullVersion: `${availableMajor}.${availableMinor}.${availablePatch}` };
45+
}
46+
}
47+
48+
return {
49+
shouldUpdate: Object.keys(filteredNewerVersions).length > 0 && JSON.stringify(filteredNewerVersions) !== JSON.stringify(latestSupportedVersions),
50+
versions: filteredNewerVersions,
51+
}
52+
} catch (error) {
53+
console.error(error);
54+
process.exit(1);
55+
}
56+
};
57+
58+
// a function that queries the Node.js unofficial release website for new musl versions and security releases,
59+
// and returns relevant information
60+
const checkForMuslVersionsAndSecurityReleases = async (github, versions) => {
61+
try {
62+
const { data: unofficialBuildsIndexText } = await github.request('https://unofficial-builds.nodejs.org/download/release/index.json');
63+
64+
for (let version of Object.keys(versions)) {
65+
const { data: unofficialBuildsWebsiteText } = await github.request(`https://unofficial-builds.nodejs.org/download/release/v${versions[version].fullVersion}`);
66+
67+
versions[version].muslBuildExists = unofficialBuildsWebsiteText.includes("musl");
68+
versions[version].isSecurityRelease = unofficialBuildsIndexText.find(indexVersion => indexVersion.version === `v${versions[version].fullVersion}`)?.security;
69+
}
70+
return versions;
71+
} catch (error) {
72+
console.error(error);
73+
process.exit(1);
74+
}
75+
};
76+
77+
export default async function(github) {
78+
// if there are no new versions, exit gracefully
79+
// if there are new versions,
80+
// check for musl builds
81+
// then run update.sh
82+
const { shouldUpdate, versions } = await checkIfThereAreNewVersions(github);
83+
84+
if (!shouldUpdate) {
85+
console.log("No new versions found. No update required.");
86+
process.exit(0);
87+
} else {
88+
const newVersions = await checkForMuslVersionsAndSecurityReleases(github, versions);
89+
let updatedVersions = [];
90+
for (let version of Object.keys(newVersions)) {
91+
if (newVersions[version].muslBuildExists) {
92+
const { stdout } = await exec(`./update.sh ${newVersions[version].isSecurityRelease ? "-s " : ""}${version}`);
93+
console.log(stdout);
94+
updatedVersions.push(newVersions[version].fullVersion);
95+
} else {
96+
console.log(`There's no musl build for version ${newVersions[version].fullVersion} yet.`);
97+
process.exit(0);
98+
}
99+
}
100+
console.log(`::set-output name=updated-versions::${updatedVersions.join(',')}`);
101+
const { stdout } = (await exec(`git diff`));
102+
console.log(stdout);
103+
}
104+
}

check-pr-status.mjs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// fetch /repos/{owner}/{repo}/pulls/{pull_number}
2+
// and check its mergeable_state
3+
// if "clean", exit with status code 0
4+
// else exit with error
5+
import { setTimeout } from 'timers/promises';
6+
7+
const tries = 10;
8+
const retryDelay = 30000;
9+
10+
export default async function(github, repository, pull_number) {
11+
const [owner, repo] = repository.split('/');
12+
await setTimeout(retryDelay);
13+
14+
for (let t = 0; t < tries; t++) {
15+
try {
16+
const { data } = await github.rest.pulls.get({owner, repo, pull_number})
17+
18+
console.log(data);
19+
if (data.mergeable_state === 'clean') {
20+
process.exit(0);
21+
}
22+
await setTimeout(retryDelay);
23+
} catch (error) {
24+
console.error(error);
25+
process.exit(1);
26+
}
27+
}
28+
process.exit(1);
29+
}

functions.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env bash
22
#
33
# Utlity functions
4+
# Don't change this file unless needed
5+
# The GitHub Action for automating new builds rely on this file
46

57
info() {
68
printf "%s\\n" "$@"

0 commit comments

Comments
 (0)