Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1723072
  • 博文数量: 150
  • 博客积分: 660
  • 博客等级: 上士
  • 技术积分: 2480
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 11:39
文章分类

全部博文(150)

文章存档

2019年(4)

2018年(36)

2017年(53)

2016年(7)

2015年(3)

2014年(3)

2013年(27)

2012年(2)

2011年(1)

2006年(1)

2005年(13)

分类: 敏捷开发

2016-02-26 15:34:33

使用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.0
sonar runner 2.3
 
 
3、目录分布


分区 /opt/tiros-ci  500G




jenkins        /opt/tiros-ci/jenkins 


maven          /opt/tiros-ci/maven/apache-maven


nexus          /opt/tiros-ci/nexus


sonar server 4.0  /opt/sonar


sonar runner 2.3  /opt/tiros-ci/sonar/sonar-runner-2.3








4、配置和调用流程


 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" created
1.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-ixl9M6


1.3、lvdisplay可以看到新加的tiros-ci
lvdisplay
  --- 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:5
1.4、创建文件系统
[root@h210 ~]# mkfs -t ext3 /dev/datavg/tiros-ci
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
32768000 inodes, 131072000 blocks
6553600 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
4000 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
        102400000


Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done


This filesystem will be automatically checked every 34 mounts or
180 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 ext3


2、jenkins安装


2.1、下载repo文件


[root@h210 tiros-ci]# wget -O /etc/yum.repos.d/jenkins.repo
--2013-11-19 16:24:11--  
Resolving pkg.jenkins-ci.org... 199.193.196.24
Connecting to pkg.jenkins-ci.org|199.193.196.24|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 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、安装key
rpm --import


2.3、安装jenkins


[root@h210 tiros-ci]# yum install jenkins
Loaded plugins: fastestmirror, refresh-packagekit
Loading mirror speeds from cached hostfile
epel/metalink                                                                                                       | 5.6 kB     00:00     
 * base: mirrors.hustunique.com
 * epel: mirrors.neusoft.edu.cn
 * extras: mirrors.hustunique.com
 * updates: mirrors.hustunique.com
base                                                                                                                | 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/213
puppetlabs-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 Process
Resolving Dependencies
--> Running transaction check
---> Package jenkins.noarch 0:1.540-1.1 set to be updated
--> Finished Dependency Resolution


Dependencies Resolved


===========================================================================================================================================
 Package                         Arch                           Version                              Repository                       Size
===========================================================================================================================================
Installing:
 jenkins                         noarch                         1.540-1.1                            jenkins                          57 M


Transaction Summary
===========================================================================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)


Total download size: 57 M
Installed size: 63 M
Is this ok [y/N]: y
Downloading Packages:
jenkins-1.540-1.1.noarch.rpm                                                                                        |  57 MB     43:03     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running 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.rpmnew


Installed:
  jenkins.noarch 0:1.540-1.1                                                                                                               


Complete!
2.4、启动
[root@h210 tiros-ci]# service jenkins start
Starting Jenkins                                           [  OK  ]


2.5、开机启动


chkconfig jenkins on


2.6、web配置





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/false
jenkins:x:493:487:Jenkins Continuous Build server:/opt/tiros-ci/jenkins:/bin/false


mkdir /opt/tiros-ci/jenkins 


chown jenkins /opt/tiros-ci/jenkins




service jenkins start








3、maven安装





cd apache-maven-3.1.1


bin/mvn






4、nexus 安装


4.1、下载


wget


4.2、安装


 tar xvfz nexus-latest-bundle.tar.gz
 
 mkdir /opt/nexus
 
 mv   nexus-2.4.0-09 /opt/tiros-ci/nexus/
 
4.3、配置


useradd nexus


chown -R nexus:nexus /opt/tiros-ci/nexus


cd nexus-2.4.0-09/bin




cp nexus nexus.bak
vi nexus 增加 


NEXUS_HOME="/opt/tiros-ci/nexus/nexus"


RUN_AS_USER=nexus


保存退出


cp nexus /etc/init.d


chkconfig nexus on




4.4、运行


service nexus start


4.5、web配置





更改默认的admin/admin123密码


admin/somePassword   




4.6、版本问题


2.7的版本需要java7,java1.6会报错


4.7、添加为maven的默认仓库


cd /opt/maven/apache-maven-3.1.1/conf


cp setting.xml setting.xml.bak


vi setting.xml  修改如下:


mirror增加如下


 
   
   
      Nexus
      central
      Nexus Public Mirror
     
   





   
       
       nexus-public-snapshots
       public-snapshots
       
   

   
       
       nexus
       *
       
   



    
 





profile段增加如下


   
        development
       
         
             central
             
             true
             true
         

       

       
         
             central
             
             true
             true
         

       

   



   
       
      public-snapshots
     
       
           public-snapshots
           
           false
           true
       

     

     
         
            public-snapshots
           
            false
            true
         

     

   







4.8、添加自定义的仓库








5、sonar 的安装使用 


参考


安装sonar使用mysql


5.1、创建sonar数据库脚本


vi create_database.sql


# Create SonarQube database and user.
#
# Command: mysql -u root -p < create_database.sql
#


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、执行建库


mysql < create_database.sql 




5.3、验证


mysql 


show dataese;






5.4、建立web服务器


5.4.1、下载


wget   #下载源码 解压
或者


 wget -O /etc/yum.repos.d/sonar.repo


5.4.2、安装 (使用yum)


yum install sonar




5.4.3、使用


 
 
 默认登录 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认证
用户:jenkins@tiros.com.cn             #该邮箱仅用来发邮件,需要定期清理发件箱,此地址必须和 Jenkins Location-system admin email相同
密码:!@#jenkins
smtp端口:25
Reply-To Address:lihui@tiros.com.cn   #设置为项目负责人比较合适
字符集:UTF-8
     


3、项目配置


3.1、deploy 设置

3.1.1、tomcat配置之tomcat-users.xml


















 







  3.1.2 tomcat配置之setting.xml 文件











   


      servername


      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         #上述选择tomcat,才有此项
        
        最后保存
        


待解决问题


1、采用unix认证方式,登录不进去


2、采用jenkins认证方式,只能放开用户注册才能增加用户


3、如果不先增加用户,就更改安全方式,更改后就无法登录系统。




[root@h210 ~]# ssh-keygen -b 1024 -t rsa
Generating 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@h210
The key's randomart image is:
+--[ RSA 1024]----+
|      ....E.o    |
|       ..o *    +|
|        . . o  o=|
|         .   =..=|
|        S   o +oo|
|         o .   o.|
|        . .   . .|
|               . |
|                 |
+-----------------+


3、jenkins的自动安装插件自重启问题


1.541@centos6.3上面,会使系统崩溃。手动重启无此问题。


4、jenkins自带的mailer插件


邮件通知选择smtp认证 ,如果此处smtp认证的邮箱和Jenkins Location处的system admin email不同,会报“553 Mail from must equal authorized user”
阅读(3961) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~