Jenkins上进行git相关配置

在Linux上搭建Jenkins后,配置git获取代码报错

主要报错有2个:

1、私钥配的有问题,导致没有权限从git上拉代码;

2、分支配的不对导致的错误

具体报错信息1:

Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/First_Test_Case
The recommended git tool is: NONE
using credential 2c98afaa-2c1a-46ca-80f1-2f4c53ba75c2
 > /usr/local/git/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > /usr/local/git/bin/git config remote.origin.url git@github.com:panda-123/Jenkins_test.git # timeout=10
Fetching upstream changes from git@github.com:panda-123/Jenkins_test.git
 > /usr/local/git/bin/git --version # timeout=10
 > git --version # 'git version 2.13.0-rc1'
using GIT_SSH to set credentials 
 > /usr/local/git/bin/git fetch --tags --progress -- git@github.com:panda-123/Jenkins_test.git +refs/heads/*:refs/remotes/origin/* # timeout=10
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from git@github.com:panda-123/Jenkins_test.git
	at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:998)
	at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1239)
	at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1299)
	at hudson.scm.SCM.checkout(SCM.java:505)
	at hudson.model.AbstractProject.checkout(AbstractProject.java:1206)
	at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:637)
	at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:86)
	at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:509)
	at hudson.model.Run.execute(Run.java:1907)
	at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
	at hudson.model.ResourceController.execute(ResourceController.java:97)
	at hudson.model.Executor.run(Executor.java:429)
Caused by: hudson.plugins.git.GitException: Command "/usr/local/git/bin/git fetch --tags --progress -- git@github.com:panda-123/Jenkins_test.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2450)
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2051)
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$500(CliGitAPIImpl.java:84)
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:573)
	at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:996)
	... 11 more
ERROR: Error fetching remote repo 'origin'
Finished: FAILURE

具体报错信息2:

Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/First_Test_Case
The recommended git tool is: NONE
using credential 2c98afaa-2c1a-46ca-80f1-2f4c53ba75c2
 > /usr/local/git/bin/git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > /usr/local/git/bin/git config remote.origin.url git@github.com:panda-123/Jenkins_test.git # timeout=10
Fetching upstream changes from git@github.com:panda-123/Jenkins_test.git
 > /usr/local/git/bin/git --version # timeout=10
 > git --version # 'git version 2.13.0-rc1'
using GIT_SSH to set credentials 
 > /usr/local/git/bin/git fetch --tags --progress -- git@github.com:panda-123/Jenkins_test.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > /usr/local/git/bin/git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > /usr/local/git/bin/git rev-parse origin/master^{commit} # timeout=10
ERROR: Couldn't find any revision to build. Verify the repository and branch configuration for this job.
Finished: FAILURE

genkins中git配置正确方法

有三个地方需要关注:

  • Repository URL

    URL有2种方式https和SSH方式:

    ssh格式: git@github.com:panda-123/Jenkins_test.git

    https格式:https://github.com/panda-123/Jenkins_test.git

  • Credentials

    本次使用的是SSH方式,设置证书时,选择SSH Username with private key

    Private Key中填写私钥,我的Jenkins搭建在Linux上的,私钥获取方式如下

    cd ~/.ssh
    cat id_rsa
    



  • 指定分支

    查看自己git上的分支,然后修改为与之相同的就行

posted on 2021-02-27 17:41  多多洛~吖  阅读(10414)  评论(0编辑  收藏  举报