本文主要记录自己在CentOS安装Jenkins时遇到的问题。希望对大家有所帮助:
安装步骤比较简单,Centos安装tomcat,部署Jenkins的包,然后根据需要安装插件(如git、maven等)即可。这里不再赘述。
然后在Jenkins的“系统管理”->“Global Tool Configuration”中配置JDK、Git、Maven。如下图所示:
1、遇到的第一个问题:Git安装后,配置项目的git路径后,Jenkins马上在工程的git路径下面提示如下错误:
Failed to connect to repository : Command "git config --local credential.helper store --file=/tmp/git2956041026506359040.credentials" returned status code 129:
stdout:
stderr: error: unknown option `local'
usage: git config [options]
Config file location
--global use global config file
--system use system config file
-f, --file <FILE> use given config file
Action
--get get value: name [value-regex]
--get-all get all values: key [value-regex]
--get-regexp get values for regexp: name-regex [value-regex]
--replace-all replace all matching variables: name value [value_regex]
--add adds a new variable: name value
--unset removes a variable: name [value-regex]
--unset-all removes all matches: name [value-regex]
--rename-section rename section: old-name new-name
--remove-section remove a section: name
-l, --list list all
-e, --edit opens an editor
--get-color <slot> find the color configured: [default]
--get-colorbool <slot>
find the color setting: [stdout-is-tty]
Type
--bool value is "true" or "false"
--int value is decimal number
--bool-or-int value is --bool or --int
--path value is a path (file or directory name)
Other
-z, --null terminate values with NUL byte
原因:git版本过低。原来我是用yum install git安装的git,执行git --version,发现版本是1.7.1。而Jenkins需要1.7.4以上的git版本,才支持--local选项。
解决方法:网上搜了一些文章,可以自行下载git 2.1.2的源码,下载gcc然后自己编译。文章在这里,大家可以参考我转载的这篇文章《Centos 6.5升级git版本的办法》
完成后,不再报此错误。
2、遇到的第二个问题:git提示新的问题:
Failed to connect to repository : Command "/usr/local/git/bin/git -c core.askpass=true ls-remote -h http://www.xxx.com/gitlab/root/test.git HEAD" returned status code 128: stdout: stderr: fatal: Unable to find remote helper for 'http'或者
Failed to connect to repository : Command "git ls-remote -h git@xxxxx.com:xxx/dev_test.git HEAD" returned status code 128: stdout: stderr: Permission denied, please tryagain. Permission denied, please try again. Permission denied(publickey,gssapi-keyex,gssapi-with-mic,password). fatal: The remote end hung up unexpectedly原因:没有配置git的ssh key。
解决方法:执行下面的命令,生成key
ssh-keygen -t rsa -C "admin@example.com"然后将~/.ssh/目录下的id_rsa.pub中的公钥,放到git的ssh key中。再在Jenkins中创建新的Credentials。类型是SSH Username with private key。Username使用ssh-keygen中用到的邮箱,Private Key中选择“From the Jenkins master ~/.ssh”即可。
修改后,问题解决。
3、安装Maven。
Maven不支持通过Centos的yum进行安装,需要手工安装。大家可以参考我转载的这篇文章:《Centos6.5安装Maven》
4、自动部署war包到tomcat
工程配置如下图:
配置很简单,这里需要说明一下配置过程中遇到的问题:
1)无权限连接tomcat的问题。提示信息如下:
Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://10.1.41.8:8081//manager/text/list
查了tomcat ,这里是权限配置错误导致的。需要为用户配置manager-script权限,而不是manager-gui权限。
修改后问题解决。
2)打算将war部署到/ROOT目录下,以便通过http://10.1.41.8:8081可以直接访问。但是系统始终报下面的错误:
Caused by: org.codehaus.cargo.container.tomcat.internal.TomcatManagerException: FAIL - Context / is defined in server.xml and may not be undeployed
注:如果不部署到根目录下,没有任何问题。部署到根目录下,context path需要填“/”。
后来查来查去,发现是由于server.xml的后面有一行下面的代码,导致无法自动undeploy。(本来这段代码是用来确保可以人工发布到/ROOT目录下的)
<Context path="/" reloadable="true" docBase="/opt/application/settlement/settlement.war" workDir="/opt/application/settlement/tmp"/>
将这段代码屏蔽后,问题解决。可以自动部署了。
【2016-9-1】调整说明
最近通过上面的配置deploy编译好的war包到tomcat时一直出错,导致构建失败。提示仍然是
Caused by: org.codehaus.cargo.container.tomcat.internal.TomcatManagerException: FAIL - Context / is defined in server.xml and may not be undeployed
1、重新打开server.xml中被注释掉的这一句<Context path="/" reloadable="true" docBase="/opt/application/settlement/settlement.war" workDir="/opt/application/settlement/tmp"/>,将系统部署改成人工部署。
这样,部署过程就变成了:先将war包拷贝到docBase指定的目录下,然后停掉tomcat,然后删除webapps/ROOT目录,然后重启tomcat,就会完成自动部署。
这些操作,需要通过shell完成。
2、删除原来通过工具deploy的部分,在maven编译完成后,增加Execute shell操作,执行如下command:scp /root/.jenkins/workspace/Settle-1.1-settlement/settlement-web/target/settlement.war root@10.1.41.8:/opt/application/settlement/,将war包拷贝到远程机器上。(注:默认scp需要输入密码。解决办法请见《两台Centos服务器之间建立信任关系》)
3、然后,利用jenkins的ssh plugin插件,在远程机器(10.1.41.8)上执行如下shell,完成tomcat停掉,删除ROOT和重启tomcat的操作。
/opt/tomcat/tomcat-settlement/bin/shutdown.sh
rm -rf /opt/tomcat/tomcat-settlement/webapps/ROOT/
/opt/tomcat/tomcat-settlement/bin/startup.sh
4、配置完成后,重新执行构建。没有再出过问题。
shell相关配置请见下图: