【问题标题】:Nvm in Gitlab-ci script and sshGitlab-ci 脚本和 ssh 中的 Nvm
【发布时间】: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


【解决方案1】:

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

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 in docker”

https://docs.gitlab.com/ee/ci/docker/using_docker_build.html

【讨论】:

    猜你喜欢
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 2021-10-14
    • 2017-04-01
    • 2022-12-10
    • 2019-04-10
    • 1970-01-01
    相关资源
    最近更新 更多