持续集成CI(二)Jenkins

来源:互联网 发布:发货打印软件 编辑:程序博客网 时间:2024/05/29 19:48

1.  Jenkins 简介

Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作,其前身叫hudson ,功能包括:

1、持续的软件版本发布/测试项目。
2、监控外部调用执行的工作。

Jenkins 发布和运行的形式都很简单,您可以去 Jenkins 官网下载安装包后,只需一个“java -jar jenkins.war”命令就能将其运行起来。官网:http://jenkins-ci.org/

Jenkins 特性

l 易于安装-只要把jenkins.war部署到servlet容器,不需要数据库支持。
l 易于配置-所有配置都是通过其提供的web界面实现。
l 集成RSS/E-mail通过RSS发布构建结果或当构建完成时通过e-mail通知。
l 生成JUnit/TestNG测试报告。
l 分布式构建支持Jenkins能够让多台计算机一起构建/测试。
l 文件识别:Jenkins能够跟踪哪次构建生成哪些jar,哪次构建使用哪个版本的jar等。
l 插件支持:支持扩展插件,你可以开发适合自己团队使用的工具。

Jenkins 使用步骤

平台具体的实现和配置流程,主要包含以下步骤:

安装 Jenkins 及必要的第三方插件;
建立新用户及配置权限;
为自动化测试建立和配置新任务;
配置用户输入定制化选项;
配置执行报告保存;
配置 email 提醒;

2.  Jenkins 安装--window版本

step1: 首先保证系统中已经安装了jdk,最好是jdk1.5以上(适合linux和windows)。

step 2:   http://jenkins-ci.org/  下载本机环境相对应包安装。
第一种是直接在http://jenkins-ci.org/ 页面上下载jenkins for windows 的安装包. 下载好了之后安装即可.
第二种是下载jenkins的war包, 用java命令执行war包, 如java -jar jenkins.war。此时默认端口号为8080.
如果 命令为: java -jar jenkins.war -httpPort=8082,此时使用的端口号为8082

更多启动命令:

--httpPort=$HTTP_PORTRuns Jenkins listener on port $HTTP_PORT using standard http protocol. The default is port 8080. To disable (because you're using https), use port -1.--httpListenAddress=$HTTP_HOSTBinds Jenkins to the IP address represented by $HTTP_HOST. The default is 0.0.0.0 — i.e. listening on all available interfaces. 
For example, to only listen for requests from localhost, you could use: --httpListenAddress=127.0.0.1--httpsPort=$HTTP_PORTUses HTTPS protocol on port $HTTP_PORT--httpsListenAddress=$HTTPS_HOSTBinds Jenkins to listen for HTTPS requests on the IP address represented by $HTTPS_HOST.--ajp13Port=$AJP_PORTRuns Jenkins listener on port $AJP_PORT using standard AJP13 protocol. The default is port 8009. To disable (because you're using https), use port -1.--ajp13ListenAddress=$AJP_HOSTBinds Jenkins to the IP address represented by $AJP_HOST. The default is 0.0.0.0 — i.e. listening on all available interfaces.--argumentsRealm.passwd.$ADMIN_USERSets the password for user $ADMIN_USER. If Jenkins security is turned on, you must log in as the $ADMIN_USER in order to configure Jenkins or a Jenkins project. NOTE: You must also specify that this user has an admin role. (See next argument below).--argumentsRealm.roles.$ADMIN_USER=adminSets that $ADMIN_USER is an administrative user and can configure Jenkins if Jenkins' security is turned on. SeeSecuring Jenkins for more information.-Xdebug -Xrunjdwp:transport=dt_socket,address=$DEBUG_PORT,server=y,suspend=nSets debugging on and you can access debug on $DEBUG_PORT.-logfile=$LOG_PATH/winstone_`date +"%Y%m-%d_%H-%M"`.logLogging to desired file-XX:PermSize=512M -XX:MaxPermSize=2048M -Xmn128M -Xms1024M -Xmx2048Mreferring to these options for Oracle Java 
step3:   在浏览器中输入 http://localhost:8080 或 http://IP:8080访问。

注: 如果在step2中修改了端口号,则输入http://localhost:端口号 访问

进入界面会发现Jenkins正在初始化,稍等片刻,可以看到主界面:



step 4:  选择菜单左边的管理JENKINS(Manage Jenkins), 找到安装windows服务(install as windows service),之后选择安装路径,等着安装成功吧。


3.  命令行操作说明

假设Jenkins的URL为:http://localhost:8080(或http://localhost:8080/jenkins/)

基本的格式为:

java -jar jenkins-cli.jar [-s JENKINS_URL] command [options][args]

具体实例
命令参数说明help
 
<span style="font-size:18px;">查看所有内嵌命令的基本功能  </span><span style="font-size:18px;"><span style="color: rgb(255, 0, 0);">无参数</span></span>
</pre><pre name="code" class="java">java -jar jenkins-cli.jar -s http://localhost:8080/jenkins/  help
build执行一次构建

JOB                 : Name of the job to build 要构建的job名称
 -c                  : Check for SCM changes before starting the build, and if
                       there's no change, exit without doing a build
 -p                  : Specify the build parameters in the key=value format.
 -r VAL              : Number of times to retry reading of the output log if it
                       does not exists on first attempt. Defaults to 0. Use
                       with -v.
 -s                  : Wait until the completion/abortion of the command
 -v                  : Prints out the console output of the build. Use with -s
 -w                  : Wait until the start of the command
 --username VAL      : User name to authenticate yourself to Jenkins
 --password VAL      : Password for authentication. Note that passing a
                       password in arguments is insecure.
     --password-file VAL : File that contains the passwordlogin保存登录状态

--username VAL      : User name to authenticate yourself to Jenkins
 --password VAL      : Password for authentication. Note that passing a
                       password in arguments is insecure.
     --password-file VAL : File that contains the passwordlogoutDeletes the credential stored with the login command. 注销mail Reads stdin and sends that out as an e-mailrestartRestart Jenkins 重启Jenkinssafe-restartSafely restart Jenkins 安全重启Jenkins,即等待已有的构建完成再重启safe-shutdownwait for existing builds to be completed, and then shut down Jenkins.安全关闭Jenkinslist-plugins
Outputs a list of installed plugins. 列出安装的所有插件list-jobs
列出所有的jobs名称,其后可以接视图名称,默认显示全部install-plugin
Installs a plugin either from a file, an URL, or from update center. 安装插件get-job Dumps the job definition XML to stdout 得到job定义的XML文档

JOB                 : Name of the job
 --username VAL      : User name to authenticate yourself to Jenkins
 --password VAL      : Password for authentication. Note that passing a
                       password in arguments is insecure.
     --password-file VAL : File that contains the passworddisable-job
Disables a job 相当于“停止构建” 


JOB                 : Name of the job
 --username VAL      : User name to authenticate yourself to Jenkins
 --password VAL      : Password for authentication. Note that passing a
                       password in arguments is insecure.
     --password-file VAL : File that contains the passworddelete-job
JOB                 : Name of the job
 --username VAL      : User name to authenticate yourself to Jenkins
 --password VAL      : Password for authentication. Note that passing a
                       password in arguments is insecure.
     --password-file VAL : File that contains the passwordcreate-jobCreates a new job by reading stdin as a configuration XML file.
从一个XML文档中创建一个job


java -jar /home/jboss/.jenkins/jenkins-cli.jar -s http://localhost:8080/jenkins/ create-job testttt
更多命令参考:http://blog.sina.com.cn/s/blog_5c9288aa0102v1zc.html

tips  :
为了操作方便,建议先通过login命令保存登录状态,这样就不需要每次执行操作都加上用户名密码参数了;缺点是安全性不好,记得操作结束之后logout

linux安装配置:

http://my.oschina.net/xinson/blog/473027

4.  jenkins须知

(1) Jenkins把配置文件和数据保存在$user.home/.jenkins中

5.  配置Jenkins

(1)安装插件

系统管理--管理插件--可选插件。 Jenkins的插件安装管理配置都很简单,通过web直接全能搞定。
  
勾选需要的插件,到页面最下面有两个按钮“Install without restart” “Download now and install after restart”,根据需要点选提交开始安装.

(2)Jenkins如何对账号管理(用户注册)

http://jingyan.baidu.com/album/7082dc1c719577e40a89bd37.html?picindex=2

(3) 用户权限分配

点击“Assign Roles”如下图所示,在“Global roles”下“User/group to add”栏中输入添加的用户名,然后勾选管理组。记得把默认的匿名用户“Anonymous”的默认admin权限去掉,在添加管理员之后,否则不需登录就能控制整个Jenkins的权限;在“Project roles”下“User/group to add”栏中输入添加的用户名,然后勾选对应构建权限名。设置完保存即可

6. 进阶 


1) 使用jenkins前疑问解答

疑问1:jenkins不用maven\ant 或者脚本等构建工具,能构建项目吗?

答:不能自动构建
你必须要有自动构建的脚本规则,告诉jenkins使用哪一种构建方案,使用什么样的构建命令和构建参数去执行等等。不然jenkins怎么知道你的项目如何构建
http://www.oschina.net/question/2254073_175943?sort=time

疑问2: eclipse 如何自动生成ant需要的build.xml文件?
选择你要生成Build.xml文件的项目,右键. Export-> General -> Ant Buildfiles .
点Next,再点Finish

疑问3:jenkins自动构建普通的java项目?
注:Jenkins支持两种工程:FreeStyle工程和Maven工程。如果代码是用Maven构造的,可以直接建立Maven工程
例如ant构建(注: 有了build.xml,后 ant taskname 即可我们build.xml里的task name),项目代码在本地
step1: 安装ant,配置win7环境变量
step2:  启动jenkins

java -jar jenkens.war
http://localhost:8080/ 就可到jenkins的首页。
step3: 
http://www.51testing.com/html/19/n-1360719-2.html

创建job
http://blog.csdn.net/littlechang/article/details/8642149
http://blog.sina.com.cn/s/blog_a39910330102vpkt.html
http://lvjun106.iteye.com/blog/1867118

jenkins + git+maven做持续集成

http://my.oschina.net/idiotsky/blog/201659

Jenkins与sonar集成

http://blog.sina.com.cn/s/blog_b5fe6b270102v7xm.html

7. 参考

http://blog.sina.com.cn/s/blog_5c9288aa0102v1zc.html

http://lvjun106.iteye.com/blog/1867118
0 0