使用用户名和密码的SSH git命令

时间:2016-05-30 01:19:41

标签: git bash jenkins ssh

我正在通过SSH协议远程运行git命令,因为服务器不接受HTTPS。我正在通过bash脚本运行它,如果我可以将用户名和密码作为变量传递,我更愿意。

更具体地说,我正在进行存储库的迁移,并且在运行时遇到问题

git remote add origin ssh://${username}:${password}@server.com/repo.git

我正在通过Jenkins工作运行脚本,并且无法轻松提示输入密码。我是将密钥复制到远程服务器的最佳选择吗?

2 个答案:

答案 0 :(得分:3)

假设您使用的是最新版本的Git,您可以使用GIT_SSH_COMMAND环境变量:

  GIT_SSH, GIT_SSH_COMMAND

      If either of these environment variables is set then git fetch
      and git push will use the specified command instead of ssh when
      they need to connect to a remote system. The command will be
      given exactly two or four arguments: the username@host (or just
      host) from the URL and the shell command to execute on that
      remote system, optionally preceded by -p (literally) and the port
      from the URL when it specifies something other than the default
      SSH port.

      $GIT_SSH_COMMAND takes precedence over $GIT_SSH, and is
      interpreted by the shell, which allows additional arguments to be
      included.  $GIT_SSH on the other hand must be just the path to a
      program (which can be a wrapper shell script, if additional
      arguments are needed).

      Usually it is easier to configure any desired options through
      your personal .ssh/config file. Please consult your ssh
      documentation for further details.

因此,在致电git之前,您可以简单地说:

export GIT_SSH_COMMAND='ssh -i /path/to/key'

如果您无法使用SSH密钥,则可以使用Jenkins Credential Binding plugin

答案 1 :(得分:1)

  1. 另一种方法是使用凭证绑定插件。此插件允许将凭据绑定到环境变量,以便从其他构建步骤中使用。

  2. 您可以随时登录远程控制台并手动添加Github凭据

  3. 干杯