Gitlab CI - Setup SSH Key In Bash

I followed gitlab's documentation on SSH keys when using the Docker executor to setup connection to my remote server, which works as expected.

before_script:
  - which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
  - eval $(ssh-agent -s)
  - ssh-add <(echo "$SSH_PRIVATE_KEY")
  - mkdir -p ~/.ssh
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

However, I'd like to put those commands in a separate script like this:

before_script:
  - bash ./scripts/ssh-config.sh

ssh-config.sh

#!/bin/bash
which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
eval $(ssh-agent -s)
ssh-add <(echo $SSH_PRIVATE_KEY)
mkdir -p ~/.ssh
[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config

When trying to connect to the remote server, it gives the following error:

$ bash scripts/ssh-config.sh
/usr/bin/ssh-agent
Agent pid 15
Identity added: /dev/fd/63 (/dev/fd/63)
$ ssh [email protected] "touch test"
Warning: Permanently added 'example.com' (ECDSA) to the list of known hosts.    
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,password).

The script seemed to have been executed correctly and have output the same logs as that by the previous method. Any ideas?

0 Comment

NO COMMENTS

LEAVE A REPLY

Captcha image