簡體   English   中英

Gitlab-ci 腳本和 ssh 中的 Nvm

[英]Nvm in Gitlab-ci script and ssh

我有一個通過 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 一起使用?

就我而言,這些行有效:

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

更多關於“碼頭工人”

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

這是對上述作者的一個小改進

該片段符號鏈接的信用: https://stackoverflow.com/a/40078875/1621381

我只是把這兩件事結合起來,現在很完美。

# Global defaults
variables:
  NODE_VERSION: "16.14.2"

# reusable nvm snippet
.setup-nvm-template: &setup-nvm
- wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash 
- export NVM_DIR="$HOME/.nvm" && . "$NVM_DIR/nvm.sh" --no-use
- eval "[ -f .nvmrc ] && nvm install || nvm install ${NODE_VERSION}"
- node --version
- npm --version
- sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/node" "/usr/local/bin/node"
- sudo ln -s "$NVM_DIR/versions/node/$(nvm version)/bin/npm" "/usr/local/bin/npm"

然后在預作業或其他作業中,您可以通過別名使用- *setup-nvm

.install-deps-template: &install-deps
  before_script:
  - export TZ=EDT
  - ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
  - export DEBIAN_FRONTEND=noninteractive
  - apt update -qq && apt install -qq -y --no-install-recommends apt-transport-https curl file gcc git
  - export LANG=en_US.UTF-8 ; export LANGUAGE=en_US:en ; export LC_ALL=en_US.UTF-8
  - *setup-nvm

使其易於繼續使用,只需固定精確的版本,而無需付出很多努力

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM