[CI] Install global npm modules with a retry and failsafe (#135437)

This commit is contained in:
Brian Seeders 2022-06-30 10:46:47 -04:00 committed by GitHub
parent 112efc8ac6
commit 82b5d8e120
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 9 deletions

View file

@ -1,5 +1,9 @@
#!/usr/bin/env bash
set -euo pipefail
source .buildkite/scripts/common/util.sh
echo "--- Setup Node"
NODE_VERSION="$(cat "$KIBANA_DIR/.node-version")"
@ -61,14 +65,7 @@ YARN_VERSION=$(node -e "console.log(String(require('./package.json').engines.yar
export YARN_VERSION
if [[ ! $(which yarn) || $(yarn --version) != "$YARN_VERSION" ]]; then
rm -rf "$(npm root -g)/yarn" # in case the directory is in a bad state
if [[ ! $(npm install -g "yarn@^${YARN_VERSION}") ]]; then
# If this command is terminated early, e.g. because the build was cancelled in buildkite,
# a yarn directory is left behind in a bad state that can cause all subsequent installs to fail
rm -rf "$(npm root -g)/yarn"
echo "Trying again to install yarn..."
npm install -g "yarn@^${YARN_VERSION}"
fi
npm_install_global yarn "^$YARN_VERSION"
fi
yarn config set yarn-offline-mirror "$YARN_OFFLINE_CACHE"

View file

@ -141,3 +141,30 @@ set_git_merge_base() {
export GITHUB_PR_MERGE_BASE
}
# If npm install is terminated early, e.g. because the build was cancelled in buildkite,
# a package directory is left behind in a bad state that can cause all subsequent installs to fail
# So this function contains some cleanup/retry logic to try to recover from this kind of situation
npm_install_global() {
package="$1"
version="${2:-latest}"
toInstall="$package@$version"
npmRoot=$(npm root -g)
packageRoot="${npmRoot:?}/$package"
# The success flag file exists just to try to make sure we know that the full install was done
# For example, if a job terminates in the middle of npm install, a directory could be left behind that we don't know the state of
successFlag="${packageRoot:?}/.install-success"
if [[ -d "$packageRoot" && ! -f "$successFlag" ]]; then
echo "Removing existing package directory $packageRoot before install, seems previous installation was not successful"
rm -rf "$packageRoot"
fi
if [[ ! $(npm install -g "$toInstall" && touch "$successFlag") ]]; then
rm -rf "$packageRoot"
echo "Trying again to install $toInstall..."
npm install -g "$toInstall" && touch "$successFlag"
fi
}

View file

@ -12,7 +12,16 @@ BUILDKITE_TOKEN="$(retry 5 5 vault read -field=buildkite_token_all_jobs secret/k
export BUILDKITE_TOKEN
echo '--- Install/build buildkite dependencies'
npm install -g ts-node
# `rm -rf <ts-node node_modules dir>; npm install -g ts-node` will cause ts-node bin files to be messed up
# but literally just calling `npm install -g ts-node` a second time fixes it
# this is only on newer versions of npm
npm_install_global ts-node
if ! ts-node --version; then
npm_install_global ts-node
ts-node --version;
fi
cd '.buildkite'
retry 5 15 npm ci
cd ..