第二集 hudson+gradle+git+maven(非必选)持续集成编译,打包,部署到线上环境集成
前言
在持续集成中,讲解了关于如果编译,打包,发布jar包到maven私服。在下面一集中,也就是第二集中,开始讲解
如何持续集成可运行的服务包到测试和正式环境。实战
第一步 准备工作
进入hudson Jobs Status 主页,如图所示:
这里我以用户登陆服务作为例子进行操作讲解,如图所示:
点击Console -> 再点击左侧菜单的Configure Job 进入Job Configurations主页
图1
图2
图3
接下来也就是重点了,结合脚本一一讲解整个服务包(tar包)如何生成并且上传线上服务器
第二步 具体参数配置
[特别注意:下面配置的参数前面4个需要结合最后贴出的gradle脚本一起使用,参数是传递给gradle脚本使用的。]
1.配置代码的git分支参数
2.配置打包存放的目录参数
3.配置当前打包的版本参数
4.设置打包时是否要加入第三方jar包
5.设置需要打包的类型
6.设置需要部署的服务器参数
第三步 配置git仓库最新代码的shell脚本
配置bulid的脚本 这里的脚本主要是获取git仓库中的最新代码,操作如下:
第四步 配置gradle打包的脚本文件
第五步 配置读取打包类型和执行部署shell,python脚本
[补充:具体如何调用python以及如何编写python这里就不贴出来了,可以去问Google哦]
最后 gradle 登场
gradle脚本代码,供参考哦
apply plugin: 'java'apply plugin: 'eclipse'version = '1.0'// 定义发布包的路径def releaseDir = System.properties['deployToDir']def gitBranch = System.properties['gitBranch'];def myZipName = gitBranch.replace("/","-") + "-" + new Date().getTime();def bigVer = System.properties['bigVer'];def isCommonLibs = System.properties['commonLibs'];// 设置 Java 源码所在目录sourceSets.main.resources.srcDirs 'config'// 设置 JDK 版本sourceCompatibility = 1.8targetCompatibility = 1.8// 设置编译使用utf-8编码tasks.withType(JavaCompile) { options.encoding = "UTF-8"}//maven仓库地址repositories { maven { url "http://ip:8081/nexus/content/groups/public/" }}// 依赖jar包dependencies { compile "commons-logging:commons-logging:1.1.1", "org.javassist:javassist:3.20.0-GA", "log4j:log4j:1.2.15", "org.springframework:spring-core:4.2.1.RELEASE", "org.springframework:spring-context:4.2.1.RELEASE", "org.slf4j:slf4j-api:1.7.6", "org.slf4j:slf4j-log4j12:1.6.1", "com.fs.fishsaying-service-api:fishsaying-service-api:0.0.2", "com.google.guava:guava:18.0", "com.fs.common:fs-common-service:0.0.1", "commons-io:commons-io:2.4", "org.jboss.resteasy:resteasy-netty4:3.0.12.Final", "org.jboss.resteasy:resteasy-guice:3.0.12.Final", "org.apache.commons:commons-lang3:3.4", "com.fs.fs-common-db:fs-common-db:0.0.1", "com.eclipse.java:javax-persistence:2.1.0", "org.msgpack:msgpack:0.6.12", "redis.clients:jedis:2.7.3", "mysql:mysql-connector-java:5.1.36", "com.eclipse.java:eclipselink:2.6.0", "org.jboss.resteasy:resteasy-jackson-provider:2.3.5.Final", "io.netty:netty-all:4.0.31.Final", "com.fishsaying:common-pool3:0.0.1", "com.google.code.gson:gson:2.2.2", "com.fasterxml.jackson.core:jackson-annotations:2.6.3", "com.fasterxml.jackson.core:jackson-core:2.6.3", "org.mongodb:mongodb-driver:3.0.4", "org.elasticsearch:elasticsearch:2.0.0", "com.fasterxml.jackson.core:jackson-databind:2.5.3", "com.aliyun.oss:oss:2.0.5", "sun.misc.BASE64Decoder:BASE64Decoder:1.0", "org.jboss.resteasy:resteasy-multipart-provider:2.3.1.GA", "net.sf.oval:oval:1.85", "com.aliyun.taobao:taobao-sdk:0.0.1", "com.aliyun.trance:aliyun-sdk:0.0.1", "com.alibaba:fastjson:1.2.4", "javax.mail:mail:1.4.7", "cn.jpush.api:jpush-client:3.2.8", "org.apache.httpcomponents:httpclient:4.5.1", "com.fish.saying:fishsaying-log-sdk:2.0.0", "org.quartz-scheduler:quartz:2.2.1", "org.quartz-scheduler:quartz-jobs:2.2.1" compile ("com.alibaba:dubbo:2.8.4") { exclude group:'org.springframework', module: 'spring-aop' } compile ("com.alibaba:dubbo:2.8.4") { exclude group:'org.springframework', module: 'spring-expression' } compile ("com.alibaba:dubbo:2.8.4") { exclude group:'org.springframework', module: 'spring-web' } compile ("com.alibaba:dubbo:2.8.4") { exclude group:'org.springframework', module: 'spring-beans' } compile ("com.alibaba:dubbo:2.8.4") { exclude group:'org.springframework', module: 'spring-core' } compile ("com.alibaba:dubbo:2.8.4") { exclude group:'org.springframework', module: 'spring-context' } compile ("com.alibaba:dubbo:2.8.4") { exclude group:'org.apache.httpcomponents', module: 'httpcore' } compile ("com.alibaba:dubbo:2.5.3") { exclude group:'org.springframework', module: 'spring' } compile ("com.alibaba:dubbo:2.5.3") { exclude group:'org.jboss.netty', module: 'netty' } compile ("org.apache.zookeeper:zookeeper:3.4.5") { exclude group:'org.jboss.netty', module: 'netty' } compile ("com.101tec:zkclient:0.6") { exclude group:'com.101tec', module: 'netty' } testCompile "junit:junit:3.8.1"}jar { exclude("app.properties","appContext.xml" ,"deploy.properties","deploy.properties.temp" ,"log4j.properties","provider.xml") manifest { attributes 'Implementation-Title': '鱼说应用', 'Implementation-Version': version attributes 'Main-Class': 'com.fs.FishApp' }}uploadArchives { repositories { flatDir { dirs releaseDir+"fs_libs" } }}uploadArchives.doFirst { def destination = file(releaseDir) if(destination.exists()){ destination.deleteDir(); }}//gradle任务task copyCommonLibs(type: Copy) { dependsOn uploadArchives from configurations.compile into releaseDir+"libs" exclude 'spring-2.5.6*.jar'}task deleteOtherLibs(type: Copy) { dependsOn copyCommonLibs delete fileTree(dir: releaseDir+"libs" , exclude: ['fs-common-service-0.0.1.jar','fishsaying-service-api-0.0.2.jar','fs-common-db-0.0.1.jar'])}// 拷贝配置文件task copyAppConfig(type: Copy) { dependsOn deleteOtherLibs from sourceSets.main.resources.srcDirs into releaseDir+"config" exclude 'deploy.properties','META-INF','appContext.xml'}// 拷贝启动相关的shelltask copyShell(type: Copy) { dependsOn copyAppConfig from project.projectDir into releaseDir include '*.sh'}copyShell << { def destination = file(releaseDir+"logs/") destination.mkdirs();}// 压缩任务task zipApp(type: Tar) {//Zip dependsOn copyShell baseName = "fs-login-"+myZipName from releaseDir}deleteOtherLibs.onlyIf { isCommonLibs.equals("false")}
持续集成部署就是这样的一套操作流程,当然还有很多的要完善的地方,这里只是讲解了如何搭建这套工具平台。接下来会不断优化并且会加入服务端自动化单元测试,敬请期待第三集哈...thx ^_^