I have an app that I deploy through Gitlab. To deploy it to the production server I use a script in deploy_production. Basically enter via ssh, remove node_modules do a pull, an install and build:
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"
But my problem uses node 11.5, and in the production server there is a node 8 by default. On that server we have installed nvm to trigger the correct node version, but the issue is that the gitlab-ci script is not able to access nvm. No matter what we do, it wont work.
Some options we have tried —without the breaklines—:
- 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"
Returns:
Warning: Permanently added 'myserver.com,x.x.x.x' (ECDSA) to the list of known hosts.
bash: /nvm.sh: No such file or directory
Or if I try to install 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"
Returns:
=> 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
If I check the .bashrc:
- ssh myuser@exmple.com "cat .bashrc"
I get:
[…] 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
And if I do:
- ssh myuser@exmple.com "echo $NVM_DIR"
I get nothing.
So it looks like, even when gitlab-ci is entering the server via ssh, can't see the environment variables. And if I try to save them, they wont be saved.
Does anyone know how to use nvm with ssh in a gitlab-ci script?