当前位置:   article > 正文

k8s + docker + Jenkins使用Pipeline部署SpringBoot项目时Jenkins错误集锦_hudson.plugins.git.gitexception: failed to fetch f

hudson.plugins.git.gitexception: failed to fetch from

背景

  • 系统版本:CentOS7
  • Jenkins版本:2.222.1
  • maven版本:apache-maven-3.6.3
  • Java版本:jdk1.8.0_231
  • Git版本:1.8.3.1
  • docker版本:1.13.1
  • k8s版本:1.9.8

使用yum install jenkins方式安装jenkins。

俺的Jenkins只部署了一台机器。

错误1

使用git branch: "$brans", credentialsId: 'platform-jenkins', url: "$GIT_URL/${app_name}.git"下载代码时提示以下异常,但是在服务器上执行git clone命令却能正常执行。

  1. ERROR: Error cloning remote repo 'origin'
  2. hudson.plugins.git.GitException: Command "git fetch --tags --progress git@git/kd-gateway.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
  3. stdout:
  4. stderr: Permission denied (publickey).
  5. fatal: Could not read from remote repository.
  6. Please make sure you have the correct access rights
  7. and the repository exists.

错误原因

因为使用的是yum方式安装的Jenkins,所以Jenkins启动的时候启动用户是jenkins(可以通过ps -ef|grep jenkins来查看启动用户),而Jenkins服务器上ssh的相关配置和用户却是root,所以就导致没有权限去执行git命令。

解决办法

配置git账号的ssh免密登录,具体怎么配置可以自行百度,这里说下配置的时候需要注意的点:

  • 生成的对应的公钥、私钥的位置是在/var/lib/jenkins/.ssh目录下
  • 确认id_rsaid_rsa.pub这两个文件的所属用户、用户组都是jenkins
  • git网站中需要给对应账号配置ssh key
  1. [root@infra2-test-k8s .ssh]# ls -l
  2. total 12
  3. -rw-------. 1 jenkins jenkins 1679 Apr 7 20:50 id_rsa
  4. -rw-r--r--. 1 jenkins jenkins 393 Apr 7 20:50 id_rsa.pub
  5. -rw-r--r--. 1 jenkins jenkins 197 Apr 7 19:19 known_hosts

修改文件所属用户、用户组命令:chown -R jenkins:jenkins id_rsa

然后去掉拉取git代码的命令行中的credentialsIdgit branch: "$brans", url: "$GIT_URL/${app_name}.git

还有另外一种就是修改jenkins用户的用户组,将其放入root用户组中,命令如下:

  1. # 添加
  2. gpasswd -a root jenkins
  3. # 移除
  4. gpasswd -d root jenkins

jenkins启动用户是放在/etc/sysconfig/jenkins文件中的JENKINS_USER参数下,对应的用户组参数:JENKINS_GROUP

这个办法理论上是可以的,不过我没试,这里仅供参考。

错误2

  1. ERROR: Error fetching remote repo 'origin'
  2. hudson.plugins.git.GitException: Failed to fetch from git@git/kd-gateway.git
  3. at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:909)
  4. at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1131)
  5. at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1167)
  6. at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:124)
  7. at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:93)
  8. at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:80)
  9. at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
  10. at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  11. at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  12. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  13. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  14. at java.lang.Thread.run(Thread.java:748)
  15. Caused by: hudson.plugins.git.GitException: Command "git config remote.origin.url git@git/kd-gateway.git" returned status code 255:
  16. stdout:
  17. stderr: error: could not lock config file .git/config: Permission denied
  18. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2430)
  19. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2360)
  20. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2356)
  21. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommand(CliGitAPIImpl.java:1916)
  22. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommand(CliGitAPIImpl.java:1928)
  23. at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.setRemoteUrl(CliGitAPIImpl.java:1542)
  24. at hudson.plugins.git.GitAPI.setRemoteUrl(GitAPI.java:160)
  25. at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:897)
  26. ... 11 more

错误原因就是没有配置git的ssh免密登录权限,在服务器上配置下即可。

错误3

使用maven命令打包时提示:

  1. + mvn -Dmaven.test.failure.ignore clean package -P test
  2. which: no java in (/sbin:/usr/sbin:/bin:/usr/bin)
  3. The JAVA_HOME environment variable is not defined correctly
  4. This environment variable is needed to run this program
  5. NB: JAVA_HOME should point to a JDK not a JRE

但是去服务器上看了下Java的配置没问题,maven的配置也没问题,PATH也没问题。

原因

maven是从/sbin:/usr/sbin:/bin:/usr/bin这几个目录下找Java的,去服务器上的这几个目录下看了,确实没有Java,那么原因应该就是这个了。

解决办法

做个Java的软连接即可。
命令:

ln -s JAVA_HOME/bin/java /usr/bin/java

错误4

  1. + mvn -Dmaven.test.failure.ignore clean package -P test
  2. [INFO] Scanning for projects...
  3. Downloading from nexus: http://XXXX:8081/repository/maven-public/org/springframework/boot/spring-boot-starter-parent/2.2.5.RELEASE/spring-boot-starter-parent-2.2.5.RELEASE.pom
  4. [WARNING] Failed to create parent directories for tracking file /opt/apache-maven-3.6.3/LocalRepo/org/springframework/boot/spring-boot-starter-parent/2.2.5.RELEASE/spring-boot-starter-parent-2.2.5.RELEASE.pom.lastUpdated
  5. [ERROR] [ERROR] Some problems were encountered while processing the POMs:
  6. [FATAL] Non-resolvable parent POM for cn.kuaidao:gateway:0.0.1-SNAPSHOT: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.2.5.RELEASE from/to nexus (http://XXXX:8081/repository/maven-public/): /opt/apache-maven-3.6.3/LocalRepo/org/springframework/boot/spring-boot-starter-parent/2.2.5.RELEASE/spring-boot-starter-parent-2.2.5.RELEASE.pom.part.lock (No such file or directory) and 'parent.relativePath' points at no local POM @ line 5, column 13
  7. @
  8. [ERROR] The build could not read 1 project -> [Help 1]
  9. [ERROR]
  10. [ERROR] The project cn.kuaidao:gateway:0.0.1-SNAPSHOT (/var/lib/jenkins/workspace/test-kd-gateway/pom.xml) has 1 error
  11. [ERROR] Non-resolvable parent POM for cn.kuaidao:gateway:0.0.1-SNAPSHOT: Could not transfer artifact org.springframework.boot:spring-boot-starter-parent:pom:2.2.5.RELEASE from/to nexus (http://XXXX:8081/repository/maven-public/): /opt/apache-maven-3.6.3/LocalRepo/org/springframework/boot/spring-boot-starter-parent/2.2.5.RELEASE/spring-boot-starter-parent-2.2.5.RELEASE.pom.part.lock (No such file or directory) and 'parent.relativePath' points at no local POM @ line 5, column 13 -> [Help 2]
  12. [ERROR]
  13. [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
  14. [ERROR] Re-run Maven using the -X switch to enable full debug logging.
  15. [ERROR]
  16. [ERROR] For more information about the errors and possible solutions, please read the following articles:
  17. [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
  18. [ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException

错误原因

原因和错误1一样,都是用户权限的问题,jenkins用户没有权限去LocalRepo这个目录下创建文件、文件夹,所以就导致找不到文件。ls -l命令查看/opt/apache-maven-3.6.3/LocalRepo这个文件夹所属用户和用户组都是root,修改为jenkins即可。

解决办法

修改文件夹所属用户、用户组。
命令:chown -R jenkins:jenkins LocalRepo

错误5

使用docker构建应用提示以下错误:

  1. + docker build -t 192.168.30.176:5000/kd-gateway:1.0.29-test -f docker/Dockerfile .
  2. Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.26/build?buildargs=%7B%7D&buildbinds=null&cachefrom=%5B%5D&cgroupparent=&cpuperiod=0&cpuquota=0&cpusetcpus=&cpusetmems=&cpushares=0&dockerfile=docker%2FDockerfile&labels=%7B%7D&memory=0&memswap=0&networkmode=default&rm=1&shmsize=0&t=192.168.30.176%3A5000%2Fkd-gateway%3A1.0.29-test&ulimits=null: dial unix /var/run/docker.sock: connect: permission denied

错误原因

/var/run/docker.sock权限问题,这个文件的所属用户、用户组都是root,需要将其修改为jenkins用户。

解决办法

修改文件所属用户、用户组。
命令:chown -R jenkins:jenkins docker.sock

总结

大部分都是权限问题,但是不知道什么原因,我的root用户切换不了jenkins用户,导致排查问题只能靠我机智的大脑去猜,这个就很坑。

原文地址:https://www.lifengdi.com/archives/article/1805

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/562487
推荐阅读
相关标签
  

闽ICP备14008679号