一、故障现象
这是我的.gitlab-ci.yml文件,如下所示:
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
输出错误如下:
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
私钥在本地测试正确。
二、问题原因
由于是第一次请求生产服务器,所以ssh会返回下列请求导致运行中断
The authenticity of host 'xxx' can't be established.
ESDSA key fingerprint is xxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no)?
三、解决方法
- 在.gitlab-ci.yml文件中添加如下代码即可运行:
before_script:
- ssh-keyscan [you_host_url] >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts