您的位置:首页 > 其它

用Jenkins+Gradle+Jetty实现持续集成、测试、部署

2016-03-10 17:29 706 查看

Installation

Add the Jenkins repository to the yum repos, and install Jenkins from here.

sudo wget -O /etc/yum.repos.d/jenkins.repo
http://pkg.jenkins-ci.org/redhat/jenkins.repo

sudo rpm --import
https://jenkins-ci.org/redhat/jenkins-ci.org.key

sudo yum install jenkins


Installation of a stable version

There is also a LTS YUM repository for the LTS
Release Line

sudo
wget -O /etc/yum.repos.d/jenkins.repo
http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo

sudo rpm --import
https://jenkins-ci.org/redhat/jenkins-ci.org.key

sudo
yum install jenkins


-----------------------------------------------------------------------------

自动集成有很多种方案,本例用到的工具是Jenkins(前身Hudson)+Gradle+Jetty,关于Gradle可参考上一篇,Gradle常见问题
本例项目名称: WAP

Jetty

安装Jenkins

本例用的是最新版Jetty9,在安全上做了升级,直接放在webapps目录部署不能成功,需要在webapps目录下添加文件jenkins.xml,内容如下:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/jenkins</Set>
<Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/jenkins.war</Set>
<Get name="securityHandler">
<Set name="loginService">
<New class="org.eclipse.jetty.security.HashLoginService">
<Set name="name">Jenkins Realm</Set>
<Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/realm.properties</Set>
</New>
</Set>
</Get>
</Configure>

详见:https://wiki.jenkins-ci.org/display/JENKINS/Jetty

Jetty发布项目

WAR包:直接拷贝到Jetty的webapps目录即可,Jetty会监测变化进行热部署,无需手动重新启动jetty。

在Jetty启动项目时有可能遇到OOME错误,解决方法:

vim /jetty9/start.ini
##找到#--exec取消注释,修改如下:
--exec
-XX:MaxPermSize=512m

Jenkins配置

主要设置包括三项:

源码管理SCM,本例用的是SVN,必要胡设置有一项是SVN地址,另一项是项目相对存放路径;

触发策略
Poll SCM:根据SVN版本的变化选择是否需要进行项目集成;
本例是每天上午10点和下午3点,周一到周五(H 10,15 * * 1-5),用的是cron语法,详细请参考Jenkins说明;

执行Gradle构建脚本

##进入项目目录
cd ${WORKSPACE}/WAP
##赋予gradlew执行权限
chmod +x ./gradlew
##执行单元测试
./gradlew test

Gradle - build.gradle测试和部署部分

//======================Test集成测试===========================

test {
//测试结果,默认为成功!
ext.testResult = true

testLogging.showStandardStreams = true

// 使用 TestNG ,默认是JUnit
useTestNG()

// 设置JVM运行时参数
minHeapSize = "128m"
maxHeapSize = "512m"

jvmArgs '-XX:MaxPermSize=256m'

//一组测试完成,如果有一组测试是失败的则设置整体结果为失败
afterSuite { descriptor, result ->
if (result.resultType == TestResult.ResultType.FAILURE) {
testResult = false
}
}

}

/**
* 单元测试完成,如果成功则部署应用到测试环境的Jetty webapps目录,Jetty会自动热部署!
*/
task testCompleted <<{
if (test.testResult) {
try {
logger.lifecycle("Build war package!" )
//编译、打包
war.execute()
//COPY到部署目录进行部署
//可根据不版本分别分发到不同开发过程服务器,如M,BETA类型等
//本例只发布到集成测试服务器
//发布目录,可通过gradle.properties配置
def projectBuildDir = "/webapps/"
def warname = projectName + '-' + version + '.war'
def fromdir = buildDir.toString() + "/libs/" + warname
copy{
logger.lifecycle("Copy war  from:  $fromdir To : $projectBuildDir")
from fromdir
into projectBuildDir
rename { String fileName ->
projectName + '.war'
}
}
} catch (Exception ex) {
logger.error("Error!" + ex.message )
}
}
}

//在执行完集成测试后执行testCompleted任务,根据测试结果判断是否需要部署。
test.finalizedBy testCompleted

查看集成测试报告

测试报告地址:
http://xxx/jenkins/job/WAP/ws/WAP/target/reports/tests/index.html

构建控制台输出Console Output

Started by an SCM change
Building in workspace /home/hadoop/.hudson/jobs/WAP/workspace
Updating https://192.168.0.140/svn/JENKINS-TEST/WAP at revision '2013-07-10T15:28:28.900 +0800'
U         src/main/webapp/WEB-INF/spring-mvc.xml
U         src/main/webapp/tologin.html
At revision 12
[workspace] $ /bin/sh -xe /tmp/hudson166417485787384228.sh
+ cd /home/hadoop/.hudson/jobs/WAP/workspace/WAP
+ chmod +x ./gradlew
+ ./gradlew test
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:buildDashboard UP-TO-DATE
:testCompleted
Build war package!
Copy war  from:  /home/xxx/.hudson/jobs/WAP/workspace/WAP/target/libs/wap-1.0.war To : /usr/local/jetty9/webapps/

BUILD SUCCESSFUL

Total time: 35.719 secs
Finished: SUCCESS


#完!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: