系统版本: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。
错误1使用git branch: "$brans", credentialsId: 'platform-jenkins', url: "$GIT_URL/${app_name}.git"下载代码时提示以下异常,但是在服务器上执行git clone命令却能正常执行。
ERROR: Error cloning remote repo 'origin' hudson.plugins.git.GitException: Command "git fetch --tags --progress git@git/kd-gateway.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. 错误原因因为使用的是yum方式安装的Jenkins,所以Jenkins启动的时候启动用户是jenkins(可以通过ps -ef|grep jenkins来查看启动用户),而Jenkins服务器上ssh的相关配置和用户却是root,所以就导致没有权限去执行git命令。
解决办法配置git账号的ssh免密登录,具体怎么配置可以自行百度,这里说下配置的时候需要注意的点:
生成的对应的公钥、私钥的位置是在/var/lib/jenkins/.ssh目录下
确认id_rsa、id_rsa.pub这两个文件的所属用户、用户组都是jenkins
git网站中需要给对应账号配置ssh key
[root@infra2-test-k8s .ssh]# ls -l total 12 -rw-------. 1 jenkins jenkins 1679 Apr 7 20:50 id_rsa -rw-r--r--. 1 jenkins jenkins 393 Apr 7 20:50 id_rsa.pub -rw-r--r--. 1 jenkins jenkins 197 Apr 7 19:19 known_hosts修改文件所属用户、用户组命令:chown -R jenkins:jenkins id_rsa。
然后去掉拉取git代码的命令行中的credentialsId:git branch: "$brans", url: "$GIT_URL/${app_name}.git
还有另外一种就是修改jenkins用户的用户组,将其放入root用户组中,命令如下:
# 添加 gpasswd -a root jenkins # 移除 gpasswd -d root jenkinsjenkins启动用户是放在/etc/sysconfig/jenkins文件中的JENKINS_USER参数下,对应的用户组参数:JENKINS_GROUP
这个办法理论上是可以的,不过我没试,这里仅供参考。
错误2 ERROR: Error fetching remote repo 'origin' hudson.plugins.git.GitException: Failed to fetch from git@git/kd-gateway.git at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:909) at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1131) at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1167) at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:124) at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:93) at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:80) at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: hudson.plugins.git.GitException: Command "git config remote.origin.url git@git/kd-gateway.git" returned status code 255: stdout: stderr: error: could not lock config file .git/config: Permission denied at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2430) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2360) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2356) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommand(CliGitAPIImpl.java:1916) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommand(CliGitAPIImpl.java:1928) at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.setRemoteUrl(CliGitAPIImpl.java:1542) at hudson.plugins.git.GitAPI.setRemoteUrl(GitAPI.java:160) at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:897) ... 11 more错误原因就是没有配置git的ssh免密登录权限,在服务器上配置下即可。
错误3使用maven命令打包时提示:
+ mvn -Dmaven.test.failure.ignore clean package -P test which: no java in (/sbin:/usr/sbin:/bin:/usr/bin) The JAVA_HOME environment variable is not defined correctly This environment variable is needed to run this program NB: JAVA_HOME should point to a JDK not a JRE但是去服务器上看了下Java的配置没问题,maven的配置也没问题,PATH也没问题。
原因maven是从/sbin:/usr/sbin:/bin:/usr/bin这几个目录下找Java的,去服务器上的这几个目录下看了,确实没有Java,那么原因应该就是这个了。
解决办法做个Java的软连接即可。
命令: