CICD 中的端口转发(Github 操作)

Port forwarding in CICD (Github Actions)

我想 运行 在 Github 操作中进行数据库迁移。数据库在堡垒后面。

我的解决方案是通过堡垒将 Postgres 端口 5432 转发到数据库主机。

我尝试了以下脚本,但似乎不起作用。

mkdir ~/.ssh
ssh-keyscan -H <bastion_ip>  >> ~/.ssh/known_hosts
echo "${{secrets.BASTION_SSH_KEY}}" >> key
chmod 600 ./key
ssh -T -i ./key -L 5432:<db_host_url>:5432 user@<bastion_ip> &
make migrate
rm ./key

make migrate 运行s 针对 localhost:5432.

的迁移

当我 运行 管道时出现以下错误

Error:  AssertionError [ERR_ASSERTION]: ifError got unwanted exception: connect ECONNREFUSED 127.0.0.1:5432

是否要修复它? 我对其他方式持开放态度。

我认为你的 ssh 命令不正确,试试这个:

ssh -fN -i ./key -L 5432:<db_host>:5432 user@<bastion_ip>

来自手册页:

-f    Requests ssh to go to background just before command execution.
      This is useful if ssh is going to ask for passwords or passphrases,
      but the user wants it in the background.  This implies -n.  The
      recommended way to start X11 programs at a remote site is with
      something like ssh -f host xterm.

并且:

-N    Do not execute a remote command.  This is useful for just
      forwarding ports.

谢谢@larsks,我让它工作了。 有几件事我必须改变才能让它工作。

  1. 已根据
  2. 的建议添加 -fN
  3. 使用ssh-agent处理密钥

下面是工作代码片段:

mkdir ~/.ssh
ssh-keyscan -H <bastion_ip> >> ~/.ssh/known_hosts
eval `ssh-agent -s`
ssh-add - <<< "${{secrets.BASTION_SSH_KEY}}"
ssh -fN -v -L 5432:<db-host>:5432 user@<bastion_ip>
make migrate