我们有一台RHEL 7远程服务器,我在其中创建了一个名为gitlabci
的虚拟用户。
- 通过SSH进入远程服务器时,我生成了一个公私钥对(用于从GitLab抓取文件时使用)
- 将公用密钥作为deploy key上载,以供我们在设置CI后使用
- 在本地计算机中生成了另一个公私钥对(用于从GitLab Runner SSH进入远程服务器时使用)
- 将公钥添加到远程服务器的
authorized_keys
- 将私钥添加到项目的CI environment variables
- 将公钥添加到远程服务器的
这个想法是当CI运行时,GitLab运行程序将以我创建的gitlabci
用户的身份通过SSH进入远程服务器,然后使用部署密钥将分支提取到Web目录中。
我以为我已经正确设置了密钥,但是每当跑步者尝试进行SSH时,连接都会被拒绝。
$ which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )
...
$ eval $(ssh-agent -s)
Agent pid 457
$ echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
Identity added: (stdin) (GitLab CI)
$ mkdir -p ~/.ssh
$ chmod 700 ~/.ssh
$ [[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
$ ssh gitlabci@random.server.com
Pseudo-terminal will not be allocated because stdin is not a terminal.
ssh: connect to host random.server.com port 22: Connection refused
ERROR: Job failed: exit code 1
当我尝试使用本地密钥对通过本地计算机上的GitBash SSH到远程服务器时,它确实起作用了。
$ ssh -i ~/.ssh/gitlabci gitlabci@random.server.com
Last login: Mon Nov 4 13:49:59 2019 from machine01.work.server.com