I have a buildspec (v 0.2) file that will be deployed and built using CodeBuild. Inside of my deploy file I am using a very specific image (cant be changed) and it is an alpine linux
container. In there, I need to run some npm commands in the build step within the deploy.yml file. The image I am using does not support the runtime_version
option so I am trying to install node 16
directly in the phases -> install -> commands:
section of the file. But when I run my deploy step, it is failing to properly link nodejs because when I run node -v
the CodeBuild logs indicate that node is not found
. What do I need to tweak here to make the deploy find node and npm? Thanks.
Error output from logs:
[Container] 2023/06/22 17:25:22 Running command apk update
fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/community/x86_64/APKINDEX.tar.gz
v3.17.4-19-g2ae254083c3 [https://dl-cdn.alpinelinux.org/alpine/v3.17/main]
v3.17.4-16-gf6cb711c1a3 [https://dl-cdn.alpinelinux.org/alpine/v3.17/community]
OK: 17840 distinct packages available
[Container] 2023/06/22 17:25:23 Running command apk add --no-cache curl python3 make g++
fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.17/community/x86_64/APKINDEX.tar.gz
(1/4) Installing libstdc++-dev (12.2.1_git20220924-r4)
(2/4) Installing libc-dev (0.7.2-r3)
(3/4) Installing g++ (12.2.1_git20220924-r4)
(4/4) Installing make (4.3-r1)
Executing busybox-1.35.0-r29.trigger
OK: 1213 MiB in 82 packages
[Container] 2023/06/22 17:25:24 Running command curl -o node.tar.xz https://nodejs.org/dist/v16.19.0/node-v16.19.0-linux-x64.tar.xz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 21.4M 100 21.4M 0 0 92.2M 0 --:--:-- --:--:-- --:--:-- 92.4M
[Container] 2023/06/22 17:25:24 Running command tar -xf node.tar.xz
[Container] 2023/06/22 17:25:26 Running command mv node-* /usr/local/node
[Container] 2023/06/22 17:25:27 Running command ln -s /usr/local/node/bin/node /usr/bin/node
[Container] 2023/06/22 17:25:27 Running command ln -s /usr/local/node/bin/npm /usr/bin/npm
[Container] 2023/06/22 17:25:27 Running command export PATH="/usr/local/node/bin:$PATH"
[Container] 2023/06/22 17:25:27 Running command node -v
/codebuild/output/tmp/script.sh: line 4: node: not found
[Container] 2023/06/22 17:25:27 Command did not exit successfully node -v exit status 127
Relevant buildspec deploy.yml
version: 0.2
phases:
install:
commands:
- apk update
- apk add --no-cache curl python3 make g++
- curl -o node.tar.xz https://nodejs.org/dist/v16.19.0/node-v16.19.0-linux-x64.tar.xz
- tar -xf node.tar.xz
- mv node-* /usr/local/node
- ln -s /usr/local/node/bin/node /usr/bin/node
- ln -s /usr/local/node/bin/npm /usr/bin/npm
# This line was added to set the PATH variable
- export PATH="/usr/local/node/bin:$PATH"
- node -v
- npm -v
UPDATE ATTEMPT #2:
From my build.yml file I try calling a bash script from the phases/install/commands section. It get's into the bash script and runs it (purpose is to install node using nvm) and it installs but when node --version
is ran it still errors out.
install_node.sh
#!/bin/bash
# Install required packages
apk update
apk add --no-cache curl bash
# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Check if a profile file exists
if [ -f ~/.bashrc ]; then
echo "Profile file ~/.bashrc already exists."
else
echo "Creating profile file ~/.bashrc."
echo "export NVM_DIR=\"$HOME/.nvm\"" >> ~/.bashrc
echo "[ -s \"$NVM_DIR/nvm.sh\" ] && \. \"$NVM_DIR/nvm.sh\"" >> ~/.bashrc
fi
# Activate NVM
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
# Install Node.js
nvm install 16.19.0
# Set the installed version as the default
nvm alias default 16.19.0
# Set the PATH environment variable
export PATH="/root/.nvm/versions/node/v16.19.0/bin:$PATH"
# Install Sudo
apk add sudo
echo "CHANGING PERMISSIONS TO EXECUTE NODE BINARY"
# Make the node binary executable
sudo chmod +x /root/.nvm/versions/node/v16.19.0/bin/node
# Check the permissions of the node binary
echo "CHECKING TO SEE IF THIS HAS PERMISSIONS TO EXECUTE NODE BINARY"
ls -l /root/.nvm/versions/node/v16.19.0/bin/node
node --version
Thanks!