【问题标题】:Port forwarding in CICD (Github Actions)CICD 中的端口转发(Github Actions)
【发布时间】:2021-10-01 04:17:18
【问题描述】:

我想在 Github Actions 中运行数据库迁移。数据库是堡垒。

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

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

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 针对localhost:5432 运行迁移。

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

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

无论如何要修复它? 我愿意接受其他方式。

【问题讨论】:

  • “似乎不起作用”是什么意思?它是否因错误而失败?是否成功连接到堡垒主机?
  • @larsks 我已经用我得到的错误更新了帖子。看了一下,好像端口转发没用。

标签: ssh github-actions portforwarding cicd


【解决方案1】:

我认为你的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.

【讨论】:

  • 谢谢,它很有帮助。此外,您建议使用详细模式帮助我意识到我传递 ssh 密钥的方式不起作用。
【解决方案2】:

谢谢@larsks,我搞定了。 为了让它正常工作,我必须进行一些更改。

  1. 按照@larsks 的建议添加了-fN
  2. 使用ssh-agent 处理密钥

下面是工作代码sn-p:

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

【讨论】:

    猜你喜欢
    • 2022-10-20
    • 2021-10-08
    • 1970-01-01
    • 2021-10-05
    • 2020-01-14
    • 1970-01-01
    • 2022-11-02
    • 2022-10-05
    • 1970-01-01
    相关资源
    最近更新 更多