无法从 Github Action 工作流程通过 SSH 连接

问题描述

通过 SSH 连接到 Github Actions runner 创建的 Droplet。

我的步骤:

  1. ssh-keygen -t rsa -f ~/.ssh/KEY_NAME -P ""
  2. doctl compute ssh-key create KEY --public-key "CONTENT OF KEY_NAME.pub"
  3. doctl compute droplet create --image ubuntu-20-04-x64 --size s-1vcpu-1gb --region fra1 DROPLET_NAME --ssh-keys FINGERPRINT --wait
  4. ssh -vvv -i ~/.ssh/KEY_NAME root@DROPLET_IP

✔️ 使用从 doctl.exe 运行的 cmd 在 Windows 本地机器上进行测试 - 有效!
✔️ 使用 doctl 脚本在基于 Linux 映像的 Docker(安装在 Windows 上)上进行测试 - 有效!
⚠️ 使用 digitalocean/action-doctl 脚本在基于 ubuntu-latest 的 Github Actions runner 上测试 - 不起作用!

收到的消息是:connect to host ADDRESS_IP port 22: Connection refused

所以步骤是正确的,为什么这对 Github Actions 不起作用?

解决方法

如果您使用的是 GitHub 操作 digitalocean/action-doctl,请先检查 issue 14

为了通过 SSH 连接到 Droplet,doctl 需要访问 SSH 密钥对的私有部分,其公共部分在 Droplet 上。
目前 doctl Action 基于 Docker 容器。

如果您直接使用 Docker 容器,则可以使用以下命令调用它:

docker run --rm --interactive --tty \
 --env=DIGITALOCEAN_ACCESS_TOKEN=<YOUR-DO-API-TOKEN> \
 -v $HOME/.ssh/id_rsa:/root/.ssh/id_rsa \
 digitalocean/doctl compute ssh <DROPLET-ID>

为了从容器外部挂载 SSH 密钥。

您最好只使用 doctl 来 grep Droplet 的 IP 地址,并使用此操作更专注于 SSH 相关用例并提供许多附加功能:marketplace/actions/ssh-remote-commands。>