Skip to content
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
6 changes: 6 additions & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ variable "CACHE_WEEK" {
default = ""
}


variable "BUILDPACK_DEBUG" {
default = ""
}

group "default" {
targets = ["build-docker"]
}
Expand All @@ -37,6 +42,7 @@ target "settings" {
context = "."
args = {
APT_HTTP_PROXY = "${APT_HTTP_PROXY}"
BUILDPACK_DEBUG = "${BUILDPACK_DEBUG}"
}
}

Expand Down
103 changes: 61 additions & 42 deletions src/usr/local/buildpack/tools/node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

set -e

require_root
check_semver $TOOL_VERSION

if [[ ! "${MAJOR}" || ! "${MINOR}" ]]; then
Expand All @@ -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 () {
Expand All @@ -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)
19 changes: 17 additions & 2 deletions src/usr/local/buildpack/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
15 changes: 12 additions & 3 deletions test/node/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -107,23 +110,24 @@ 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

RUN set -ex; \
npm --version; \
command -v npm;


RUN set -ex; cd test/a; npm i

RUN npm install -g yarn
Expand Down Expand Up @@ -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
Expand Down