Moe*_*een 5 linux shell docker gitlab-ci
我正在尝试在管道中的gitlab ci作业中使用名为install-nodejs.sh的外壳脚本文件安装nodejs。安装成功,并且node在install-nodejs.sh文件中显示版本,但是当我说gitlab-ci.yml文件中的node -v不能识别node命令时,我在做什么错?
我创建了一个Shell脚本来安装nodejs并设置导出路径,但是仍然无法识别该节点
install-nodejs.sh文件代码
#!/usr/bin/env bash
set -e
command -v node > /dev/null | NOT_INSTALLED=1
if [[ "$NOT_INSTALLED" != "1" ]]
then
mkdir /usr/local/nvm
NVM_DIR="/usr/local/nvm"
NODE_VERSION="10.12.0"
NVM_INSTALL_PATH="$NVM_DIR/versions/node/v$NODE_VERSION"
rm /bin/sh && ln -s /bin/bash /bin/sh
curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
source ~/.nvm/nvm.sh
nvm install $NODE_VERSION
nvm alias default $NODE_VERSION
nvm use default
export NODE_PATH="$NVM_INSTALL_PATH/lib/node_modules"
export PATH="$NVM_INSTALL_PATH/bin:$PATH"
fi
Run Code Online (Sandbox Code Playgroud)
和gitlab.yml文件代码,我正在调用此文件
test_install_nodejs:
<<: *default_job_template
stage: build
script:
- ./scripts/install-nodejs.sh
- node -v
- npm -v
- npm install -g newman
Run Code Online (Sandbox Code Playgroud)
gitlab.yml文件中的node -v无法识别节点,但是我可以从上面的shell文件中看到节点安装成功。
Node is not found in the gitlab.yml file because the variables that you have defined in your install script are not available there. You can see that by yourself by calling echo $PATH just after ./scripts/install-nodejs.sh. You will see that PATH does not contain $NVM_INSTALL_PATH/bin.
The reason is that export is exporting the variables to the child process, not the parent. See Bash: export not passing variables correctly to parent.
You can make them available in the gitlab.yml file by using source:
test_install_nodejs:
<<: *default_job_template
stage: build
script:
- source ./scripts/install-nodejs.sh
- node -v
- npm -v
- npm install -g newman
Run Code Online (Sandbox Code Playgroud)
Note that I am assuming that install-nodejs.sh is exactly the one you have shown and does not end with exit (otherwise the yml script will end just after source).