我正在通过SSH协议远程运行git命令,因为服务器不接受HTTPS。我正在通过 bash 脚本运行它,并且希望我可以传入用户名和密码作为变量。
更具体地说,我正在迁移存储库,并且在运行时遇到问题
git remote add origin ssh://${username}:${password}@server.com/repo.git
我正在通过 Jenkins 作业运行脚本,并且无法轻松提示输入密码。将密钥复制到远程服务器的最佳选择是吗?
假设您使用的是最新版本的 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 凭据绑定插件。
-
另一种方法是使用凭据绑定插件。此插件允许将凭据绑定到环境变量,以便在其他构建步骤中使用。
-
您始终可以登录到远程框并手动添加Github凭据
干杯