From f3e98aced27742c8c077b5ef8382d307b90514f9 Mon Sep 17 00:00:00 2001 From: Gio Lodi Date: Thu, 30 May 2024 14:00:17 +1000 Subject: [PATCH 1/5] Remove a trailing whitespace --- bin/install_npm_packages | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/install_npm_packages b/bin/install_npm_packages index fbb5cf98..8f290da0 100755 --- a/bin/install_npm_packages +++ b/bin/install_npm_packages @@ -6,7 +6,7 @@ if [ ! -f package.json ]; then exit 1 fi -# Ensure package-lock.json is present +# Ensure package-lock.json is present if [ ! -f package-lock.json ]; then echo "No valid package-lock.json file found" exit 1 From 53d9684bda34035c51c7bc753a3d6936567de977 Mon Sep 17 00:00:00 2001 From: Gio Lodi Date: Thu, 30 May 2024 14:07:07 +1000 Subject: [PATCH 2/5] Tweak error messages in `install_npm_packages` --- bin/install_npm_packages | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/install_npm_packages b/bin/install_npm_packages index 8f290da0..e7499277 100755 --- a/bin/install_npm_packages +++ b/bin/install_npm_packages @@ -2,13 +2,13 @@ # Ensure package.json is present if [ ! -f package.json ]; then - echo "No valid package.json file found" + echo "No package.json found!" exit 1 fi # Ensure package-lock.json is present if [ ! -f package-lock.json ]; then - echo "No valid package-lock.json file found" + echo "No package-lock.json found!" exit 1 fi From 84d6b959b1a303d91d83861902e623b146f186df Mon Sep 17 00:00:00 2001 From: Gio Lodi Date: Thu, 30 May 2024 14:07:46 +1000 Subject: [PATCH 3/5] Add command to run `npm ci` --- bin/install_npm_packages_clean | 71 ++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 bin/install_npm_packages_clean diff --git a/bin/install_npm_packages_clean b/bin/install_npm_packages_clean new file mode 100755 index 00000000..e2bd9f26 --- /dev/null +++ b/bin/install_npm_packages_clean @@ -0,0 +1,71 @@ +#!/bin/bash -eu + +# Ensure package.json is present +if [ ! -f package.json ]; then + echo "No package.json found!" + exit 1 +fi + +# Ensure package-lock.json is present +# TODO: What to do with yarn-based projects? Make the hash on lockfile optional? +if [ ! -f package-lock.json ]; then + echo "No package-lock.json found!" + exit 1 +fi + +PLATFORM=$(uname -s) +ARCHITECTURE=$(uname -m) +NODE_VERSION=$(node --version) +PACKAGE_HASH=$(hash_file package-lock.json) + +# TODO: This comes from Studio. Figure out if it's standard in Node or not. +if [ -d patches ]; then + PATCHES_HASH=$(hash_directory patches/) +else + PATCHES_HASH=nopatch +fi + +CACHEKEY="$BUILDKITE_PIPELINE_SLUG-npm-$PLATFORM-$ARCHITECTURE-node-$NODE_VERSION-$PACKAGE_HASH-$PATCHES_HASH" + +LOCAL_NPM_CACHE=./vendor/npm +mkdir -p $LOCAL_NPM_CACHE +echo "--- :npm: Set npm to use $LOCAL_NPM_CACHE for cache" +npm set cache $LOCAL_NPM_CACHE +echo "npm cache set to $(npm get cache)" + +echo "--- :npm: Restore npm cache if present" +restore_cache "$CACHEKEY" + +echo "--- :npm: Install Node dependencies" + +# To avoid constant ECONNRESET errors a limit is set for Linux, +# as this is not happening with the Mac jobs. +# This issue is being tracked here: +# https://github.com/npm/cli/issues/4652 +if [ "$PLATFORM" = "Linux" ]; then + MAX_SOCKETS=1 +else + # Default value from npm. + # TODO: Link to source + MAX_SOCKETS=15 +fi + +npm ci \ + --unsafe-perm \ + --prefer-offline \ + --no-audit \ + --no-progress \ + --maxsockets "$MAX_SOCKETS" \ + "$@" # TODO: Use approach from other commands where only a subset of flags are allowed + +echo "--- :npm: Save cache if necessary" +# Notice that we don't cache the local node_modules. +# Those get regenerated by npm ci as per Node reccomendations. +# What we are caching is the root npm folder, which stores pacakge downloads so they are available if the package.json resolution demands them. +# +# npm stores temporary files in its cache that we don't want to extract because they might run into naming conflicts. +# So, before archiving it, we remove those tmp files. +# +# Example: https://buildkite.com/automattic/gutenberg-mobile/builds/8857#018e37eb-7afc-4280-b736-cba76f02f1a3/524 +rm -rf "$LOCAL_NPM_CACHE/_cacache/tmp" +save_cache "$LOCAL_NPM_CACHE" "$CACHEKEY" From 574c11b9189d85c1cf1748a83847740af8170945 Mon Sep 17 00:00:00 2001 From: Gio Lodi Date: Mon, 3 Jun 2024 11:20:20 +1000 Subject: [PATCH 4/5] Remove note about `yarn` from `npm ci` command If a project uses `yarn` it should not use the `npm ci` command in CI! --- bin/install_npm_packages_clean | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/install_npm_packages_clean b/bin/install_npm_packages_clean index e2bd9f26..6bbcc616 100755 --- a/bin/install_npm_packages_clean +++ b/bin/install_npm_packages_clean @@ -7,7 +7,6 @@ if [ ! -f package.json ]; then fi # Ensure package-lock.json is present -# TODO: What to do with yarn-based projects? Make the hash on lockfile optional? if [ ! -f package-lock.json ]; then echo "No package-lock.json found!" exit 1 From 154bde21a24a68aea98b566d074e7ee01ee3d9ac Mon Sep 17 00:00:00 2001 From: Gio Lodi Date: Mon, 3 Jun 2024 11:20:51 +1000 Subject: [PATCH 5/5] Add documentation for patches support to `npm ci` command See https://github.com/Automattic/a8c-ci-toolkit-buildkite-plugin/pull/96#discussion_r1620449626 Co-authored-by: Olivier Halligon --- bin/install_npm_packages_clean | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/install_npm_packages_clean b/bin/install_npm_packages_clean index 6bbcc616..646a0f27 100755 --- a/bin/install_npm_packages_clean +++ b/bin/install_npm_packages_clean @@ -17,7 +17,7 @@ ARCHITECTURE=$(uname -m) NODE_VERSION=$(node --version) PACKAGE_HASH=$(hash_file package-lock.json) -# TODO: This comes from Studio. Figure out if it's standard in Node or not. +# See https://www.npmjs.com/package/patch-package#circleci if [ -d patches ]; then PATCHES_HASH=$(hash_directory patches/) else