Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...