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上的分支,然后修改為與之相同的就行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM