gpt4 book ai didi

node.js - Gitlab-ci 脚本和 ssh 中的 Nvm

转载 作者:行者123 更新时间:2023-12-03 20:53:57 35 4
gpt4 key购买 nike

我有一个通过 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"

但是我的问题使用 Node 11.5,并且在生产服务器中默认有一个 Node 8。在那个服务器上我们已经安装了 nvm 来触发正确的 Node 版本,但问题是 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?

最佳答案

在我的情况下,这些行有效:

variables: # variables for dind
DOCKER_REGISTRY_DOMAIN: "registry.digitalocean.com"
DOCKER_HOST: tcp://docker:2375
DOCKER_TLS_CERTDIR: ""
DOCKER_DRIVER: overlay2

image: docker:latest

services:
- docker:dind

task:
image: archlinux:latest # image on arch in this example
script:
- pacman -Sy wget --noconfirm # install wget
- wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.38.0/install.sh | bash #install nvm
- export NVM_DIR="$HOME/.nvm" && . "$NVM_DIR/nvm.sh" --no-use #load nvm
- eval "[ -f .nvmrc ] && nvm install || nvm install 14" #install node
- node --version
- npm --version
您可以在存储库中找到最新版本
https://github.com/nvm-sh/nvm
更多关于“ docker ”
https://docs.gitlab.com/ee/ci/docker/using_docker_build.html

关于node.js - Gitlab-ci 脚本和 ssh 中的 Nvm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61625191/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com