zoukankan      html  css  js  c++  java
  • 持续集成配置实例

    前言

    持续集成(Continuous Intergration)做为软件开发重要环节,已经被提出、实施了很多年,在国内IT行业也已普及,它是为敏捷开发而创建。通过引入CI,可以减少重复工作、尽早暴露系统问题。

    需求

    1, 能够自动从源码管理系统(SVN、Git)中获取源码并编译

    2, 能够自动运行单元测试

    3, 能够自动部署到测试服务器

    4, 能够自动发邮件向管理员报告集成结果

    Jenkins安装

    Jenkins是一个开源的CI软件,有相当多的插件以及自带Web管理界面。到官网下载对应操作系统安装包(本文以Windows为例),一路选择默认设置即可安装完毕。

     第一次安装好后,会弹出页面让你输入管理员key

    Jenkins配置

    安装后还需要设置一些配置参数,如邮件服务器、帐号等。

    邮箱设置

    点击系统管理->系统设置,设置Mail相关参数。

     

    Jenkins URL:后台管理地址,可以根据实际情况修改端口号

    系统管理员邮件地址:需要与下面的Reply-To Address一样。

      

    注意:如果此处和我一样用163的邮箱服务器,密码一栏不是邮箱帐号的登录密码,而是在163网页邮箱中设置的客户端登陆密码。

    全局工具配置

    可以在系统管理->全局工具配置中设置JDK,Maven等软件安装目录

    新建任务

    点击左边导航栏“新建任务”,输入任务名称,选择项目类型为“构建自由风格的软件项目”

     如果你看到界面和我的不一样,那是因为安装的插件不一样,在插件管理中选择对应的插件安装。

    任务配置

    创建好任务后,需要设置源码位置、构建触发器、构建以及构建后操作。

    源码管理

    源码管理配置用于Jenkins自动获取源码。

     

    Repository URL:你的仓库地址,上图填写的是SVN配置,Git配置如下图。实际测试发现SVN获取的源码并不是最新的(当提交完代码立即手动构建是会出现该问题),因为获取源码插件是以时间戳下载,而不是以版本号。解决办法是在svn地址后面加上@HEAD。

    Credentials: 你的SVN或Git帐号

    构建触发器

    构建触发器指定构建触发条件,可以是定时构建,也可以是根据事件触发(比如上传代码触发)。

     

    示例表示每小时构建一次

    构建

    构建指定Build时要执行的参数,主要参数有clean package install等maven参数。如有用到其它Maven插件,还需添加maven插件相关参数,如checkstyle:check。

     

    这里的Maven就是在全局工具配置中设置的Maven。

     上面Goals(clean checkstyle:check package)表示先清理工程,然后检查代码规范,最后打包项目为war包。代码规范检查用的是maven checkstyle插件,该插件需要配置pom.xml。

     <reporting>
          <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-checkstyle-plugin</artifactId>
              <version>3.0.0</version>
              <reportSets>
                <reportSet>
                  <reports>
                    <report>checkstyle</report>
                  </reports>
                </reportSet>
              </reportSets>
            </plugin>
        </reporting>

    正确的做法是先在eclipse中正确调通maven,然后才把这些参数写在Jenkins中。

    构建后操

    构建后操作包括发布到测试服务器以及发送邮件通知。需要先配置tomcat帐号、pom.xml。

    Tomcat帐号配置

    Tomcat安装目录/onfig/tomcat-users.xm添加帐号,重启tomcat。

    <role rolename="manager-script"/>
    <user username="admin" password="admin" roles="manager-script"/>

    Content Path: 如果直接发布到根目录,则只需写“/”, 插件自动打包为root.war。

    上面的admin就是tomcat中配置的帐号。

    整体测试

    配置好任务各项参数后,点击立即构造就能看到运行结果。如果失败,可以点击控制台输出,查看失败原因。

    后记

    关于CI与Docker的集成本次没有涉及,在实践CI过程中,碰到的问题绝大多数与Jenkins本身无关,而与Maven、tomcat、git相关。

    完整pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.bf</groupId>
      <artifactId>fuqinghome</artifactId>
      <version>1</version>
      <packaging>war</packaging>
      
      <build>
          <sourceDirectory>src/com/bf</sourceDirectory>      
          <resources>
          <resource>
            <directory>resources</directory>
          </resource>
        </resources>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
              <encoding>utf8</encoding>
              <compilerArguments>
                  <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
              </compilerArguments>
            </configuration>
          </plugin>
          
          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
              <webXml>webappWEB-INFweb.xml</webXml>
              <warSourceDirectory>webapp</warSourceDirectory>
            </configuration>
          </plugin>      
        </plugins>
      </build>
      
      <reporting>
         <plugins>
           <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-checkstyle-plugin</artifactId>
             <version>3.0.0</version>
             <reportSets>
               <reportSet>
                 <reports>
                   <report>checkstyle</report>
                 </reports>
               </reportSet>
             </reportSets>
           </plugin>
           <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>findbugs-maven-plugin</artifactId>
          <version>3.0.6-SNAPSHOT</version>
        </plugin>  
          </plugins>  
       </reporting>
      
      <dependencies>
           <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
        </dependency>
           <dependency>
              <groupId>commons-lang</groupId>
              <artifactId>commons-lang</artifactId>
              <version>2.5</version>
          </dependency>
          <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-webmvc</artifactId>
              <version>4.3.13.RELEASE</version>
          </dependency>      
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.13.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-support</artifactId>
            <version>2.0.6</version>
        </dependency>
          <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.19</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
            <version>2.6.12</version>
        </dependency>
        <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.10</version>
        </dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.5</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
        <dependency>
            <groupId>xmlpull</groupId>
            <artifactId>xmlpull</artifactId>
            <version>1.1.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.5</version>
        </dependency>
        <dependency>
            <groupId>com.jslsolucoes</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.1.0</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
      </dependencies>
      
    </project>

    Spring Boot发布到Linux

    新项目采用Spring Boot开发,并且布署到Linux,相关配置如下参照,https://www.jianshu.com/p/d4f2953f3ce0

    start.sh

    #!/bin/bash
    export JAVA_HOME=/usr/local/jdk1.8.0_201
    echo ${JAVA_HOME}
    PROJECTNAME=assistant-admin
    echo "*******begin to get pid*********"
    pid=`ps -ef |grep $PROJECTNAME |grep -v "grep" |awk '{print $2}' `
    echo "pid is:"
    echo $pid
    echo "*******begin to kill*********"
    kill -9 $pid
    echo "****************"
    echo "*******begin to start*********"
    source /etc/profile
    nohup java -jar assistant-admin-1.jar  >> log.out 2>&1 &

    远程执行sh提示没有java,请参照https://blog.csdn.net/u013189824/article/details/85338221

    sh文件格式造成问题,请参照https://jingyan.baidu.com/article/215817f788e8c21eda1423ea.html

  • 相关阅读:
    MySQL字段数据全部查出【只保留中文、英文、数字、空格的词表】
    MySQL查看当前运行的事务和执行的账户
    【转】【MySQL报错】ERROR 1558 (HY000): Column count of mysql.user is wrong. Expected 43, found 39.
    【转】mysqldump的锁表的问题
    mysql 通过echo的方式写入数据库 中文乱码解决方案
    Python3.5爬取豆瓣电视剧数据并且同步到mysql中
    Python3.5爬取cbooo.cn数据并且同步到mysql中
    【转&参考】MySQL利用frm和ibd文件进行数据恢复
    [算法]从一道题引出variable-precision SWAR算法
    [转]nginx负载均衡的五种算法
  • 原文地址:https://www.cnblogs.com/season2009/p/9330899.html
Copyright © 2011-2022 走看看