使用jenkins+maven+nexus+sonar的持续集成构建系统2013年写的一篇老文,记录当时在公司搭建的jenkins持续集成系统。一、系统规划1、说明jenkins 自动构建系统,调用maven 进行构建maven 构建工具nexus 本地的maven仓库sonar 代码审查工具2、环境os centos6.4 jdk 1.6.0_45 jenkins 1.540 maven 3.1.1 nexus 2.4.0-09 sonar server 4.0sonar runner 2.3 3、目录分布分区 /opt/tiros-ci 500Gjenkins /opt/tiros-ci/jenkins maven /opt/tiros-ci/maven/apache-mavennexus /opt/tiros-ci/nexussonar server 4.0 /opt/sonarsonar runner 2.3 /opt/tiros-ci/sonar/sonar-runner-2.34、配置和调用流程 jenkins --作为总控系统 调用 maven和sonar 具体执行 maven----作为构建执行 nexus----作为maven仓库 sonar----作为代码审查环节,在构建之前 二、安装及基础配置修改 1、存储空间准备1.1、 创建新的lvm分区为500g[root@h210 ~]# lvcreate --size 500g --name tiros-ci /dev/datavg Logical volume "tiros-ci" created1.2、datavg之前是800G+,现在剩余300G+[root@h210 ~]# vgdisplay --- Volume group --- VG Name datavg System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 16 VG Access read/write VG Status resizable MAX LV 0 Cur LV 5 Open LV 4 Max PV 0 Cur PV 1 Act PV 1 VG Size 1.34 TiB PE Size 4.00 MiB Total PE 351881 Alloc PE / Size 260500 / 1017.58 GiB Free PE / Size 91381 / 356.96 GiB VG UUID nGWN9S-DUqr-aas9-8fVe-ROWf-WxGM-ixl9M61.3、lvdisplay可以看到新加的tiros-cilvdisplay --- Logical volume --- LV Name /dev/datavg/h238-01 VG Name datavg LV UUID 42BB9D-A8pO-DNfy-QCFe-aT53-XeOv-XYWl4Q LV Write Access read/write LV Status available # open 1 LV Size 195.31 GiB Current LE 50000 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:2 --- Logical volume --- LV Name /dev/datavg/website VG Name datavg LV UUID LaBaAe-kIKi-KegE-dPao-bKVh-IP8r-H2MNsm LV Write Access read/write LV Status available # open 1 LV Size 78.12 GiB Current LE 20000 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:3 --- Logical volume --- LV Name /dev/datavg/h218 VG Name datavg LV UUID JzGeMv-WhSS-5t0C-RHSa-cxmx-Ap8Y-05c15O LV Write Access read/write LV Status available # open 1 LV Size 195.31 GiB Current LE 50000 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:4 --- Logical volume --- LV Name /dev/datavg/h227 VG Name datavg LV UUID YeeYf1-Bxb0-lX6Y-nNX4-LZEC-ODGA-OSJ03f LV Write Access read/write LV Status available # open 1 LV Size 48.83 GiB Current LE 12500 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:6 --- Logical volume --- LV Name /dev/datavg/tiros-ci VG Name datavg LV UUID gmfR3F-btY6-pQ9c-U3Tb-w9Ky-zFJq-R0kzqL LV Write Access read/write LV Status available # open 0 LV Size 500.00 GiB Current LE 128000 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:7 --- Logical volume --- LV Name /dev/rootvg/lv00 VG Name rootvg LV UUID 3oGRhp-Y45D-QoEV-DU2G-p5Sh-rnmg-Wg2F5S LV Write Access read/write LV Status available # open 1 LV Size 8.00 GiB Current LE 2048 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Name /dev/rootvg/lv01 VG Name rootvg LV UUID XL3CnW-t7uT-lnGU-3xaW-JQd9-wmzj-6AKZM0 LV Write Access read/write LV Status available # open 1 LV Size 48.83 GiB Current LE 12500 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1 --- Logical volume --- LV Name /dev/rootvg/test VG Name rootvg LV UUID RldihX-Ij5w-5ozC-d7T2-6IE6-o87p-26togx LV Write Access read/write LV Status available # open 0 LV Size 150.00 GiB Current LE 38400 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:51.4、创建文件系统[root@h210 ~]# mkfs -t ext3 /dev/datavg/tiros-cimke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks32768000 inodes, 131072000 blocks6553600 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=42949672964000 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 102400000Writing inode tables: done Writing superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 34 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.1.5、挂载到系统mkdir /opt/tiros-ci mount /dev/datavg/tiros-ci /opt/tiros-ci -t ext32、jenkins安装2.1、下载repo文件[root@h210 tiros-ci]# wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo--2013-11-19 16:24:11-- http://pkg.jenkins-ci.org/redhat/jenkins.repoResolving pkg.jenkins-ci.org... 199.193.196.24Connecting to pkg.jenkins-ci.org|199.193.196.24|:80... connected.HTTP request sent, awaiting response... 200 OKLength: 75 [text/plain]Saving to: “/etc/yum.repos.d/jenkins.repo”100%[=================================================================================================>] 75 --.-K/s in 0s 2013-11-19 16:24:13 (7.85 MB/s) - “/etc/yum.repos.d/jenkins.repo” saved [75/75]2.2、安装keyrpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key2.3、安装jenkins[root@h210 tiros-ci]# yum install jenkinsLoaded plugins: fastestmirror, refresh-packagekitLoading mirror speeds from cached hostfileepel/metalink | 5.6 kB 00:00 * base: mirrors.hustunique.com * epel: mirrors.neusoft.edu.cn * extras: mirrors.hustunique.com * updates: mirrors.hustunique.combase | 3.7 kB 00:00 epel | 4.2 kB 00:00 epel/primary_db | 5.7 MB 00:04 extras | 3.4 kB 00:00 jenkins | 951 B 00:00 jenkins/primary | 23 kB 00:00 jenkins 213/213puppetlabs-deps | 1.9 kB 00:00 puppetlabs-products | 1.9 kB 00:00 updates | 3.4 kB 00:00 zabbix | 951 B 00:00 zabbix-non-supported | 951 B 00:00 Setting up Install ProcessResolving Dependencies--> Running transaction check---> Package jenkins.noarch 0:1.540-1.1 set to be updated--> Finished Dependency ResolutionDependencies Resolved=========================================================================================================================================== Package Arch Version Repository Size===========================================================================================================================================Installing: jenkins noarch 1.540-1.1 jenkins 57 MTransaction Summary===========================================================================================================================================Install 1 Package(s)Upgrade 0 Package(s)Total download size: 57 MInstalled size: 63 MIs this ok [y/N]: yDownloading Packages:jenkins-1.540-1.1.noarch.rpm | 57 MB 43:03 Running rpm_check_debugRunning Transaction TestTransaction Test SucceededRunning Transaction Installing : jenkins-1.540-1.1.noarch 1/1 warning: /etc/yum.repos.d/jenkins.repo created as /etc/yum.repos.d/jenkins.repo.rpmnewInstalled: jenkins.noarch 0:1.540-1.1 Complete!2.4、启动[root@h210 tiros-ci]# service jenkins startStarting Jenkins [ OK ]2.5、开机启动chkconfig jenkins on2.6、web配置http://192.168.1.210:8080/2.7、配置修改vi /etc/sysconfig/jenkins修改#JENKINS_HOME="/var/lib/jenkins"JENKINS_HOME="/opt/tiros-ci/jenkins"vi /etc/passwd将jenkins:x:493:487:Jenkins Continuous Build server:/var/lib/jenkins:/bin/falsejenkins:x:493:487:Jenkins Continuous Build server:/opt/tiros-ci/jenkins:/bin/falsemkdir /opt/tiros-ci/jenkins chown jenkins /opt/tiros-ci/jenkinsservice jenkins start3、maven安装http://mirror.bit.edu.cn/apache/maven/maven-3/3.1.1/binaries/apache-maven-3.1.1-bin.tar.gzcd apache-maven-3.1.1bin/mvn4、nexus 安装4.1、下载wget http://www.sonatype.org/downloads/nexus-latest-bundle.tar.gz4.2、安装 tar xvfz nexus-latest-bundle.tar.gz mkdir /opt/nexus mv nexus-2.4.0-09 /opt/tiros-ci/nexus/ 4.3、配置useradd nexuschown -R nexus:nexus /opt/tiros-ci/nexuscd nexus-2.4.0-09/bincp nexus nexus.bakvi nexus 增加 NEXUS_HOME="/opt/tiros-ci/nexus/nexus"RUN_AS_USER=nexus保存退出cp nexus /etc/init.dchkconfig nexus on4.4、运行service nexus start4.5、web配置http://192.168.1.210:8081/nexus更改默认的admin/admin123密码admin/somePassword 4.6、版本问题2.7的版本需要java7,java1.6会报错4.7、添加为maven的默认仓库cd /opt/maven/apache-maven-3.1.1/confcp setting.xml setting.xml.bakvi setting.xml 修改如下:mirror增加如下 profile段增加如下 4.8、添加自定义的仓库5、sonar 的安装使用 参考 http://docs.codehaus.org/display/SONAR/Installing安装sonar使用mysql5.1、创建sonar数据库脚本vi create_database.sql# Create SonarQube database and user.## Command: mysql -u root -p#CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;CREATE USER 'sonar' IDENTIFIED BY 'sonar';GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';FLUSH PRIVILEGES;5.2、执行建库mysql5.3、验证mysql show dataese;5.4、建立web服务器5.4.1、下载wget http://dist.sonar.codehaus.org/sonarqube-4.0.zip #下载源码 解压或者 wget -O /etc/yum.repos.d/sonar.repo http://downloads.sourceforge.net/project/sonar-pkg/rpm/sonar.repo 5.4.2、安装 (使用yum)yum install sonar5.4.3、使用 http://192.168.1.210:9000 默认登录 admin/admin 5.5、服务端配置5.5.1、admin登录5.5.2、进入配置-更新中心-availible plugins 选择java和chinese pack 插件进行安装 安装后需要重启sonar ,service sonar restart 5.5.3、进入 质量配置-java-sonar way 选择资源库-选择未激活-搜索 -右侧选择全部激活 三、jenkins配置1、安全设置设置可使用jenkins的用户,并给予相应权限2、全局设置2.1、git配置 根据构建的项目配置相应的git地址,2.2、jdk配置 2.3、邮件通知配置 用来配置各项目发生构建等动作时 SMTP服务器:smtp.tiros.com使用smtp认证用户:[email protected] #该邮箱仅用来发邮件,需要定期清理发件箱,此地址必须和 Jenkins Location-system admin email相同密码:!@#jenkinssmtp端口:25Reply-To Address:[email protected] #设置为项目负责人比较合适字符集:UTF-8 3、项目配置3.1、deploy 设置3.1.1、tomcat配置之tomcat-users.xml 3.1.2 tomcat配置之setting.xml 文件 admin admin 注:以上两个文件的用户密码需要修改 jenkins端配置 选中项目-配置-增加构建后步骤-选中“Deploy war/ear to the container" 然后各目标值填写如下: WAR/EAR files target/xxxx.war #此处填写的是jenkins构建在target目录下打包生成的war包名称 Context path xxxx.war #此处填写在tomcat在webapp下面的war名称 Container tomcat6.x #选择java容器和版本 Tomcat URL http://ip:port #上述选择tomcat,才有此项 最后保存 待解决问题1、采用unix认证方式,登录不进去2、采用jenkins认证方式,只能放开用户注册才能增加用户3、如果不先增加用户,就更改安全方式,更改后就无法登录系统。[root@h210 ~]# ssh-keygen -b 1024 -t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:db:95:cd:7b:45:bd:59:9d:f6:02:24:e3:02:1d:57:0e root@h210The key's randomart image is:+--[ RSA 1024]----+| ....E.o || ..o * +|| . . o o=|| . =..=|| S o +oo|| o . o.|| . . . .|| . || |+-----------------+3、jenkins的自动安装插件自重启问题[email protected]上面,会使系统崩溃。手动重启无此问题。4、jenkins自带的mailer插件邮件通知选择smtp认证 ,如果此处smtp认证的邮箱和Jenkins Location处的system admin email不同,会报“553 Mail from must equal authorized user” 09-23 01:38