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

ci(update): port update.sh to nodejs #1368

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
126 changes: 0 additions & 126 deletions functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -153,44 +153,6 @@ function get_versions() {
fi
}

function is_alpine() {
local variant
variant=${1}
shift

if [ "${variant}" = "${variant#alpine}" ]; then
return 1
fi
}

function is_debian() {
local variant
variant=$1
shift

IFS=' ' read -ra debianVersions <<< "$(get_config "./" "debian_versions")"
for d in "${debianVersions[@]}"; do
if [ "${d}" = "${variant}" ]; then
return 0
fi
done
return 1
}

function is_debian_slim() {
local variant
variant=$1
shift

IFS=' ' read -ra debianVersions <<< "$(get_config "./" "debian_versions")"
for d in "${debianVersions[@]}"; do
if [ "${d}-slim" = "${variant}" ]; then
return 0
fi
done
return 1
}

function get_fork_name() {
local version
version=$1
Expand All @@ -202,24 +164,6 @@ function get_fork_name() {
fi
}

function get_full_tag() {
local variant
local tag
local full_tag
variant="$1"
shift
tag="$1"
shift
if [ -z "${variant}" ]; then
full_tag="${tag}"
elif [ "${variant}" = "default" ]; then
full_tag="${tag}"
else
full_tag="${tag}-${variant}"
fi
echo "${full_tag}"
}

function get_full_version() {
local version
version=$1
Expand All @@ -246,25 +190,6 @@ function get_major_minor_version() {
echo "$(echo "${fullversion}" | cut -d'.' -f1).$(echo "${fullversion}" | cut -d'.' -f2)"
}

function get_path() {
local version
local variant
local path
version="$1"
shift
variant="$1"
shift

if [ -z "${variant}" ]; then
path="${version}/${variant}"
elif [ "${variant}" = "default" ]; then
path="${version}"
else
path="${version}/${variant}"
fi
echo "${path}"
}

function get_tag() {
local version
version=$1
Expand Down Expand Up @@ -308,54 +233,3 @@ function sort_versions() {

echo "${sorted[@]}"
}

function commit_range() {
local commit_id_end=${1}
shift
local commit_id_start=${1}

if [ -z "${commit_id_start}" ]; then
if [ -z "${commit_id_end}" ]; then
echo "HEAD~1..HEAD"
elif [[ "${commit_id_end}" =~ .. ]]; then
echo "${commit_id_end}"
else
echo "${commit_id_end}~1..${commit_id_end}"
fi
else
echo "${commit_id_end}..${commit_id_start}"
fi
}

function images_updated() {
local commit_range
local versions
local images_changed

commit_range="$(commit_range "$@")"

IFS=' ' read -ra versions <<< "$(
IFS=','
get_versions
)"
images_changed=$(git diff --name-only "${commit_range}" "${versions[@]}")

if [ -z "${images_changed}" ]; then
return 1
fi
return 0
}

function tests_updated() {
local commit_range
local test_changed

commit_range="$(commit_range "$@")"

test_changed=$(git diff --name-only "${commit_range}" test*)

if [ -z "${test_changed}" ]; then
return 1
fi
return 0
}
23 changes: 2 additions & 21 deletions genMatrix.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
'use strict';
const path = require('path');
const fs = require('fs');
const { getAllDockerfiles, getDockerfileNodeVersion } = require('./utils');

const testFiles = [
'genMatrix.js',
'.github/workflows/build-test.yml',
];

const nodeDirRegex = /^\d+$/;

const areTestFilesChanged = (changedFiles) => changedFiles
.some((file) => testFiles.includes(file));

// Returns a list of the child directories in the given path
const getChildDirectories = (parent) => fs.readdirSync(parent, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map(({ name }) => path.resolve(parent, name));

const getNodeVerionDirs = (base) => getChildDirectories(base)
.filter((childPath) => nodeDirRegex.test(path.basename(childPath)));

// Returns the paths of Dockerfiles that are at: base/*/Dockerfile
const getDockerfilesInChildDirs = (base) => getChildDirectories(base)
.map((childDir) => path.resolve(childDir, 'Dockerfile'));

const getAllDockerfiles = (base) => getNodeVerionDirs(base).flatMap(getDockerfilesInChildDirs);

const getAffectedDockerfiles = (filesAdded, filesModified, filesRenamed) => {
const files = [
...filesAdded,
Expand All @@ -52,13 +36,10 @@ const getAffectedDockerfiles = (filesAdded, filesModified, filesRenamed) => {
];
};

const getFullNodeVersionFromDockerfile = (file) => fs.readFileSync(file, 'utf8')
.match(/^ENV NODE_VERSION (\d*\.*\d*\.\d*)/m)[1];

const getDockerfileMatrixEntry = (file) => {
const [variant] = path.dirname(file).split(path.sep).slice(-1);

const version = getFullNodeVersionFromDockerfile(file);
const version = getDockerfileNodeVersion(file);

return {
version,
Expand Down
62 changes: 62 additions & 0 deletions update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env node
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work on windows? requiring node update.js is probably better?

'use strict';
const update = require('./updateLib');

const usage = `
Update the node docker images.

Usage:
./update.js [ OPTIONS ]

OPTIONS:
-h, --help\tthis message
-a, --all\tupdate all images even if no node version update`;

const printUsage = () => {
console.log(usage);
};

const runUpdate = async (updateAll) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the Yarn part would be a separate parameter here, defaulting to true

const updated = await update(updateAll);

updated.forEach(({ file }) => {
console.log('Updated', file);
});

if (!updated.length) {
console.log('Nothing updated');
}
};

const main = async () => {
if (process.argv.length > 3) {
printUsage();
process.exit(1);
}

if (process.argv.length === 2) {
await runUpdate(false);
return;
}

switch (process.argv[2]) {
case '-a':
case '--all':
await runUpdate(true);
return;

case '-h':
case '--help':
printUsage();
return;

default:
printUsage();
process.exit(1);
}
};

main().catch((e) => {
console.error(e);
process.exit(1);
});
Loading