Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

gitlab - SSH bash session and environments variables scope

I have a Gitlab pipeline to get into a server via ssh, load correct node version with nvm, and install node packages using a NPM_TOKEN.

The NPM_TOKEN is saved on GitLab project configuration panel, at Test_project > Settings > CI/CD > Environment variables

NPM_TOKEN

Then I use it as follow:

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 *
StrictHostKeyChecking no

" > ~/.ssh/config'

stages:
  - deploy

deploy:
  stage: deploy
  only:
    - master
  script:
    - ssh user@example "export NPM_TOKEN=${NPM_TOKEN} && cd ~/www/linking_frontend_master && export NVM_DIR="$HOME/.nvm" && . "$NVM_DIR/nvm.sh" --no-use && eval "[ -f .nvmrc ] && nvm install || nvm install stable" && nvm use --delete-prefix && git checkout master && npm ci --verbose && npm run prod"

Here the pipeline is loading the ssh token and using it to connect to the server. Within the ssh commands I make use of the ${NPM_TOKEN} variable thanks to the double quotes, that expose the context within ssh to the surrounding environment —as oppose to single quotes.

The issue here is that $HOME returns /root, and $NVM_DIR/nvm.sh is set as /root/nvm.sh, which doesn't exist; it should be /Users/myuser/.nvmsh or equivalent path given the $HOME variable of the server session.

Does anyone know how to achieve that?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...