diff --git a/docker-bake.hcl b/docker-bake.hcl index f97df6084a..3d89643559 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -16,6 +16,11 @@ variable "CACHE_WEEK" { default = "" } + +variable "BUILDPACK_DEBUG" { + default = "" +} + group "default" { targets = ["build-docker"] } @@ -37,6 +42,7 @@ target "settings" { context = "." args = { APT_HTTP_PROXY = "${APT_HTTP_PROXY}" + BUILDPACK_DEBUG = "${BUILDPACK_DEBUG}" } } diff --git a/src/usr/local/buildpack/tools/node.sh b/src/usr/local/buildpack/tools/node.sh index fe05aeace2..773020f0a9 100644 --- a/src/usr/local/buildpack/tools/node.sh +++ b/src/usr/local/buildpack/tools/node.sh @@ -2,7 +2,6 @@ set -e -require_root check_semver $TOOL_VERSION if [[ ! "${MAJOR}" || ! "${MINOR}" ]]; then @@ -11,18 +10,26 @@ if [[ ! "${MAJOR}" || ! "${MINOR}" ]]; then fi NODE_DISTRO=linux-x64 -NODE_INSTALL_DIR=/usr/local/node/${TOOL_VERSION} +tool_path=$(find_tool_path) +INSTALL_DIR=$(get_install_dir) +PREFIX="${USER_HOME}/.npm-global" -if [[ -d "${NODE_INSTALL_DIR}" ]]; then - echo "Skipping, already installed" - exit 0 -fi function update_env () { - PATH="${1}/bin:${PATH}" - link_wrapper ${TOOL_NAME} - link_wrapper npm - link_wrapper npx + reset_tool_env + + link_wrapper ${TOOL_NAME} $tool_path/bin + link_wrapper npm $tool_path/bin + link_wrapper npx $tool_path/bin + + export_tool_path "${PREFIX}/bin" + + cat >> $(find_tool_env) <<- EOM +# openshift override unknown user home +if [ "\${EUID}" != 0 ] && [ "\${EUID}" != ${USER_ID} ]; then + export NPM_CONFIG_PREFIX="${PREFIX}" +fi +EOM } function prepare_prefix () { @@ -34,51 +41,63 @@ function prepare_prefix () { function prepare_global_config () { local prefix=${1} prepare_prefix ${prefix} - npm config set prefix ${prefix} --global + mkdir -p ${tool_path}/etc + echo "prefix = \"${prefix}\"" >> ${tool_path}/etc/npmrc } function prepare_user_config () { local prefix=${1} - prepare_prefix ${prefix} - export_path "${prefix}/bin" - chown -R ${USER_ID} ${prefix} - chmod -R g+w ${prefix} + if [[ -r "${USER_HOME}/.npmrc" && $(cat ${USER_HOME}/.npmrc | grep 'prefix') ]]; then + return + fi - su $USER_NAME -c "npm config set prefix ${prefix}" - cat >> $ENV_FILE <<- EOM -# openshift override unknown user home -if [ "\${EUID}" != 0 ] && [ "\${EUID}" != ${USER_ID} ]; then - export NPM_CONFIG_PREFIX="${prefix}" -fi -EOM + prepare_prefix ${prefix} + echo "prefix = \"${prefix}\"" >> ${USER_HOME}/.npmrc + chown -R ${USER_ID} ${prefix} ${USER_HOME}/.npmrc + chmod -R g+w ${prefix} ${USER_HOME}/.npmrc } -mkdir -p $NODE_INSTALL_DIR +if [[ -z "${tool_path}" ]]; then + base_path=${INSTALL_DIR}/${TOOL_NAME} + tool_path=${base_path}/${TOOL_VERSION} + npm=${tool_path}/bin/npm -curl -sLo node.tar.xz https://nodejs.org/dist/v${TOOL_VERSION}/node-v${TOOL_VERSION}-${NODE_DISTRO}.tar.xz -tar -C ${NODE_INSTALL_DIR} --strip 1 -xf node.tar.xz -rm node.tar.xz + mkdir -p $tool_path -update_env ${NODE_INSTALL_DIR} + file=/tmp/${TOOL_NAME}.tar.xz -if [[ ${MAJOR} < 15 ]]; then - # update to latest node-gyp to fully support python3 - ${NODE_INSTALL_DIR}/bin/npm explore npm -g -- npm install --cache /tmp/empty-cache node-gyp@latest - rm -rf /tmp/empty-cache -fi + curl -sLo ${file} https://nodejs.org/dist/v${TOOL_VERSION}/node-v${TOOL_VERSION}-${NODE_DISTRO}.tar.xz + tar -C ${tool_path} --strip 1 -xf ${file} + rm ${file} + if [[ $EUID -eq 0 ]]; then + # redirect root install + prepare_global_config /usr/local -# redirect root install -prepare_global_config /usr/local + # redirect user install + prepare_user_config ${PREFIX} + else + # redirect user install + prepare_global_config ${PREFIX} + fi -# redirect user install -prepare_user_config "${USER_HOME}/.npm-global" + # required for npm + link_wrapper ${TOOL_NAME} $tool_path/bin -echo node: $(node --version) $(command -v node) -echo npm: $(npm --version) $(command -v npm) + if [[ ${MAJOR} < 15 ]]; then + # update to latest node-gyp to fully support python3 + NPM_CONFIG_PREFIX=$tool_path $npm explore npm -g -- $npm install --cache /tmp/empty-cache node-gyp@latest + rm -rf /tmp/empty-cache + fi -# Clean download cache -npm cache clean --force + # Clean download cache + NPM_CONFIG_PREFIX=$tool_path $npm cache clean --force -# Clean node-gyp cache -rm -rf /root/.cache + # Clean node-gyp cache + rm -rf $HOME/.cache +fi + +update_env ${tool_path} + +echo node: $(node --version) $(command -v node) +echo npm: $(npm --version) $(command -v npm) diff --git a/src/usr/local/buildpack/util.sh b/src/usr/local/buildpack/util.sh index 0dbeccaf3c..f9d1f46cc4 100644 --- a/src/usr/local/buildpack/util.sh +++ b/src/usr/local/buildpack/util.sh @@ -43,6 +43,15 @@ function reset_tool_env () { fi } +function find_tool_env () { + local install_dir=$(get_install_dir) + if [[ -z "${TOOL_NAME+x}" ]]; then + echo "No TOOL_NAME defined - skipping: ${TOOL_NAME}" >&2 + exit 1; + fi + + echo "$install_dir/env.d/${TOOL_NAME}.sh" +} function export_tool_env () { local install_dir=$(get_install_dir) @@ -92,8 +101,14 @@ EOM function link_wrapper () { local install_dir=$(get_install_dir) local TARGET="${install_dir}/bin/${1}" - local SOURCE=$(command -v ${1}) - check_command $1 + local SOURCE=$2 + if [[ -z "$SOURCE" ]]; then + SOURCE=$(command -v ${1}) + fi + if [[ -d "$SOURCE" ]]; then + SOURCE=$SOURCE/${1} + fi + check_command $SOURCE ln -sf $SOURCE $TARGET } diff --git a/test/node/Dockerfile b/test/node/Dockerfile index 9b2f1cf7f4..f2c38fc68d 100644 --- a/test/node/Dockerfile +++ b/test/node/Dockerfile @@ -1,7 +1,10 @@ ARG IMAGE=containerbase/buildpack +ARG BUILDPACK_DEBUG + FROM ${IMAGE} as build ARG APT_HTTP_PROXY +ARG BUILDPACK_DEBUG # renovate: datasource=node RUN install-tool node v16.13.1 @@ -107,15 +110,17 @@ FROM ${IMAGE} as testd ARG APT_HTTP_PROXY +RUN touch /.dummy + +USER 1000 + # renovate: datasource=node RUN install-tool node v17.0.1 -RUN touch /.dummy # renovate: datasource=npm RUN install-tool yarn 1.22.17 -USER 1000 COPY --chown=1000:0 test test @@ -123,7 +128,6 @@ RUN set -ex; \ npm --version; \ command -v npm; - RUN set -ex; cd test/a; npm i RUN npm install -g yarn @@ -216,9 +220,14 @@ RUN set -ex; cd a; yarn-slim install #-------------------------------------- FROM ${IMAGE} as testi +ARG BUILDPACK_DEBUG + # don't update!! +RUN install-tool node v14.18.1 RUN install-tool node v14.18.2 +RUN cat $USER_HOME/.npmrc | grep "prefix = \"$USER_HOME/.npm-global\"" + RUN touch /.dummy USER 1000