1. Symptoms
Here is my .gitlab-ci.yml file:
before_script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
master-deploy:
stage: deploy
only:
- master
script:
- scp -r index.html user@url:/index.html
The error output is as follows:
Host key verification failed.
lost connection
Cleaning up project directory and file based variables
00:00
+ set -o
+ grep pipefail
+ set -o pipefail
+ set -o errexit
+ set +o noclobber
+ :
···
+ exit 0
ERROR: Job failed: exit status 1
The private key works correctly when tested locally.
2. Cause
Because this is the first connection to the production server, ssh returns the following prompt, which interrupts the job
The authenticity of host 'xxx' can't be established.
ESDSA key fingerprint is xxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no)?
3. Solution
- Add the following code to the .gitlab-ci.yml file and the job will run:
before_script:
- ssh-keyscan [you_host_url] >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts