一、故障現象
這是我的.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