簡體   English   中英

帶有用戶名和密碼的SSH git命令

[英]SSH git commands with username and password

我正在通過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憑據綁定插件

  1. 另一種方法是使用憑證綁定插件。 此插件允許將憑據綁定到環境變量以從其他構建步驟中使用。

  2. 您總是可以登錄到遠程框中並手動添加Github憑據

干杯

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM