【发布时间】:2020-08-20 19:48:02
【问题描述】:
我有一个通过 Gitlab 部署的应用程序。要将其部署到生产服务器,我使用deploy_production 中的脚本。基本上通过 ssh 进入,删除 node_modules 进行拉取,安装和构建:
image: node:latest
before_script:
- apt-get update -qq
- apt-get install -qq git
- 'which ssh-agent || ( apt-get install -qq openssh-client )'
- eval $(ssh-agent -s)
- ssh-add <(echo "$K8S_SECRET_SSH_PRIVATE_KEY" | base64 -d)
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
stages:
- install
- build
- deploy_production
cache:
paths:
- node_modules/
install:
stage: install
script:
- npm install
artifacts:
paths:
- node_modules/
build:
stage: build
script:
- npm build
deploy_production:
stage: deploy_production
only:
- master
script:
- ssh example@example.com "export NPM_TOKEN=${NPM_TOKEN} && cd www/myproject && rm -rf node_modules dist/* && git pull && npm ci && npm run prod"
但是我的问题使用的是节点11.5,而在生产服务器中默认有一个节点8。在该服务器上,我们安装了 nvm 以触发正确的节点版本,但问题是 gitlab-ci 脚本无法访问 nvm。无论我们做什么,它都行不通。
我们尝试过的一些选项——没有分隔线——:
- ssh myuser@example.com "cd www/myproject
&& export NPM_TOKEN=${NPM_TOKEN}
&& export NVM_DIR='$HOME/.nvm' && . '$NVM_DIR/nvm.sh' --no-use
&& eval '[ -f .nvmrc ] && nvm install || nvm install stable'
&& nvm use --delete-prefix
&& rm -rf node_modules dist/*
&& git pull && node -v
&& npm ci && npm run prod"
返回:
Warning: Permanently added 'myserver.com,x.x.x.x' (ECDSA) to the list of known hosts.
bash: /nvm.sh: No such file or directory
或者如果我尝试安装 nvm:
- ssh myuser@example.com "cd www/myproject
&& export NPM_TOKEN=${NPM_TOKEN}
&& wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
&& export NVM_DIR='$HOME/.nvm' && . '$NVM_DIR/nvm.sh' --no-use
&& eval '[ -f .nvmrc ] && nvm install || nvm install stable'
&& nvm use --delete-prefix
&& rm -rf node_modules dist/*
&& git pull
&& node -v
&& npm ci
&& npm run prod"
返回:
=> nvm is already installed in /home/myuser/.nvm, trying to update using git
=> => Compressing and cleaning up git repository
=> nvm source string already in /home/myuser/.bashrc
=> bash_completion source string already in /home/myuser/.bashrc
nvm is not compatible with the npm config "prefix" option: currently set to "/usr/home/myuser/.nvm/versions/node/v11.5.0"
Run `npm config delete prefix` or `nvm use --delete-prefix v11.5.0 --silent` to unset it.
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
bash: /nvm.sh: No such file or directory
如果我检查 .bashrc:
- ssh myuser@exmple.com "cat .bashrc"
我明白了:
[…] lots of stuff
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
如果我这样做:
- ssh myuser@exmple.com "echo $NVM_DIR"
我什么也得不到。
所以看起来,即使gitlab-ci通过ssh进入服务器,也看不到环境变量。如果我试图拯救它们,它们将不会被拯救。
有人知道如何在 gitlab-ci 脚本中使用 nvm 和 ssh 吗?
【问题讨论】:
-
tl;dr ,使用单引号
'而不是双引号"... -
这是正确的答案……
标签: node.js bash ssh gitlab nvm