Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add upgrade-node workflow #29

Merged
merged 4 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions .github/scripts/check-node-versions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
const fetch = require("node-fetch"); // @TODO this can be removed once we upgrade to Node 18 and use native fetch

const today = new Date();
const oneMonthFromToday = new Date();
oneMonthFromToday.setDate(today.getDate() + 30);
// console.debug("oneMonthFromToday", oneMonthFromToday.toDateString());

// Code below adapted from https://stackoverflow.com/a/71520193
const versionRegex = /v(\d+)\.(\d+)\.(\d+)/; // 'v16.14.3'

// convert version string to a number for easier sorting
function calcVersion(x) {
const match = x.match(versionRegex);
if (!match) {
throw new Error(`version regex failed to match version string '${x}'`);
}
return +match[1] * 1000000 + +match[2] * 1000 + +match[3];
}

async function getLTSVersions() {
const response = await fetch(
"https://nodejs.org/download/release/index.json",
);
const data = await response.json();
const allLTSVersions = data.filter((item) => item.lts);

// for performance reasons when sorting,
// precalculate an actual version number from the version string
allLTSVersions.forEach(
(item) => (item.numVersion = calcVersion(item.version)),
);
allLTSVersions.sort((a, b) => a.numVersion - b.numVersion);

// console.debug("All LTS versions - sorted oldest first");
// console.debug(allLTSVersions.map(item => item.version));

const firstLTSVersions = {};
allLTSVersions.forEach((item) => {
const majorVersion = `v${item.version.match(versionRegex)[1]}`;
if (!firstLTSVersions[majorVersion]) {
firstLTSVersions[majorVersion] = item.version;
}
});

console.debug("First LTS versions");
console.debug(firstLTSVersions);

return firstLTSVersions;
}

/** Return the earliest supported version whose EOL date is at least a month away */
async function getEarliestSupportedVersion() {
// https://github.com/nodejs/Release/blob/main/schedule.json
const response = await fetch(
"https://raw.githubusercontent.com/nodejs/Release/main/schedule.json",
);
const data = await response.json();
const activelySupportedVersions = Object.entries(data)
.filter(([version, metadata]) => {
return (
new Date(metadata.lts) <= today &&
new Date(metadata.end) > oneMonthFromToday
);
})
.sort((a, b) => new Date(a[1].start) - new Date(b[1].start));

console.debug(
"Actively supported versions with an EOL date at least 1 month away",
);
console.debug(Object.fromEntries(activelySupportedVersions));

return activelySupportedVersions[0][0];
}

async function getDesiredVersion() {
const ltsVersions = await getLTSVersions();
const earliestSupportedVersion = await getEarliestSupportedVersion();
const version = ltsVersions[earliestSupportedVersion];

// console.debug("earliestSupportedVersion", earliestSupportedVersion);
console.log("desired version", version);

return version;
}

module.exports = async ({ github, context, core }) => {
const version = await getDesiredVersion();
const short = version.match(versionRegex)[1];

core.exportVariable("NEW_NODEJS_VERSION", version.slice(1)); // strip the 'v' from the start of the string
core.exportVariable("NEW_NODEJS_VERSION_SHORT", short);
};
60 changes: 60 additions & 0 deletions .github/workflows/upgrade-node.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: upgrade-node
on:
schedule:
- cron: '20 5 * * *'
workflow_dispatch: {}
concurrency: ${{ github.workflow }}-${{ github.ref }}
jobs:
upgrade:
name: Upgrade Node.js
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3
- name: Install
run: yarn install
- name: Get current Node.js version
id: current_version
run: |-
ENGINES_NODE_VERSION=$(npm pkg get engines.node | tr -d '"')
CURRENT_VERSION=$(cut -d " " -f 2 <<< "$ENGINES_NODE_VERSION")
CURRENT_VERSION_SHORT=$(cut -d "." -f 1 <<< "$CURRENT_VERSION")
echo "CURRENT_NODEJS_VERSION=$CURRENT_VERSION" >> $GITHUB_ENV
echo "CURRENT_NODEJS_VERSION_SHORT=$CURRENT_VERSION_SHORT" >> $GITHUB_ENV
echo "value=$CURRENT_VERSION" >> $GITHUB_OUTPUT
echo "short=$CURRENT_VERSION_SHORT" >> $GITHUB_OUTPUT
- name: Get the earliest supported Node.js version whose EOL date is at least a month away
uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1
with:
script: |-
const script = require('./.github/scripts/check-node-versions.js')
await script({github, context, core})
- name: Update the package with the new minimum Node version and update @types/node
if: env.CURRENT_NODEJS_VERSION_SHORT != env.NEW_NODEJS_VERSION_SHORT
run: |-
npm pkg set engines.node=">= $NEW_NODEJS_VERSION"
yarn add -D @types/node@^$NEW_NODEJS_VERSION_SHORT
# If there are other steps you need to take in your project to update Node.js everywhere, add those here
- name: Get values for pull request
id: latest_version
if: env.CURRENT_NODEJS_VERSION_SHORT != env.NEW_NODEJS_VERSION_SHORT
run: |-
echo "value=$NEW_NODEJS_VERSION" >> $GITHUB_OUTPUT
echo "short=$NEW_NODEJS_VERSION_SHORT" >> $GITHUB_OUTPUT
- name: Create Pull Request
if: env.CURRENT_NODEJS_VERSION_SHORT != env.NEW_NODEJS_VERSION_SHORT
uses: peter-evans/create-pull-request@284f54f989303d2699d373481a0cfa13ad5a6666 # v5.0.1
with:
commit-message: "chore!: increase minimum supported Node.js version to ${{ steps.latest_version.outputs.short }}"
branch: auto/upgrade-node-${{ steps.latest_version.outputs.short }}
base: main
title: "chore!: increase minimum supported Node.js version to ${{ steps.latest_version.outputs.short }}"
body: This PR increases the minimum supported Node.js version to `${{ steps.latest_version.outputs.value }}` from `${{ steps.current_version.outputs.value }}` because version ${{ steps.current_version.outputs.short }} is less than 30 days away from EOL.
labels: automerge,automated,security
token: ${{ secrets.TEAM_TF_CDK }}
author: team-tf-cdk <[email protected]>
committer: team-tf-cdk <[email protected]>
signoff: true
delete-branch: true
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@types/node": "^15.6.2",
"cdktf-cli": "0.18.2",
"jest": "^29.6.4",
"node-fetch": "~2",
"ts-jest": "^29.1.1",
"ts-node": "^10.5.0",
"typescript": "^4.3.2"
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10282,6 +10282,13 @@ node-fetch@^2.6.1, node-fetch@^2.6.11, node-fetch@^2.6.7:
dependencies:
whatwg-url "^5.0.0"

node-fetch@~2:
version "2.7.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
dependencies:
whatwg-url "^5.0.0"

node-forge@^0.10.0:
version "0.10.0"
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.10.0.tgz#32dea2afb3e9926f02ee5ce8794902691a676bf3"
Expand Down