如何使用别名访问Jenkins中的远程Git存储库?

时间:2016-04-06 11:35:32

标签: git github jenkins ssh domain-aliasing

简短版

我正在使用别名来连接我的远程Git存储库。它可以工作,但是当我将这样的存储库添加到Jenkins时,它无法访问存储库并显示错误:

  

无法连接到存储库:命令“git -c core.askpass = true   ls-remote -h git@github.com-foo:myaccount / myrepository.git HEAD“   返回状态码128:stdout:stderr:ssh:无法解析   hostname github.com-foo:名称或服务未知致命:不能   从远程存储库中读取。

     

请确保您拥有正确的访问权限和存储库   存在。

详细的问题说明

为了将commit / push代码作为不同的用户,我使用了别名:

C:\Users\myusername\.ssh\config

...

#github.com-foo account
Host github.com-foo
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_foo

#github.com-bar account
Host github.com-bar
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_bar

...

然后代替

git clone git@github.com:myaccount/myrepository.git

我只是使用

git clone git@github.com-foo:myaccount/myrepository.git

.ssh目录可以作为共享文件夹从我的本地VM访问,因此也可以在VM上使用我的凭据。

一切正常。

现在我在一台虚拟机上安装了Jenkins,并希望配置与存储在GitHub上的远程存储库的连接。我做了什么:

  1. 凭据:Jenkins -> Credentials -> [link] Global credentials (unrestricted) -> [radio button] Enter directly -> [textarea] Key -> added -> [button] Save

  2. Git插件:Jenkins -> Configuration -> [section] Git plugin - 设置电子邮件和名称:

  3. enter image description here

    1. Project Git设置:[project] -> Configuration -> [section] Source Code Management -> [section] Git - 存储库URL和凭据:
    2. enter image description here

      如图所示,此连接失败。

      为什么它不起作用以及如何使其工作,以便使用别名连接Jenkins中的远程Git存储库?

1 个答案:

答案 0 :(得分:2)

您需要jenkins ssh C:\Users\myusername\.ssh\config 配置。文件

jenkins

适用于您的用户,但您也需要jenkins用户的别名,其中jenkins正在运行。查找.ssh\config用户的主目录,并在其中添加文件ssh

然后,您可能还需要在其主目录中为jenkins复制/创建不同的jenkins密钥,因为您无法从chown -R jenkins /var/lib/jenkins/.ssh用户访问。

在此之后,应设置文件评估权限。它可以通过// Collect and validate user inputs if($_SERVER["REQUEST_METHOD"] == "POST") { session_start(); $forename = trim(filter_input(INPUT_POST,"user_forename",FILTER_SANITIZE_STRING)); $surname = trim(filter_input(INPUT_POST,"user_surname",FILTER_SANITIZE_STRING)); $gender = trim(filter_input(INPUT_POST,"user_gender",FILTER_SANITIZE_STRING)); $email = trim(filter_input(INPUT_POST,"user_email",FILTER_SANITIZE_EMAIL)); $password = trim(filter_input(INPUT_POST,"user_password")); $city = trim(filter_input(INPUT_POST,"user_city")); $team = trim(filter_input(INPUT_POST,"user_team",FILTER_SANITIZE_STRING)); $bio = trim(filter_input(INPUT_POST,"user_bio",FILTER_SANITIZE_SPECIAL_CHARS)); $human = trim(filter_input(INPUT_POST,"user_human",FILTER_SANITIZE_STRING)); $userExist = mysql_query("SELECT * FROM User WHERE U_Email='$email'"); if($forename == "" || $surname == "" || $email == "" || $password == "" || $city == "" || $team == "" || $bio == "" || $human == "") { $error_message = "Please fill in all form fields"; } if (!isset($error_message) && !filter_var($email, FILTER_VALIDATE_EMAIL)) { $error_message = "$email is a not a valid email address"; } if (!isset($error_message) && (mysql_num_rows($userExist) > 0)) { $error_message = "$email is already taken!"; } if(!isset($error_message)) { $sql = $db->query("INSERT INTO User (U_Forename, U_Surname, U_Gender, U_Email, U_Password, U_City, U_Team, U_Biography) VALUES('{$forename}', '{$surname}', '{$gender}', '{$email}', '{$password}', '{$city}', '{$team}', '{$bio}')"); // header('Location: index.php'); } } <div class="wrapper"> <h1>Register, it's free!</h1> <div> <?php if (isset($error_message)) { echo "<h2>".$error_message."</h2>"; } ?> </div> 轻松完成。

在全局Jenkins设置中,私钥可以设置为“From the Jenkins master~ / .ssh”:

enter image description here