如何在 ruby 脚本中将密码传递给 git

How can I pass a password to git in a ruby script

我正在编写一个 ruby 小脚本,它将在两个不同的服务器上检出和同步一个分支。我想弄清楚如何在拉动时将密码传递给 git。现在我有

Net::SSH.start(host, user, password: password) do |ssh|
  # other code....
  result = ssh.exec!("git pull")
  # results in Enter passphrase for key '/root/.ssh/id_rsa'
end

在 运行 git 命令后,它会提示输入密钥密码。

是否可以通过 git 命令传递它?或者在 ruby?

中还有其他方法吗?

net-ssh documetation 确实提到方法 Net::SSH.start() 接受密码短语参数:

passphrase
the passphrase to use when loading a private key (default is nil, for no passphrase)

因此,如果您可以在程序中获取密码(从文件或环境变量),则可以将该参数添加到 Net::SSH.start() 方法。

但是,一旦连接,最好:

  • 为 Git SSH 使用 passphrase-less 密钥 URL
  • 或使用 HTTPS URL 作为 git pull 命令的代替,因为您可以在凭证中一次性注册与该 HTTPS URL 相关联的所有密码(不是密码)存储。

使用密码意味着添加到您的 SSH 会话:

  • 运行 ssh-agent
  • 进入passphrase through a script

这看起来很麻烦。