Hi community,
I would like run SSH script from CI runner on gitlab.com. I’ve add secret variable (project/CI-CD/Settings) with SSH_PRIVATE_KEY
key and id_rsa
generate from my production server value.
In my .gitlab-ci.yml
:
image: node:9.11.1-alpine
deploy:
stage: deploy
before_script:
##
## Install ssh-agent if not already installed, it is required by Docker.
## (change apt-get to yum if you use a CentOS-based image)
##
- "which ssh-agent || ( apk add --update openssh )"
##
## Add bash & git
##
- apk add --update bash git
##
## Run ssh-agent (inside the build environment)
##
- eval $(ssh-agent -s)
##
## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
## We're using tr to fix line endings which makes ed25519 keys work
## without extra base64 encoding.
## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
##
- echo "$SSH_PRIVATE_KEY"
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
##
## Create the SSH directory and give it the right permissions
##
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
##
##
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
##
## SSH test
##
- echo "$TARGET_SERVER_USER"
- echo "$TARGET_SERVER_HOST"
- ssh -o StrictHostKeyChecking=no -T "$TARGET_SERVER_USER@$TARGET_SERVER_HOST"
script:
- npm i -g pm2
All echo
on before_script
return good value. But the result of job is not good :
$ ssh -o StrictHostKeyChecking=no -T "$TARGET_SERVER_USER@$TARGET_SERVER_HOST"
Warning: Permanently added 'xxx.x.xxx.xxx' (ECDSA) to the list of known hosts.
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).
I don’t understand why the SSH connexion fail. I think I had to make a misconfiguration, but I can not find where.
Anyone can help me ?
Thank you community !