zoukankan      html  css  js  c++  java
  • Jenkins(二)

    官网:https://wiki.jenkins-ci.org/display/JENKINS/Meet+Jenkins

    我的这篇文章不过简单的依据上文,介绍Jenkins提供了哪些功能。详细大家还是要自己学习啦~


    官网首页就提供了windows版本号的Jenkins安装包。我们能够下载一个用于学习。安装后自己主动打开http://localhost:8080,你就能看见Jenkins的界面了。

    其它也须要安装的是:

    1,Jenkins是java程序,因此须要安装JDK。

    2,同一时候执行job须要提供repository,也就是存放Jenkins定期poll源码的地方。我们能够去github免费注冊一个。

    3,假设想在Jenkins中使用ant,maven等,则还须要单独安装。但不是必须的。


    启动Jenkins

    Jenkins天生支持unix-like system。

    • 直接执行

    好吧,Jenkins是一个java程序,所以要执行它,仅仅须要:

    $ java -jar jenkins.war

    我们也能够使用nohup命令,让Jenkins在后台执行。

    之后打开URL http://myServer:8080 就能够方便的操作Jenkins了

    官网给了一个sh的样例,用于启动Jenkins。能够參考一下。

    • 在Servlet container中执行

    Alternatively, if you have a servlet container that supports Servlet 2.4/JSP 2.0, such as Glassfish v2, Tomcat 5 (or any later versions), then you can run them as services, and deployjenkins.war as you would any other war file.

    For example,
    you could simply place the jenkins.war file in Tomcat’s webapps directory.  此时使用的URL默认就变成:

    http://localhost:8080/jenkins


    同一时候Jenkins提供一些默认不会启动的特殊的功能,參考以下的link来enable它们。

    https://wiki.jenkins-ci.org/display/JENKINS/Features+controlled+by+system+properties


    Jenkins的文件夹结构

    和CruiseControler一样,Jenkins须要一个文件夹来存储相关文件:JENKINS_HOME。默觉得 ~/.jenkins。即为user的home文件夹下的一个隐藏文件夹。我们也能够更改JENKINS_HOME,指向我们希望的地方。

    (注意由于是隐藏文件夹,所以须要使用ls -al 才干看到)

    JENKINS_HOME
     +- config.xml     (jenkins root configuration)
     +- *.xml          (other site-wide configuration files)
     +- userContent    (files in this directory will be served under your http://server/userContent/)
     +- fingerprints   (stores fingerprint records)
     +- plugins        (stores plugins)
     +- jobs
         +- [JOBNAME]      (sub directory for each job)
             +- config.xml     (job configuration file)
             +- workspace      (working directory for the version control system)
             +- latest         (symbolic link to the last successful build)
             +- builds
                 +- [BUILD_ID]     (for each build)
                     +- build.xml      (build result summary)
                     +- log            (log file)
                     +- changelog.xml  (change log)

    假设有权限管理,则在HOME文件夹下还会有users文件夹。

    从文件夹结构来看,和CruiseController很相似。当中config.xml是Jenkins重要的配置文件。我们都知道Jenkins用于monitor多个build,而jobs这个文件夹无疑就是存储每一个build相关信息的地方。

    总的来说,Jenkins文件夹结构很直白,简洁。


    备份和恢复

    备份和恢复很easy,就是简单的copy Jenkins的文件夹就好了:

    All the settings, build logs, artifact archives are stored under the JENKINS_HOME directory. Simply archive this directory to make a back up. Similarly, restoring the data is just replacing the contents of the JENKINS_HOME directory from a back up.


    移动/拷贝/重命名 job

    因为每一个jobs都有自己单独的文件夹,我们能够非常easy的:

        1,move a job from one installation of Jenkins to another by simply copying the corresponding job directory.
       ,2,make a copy of an existing job by making a clone of a job directory by a different name.
        3,rename an existing job by renaming a directory.

    改动后运行以下的命令刷新:

    http://[jenkins-server]/[command]

    在这里[command]能够是:exit 退出,restart 重新启动, reload 重载。


    创建一个Project

    由于Jenkins能够用于执行各种CI,測试,批处理任务等等,所以在Jenkins中将这些任务统称为“free-style software project”.

    Jenkins也提供了其它类型的jobs,比如:

    1,假设项目是Maven,Jenkins还提供了一种仅用于Maven Project的job。但事实上free-style software projec仍然能够用于创建Maven项目,仅仅只是这样的更适合Maven项目,结合的更好而已。

    2,也能够创建一个"Monitor an external job“用于监控外部进程。

    3,或者一个Matrix project,也就是multi-configuration project。

    我不确定是否仅有这4种job,或许使用插件能够创建很多其它类型的job,大家自己看资料吧。


    以下是怎样创建一个最常见的“free-style software project"的过程:

    Go to Jenkins top page, select "New Job", then choose "Build a free-style software project". This job type consists of the following elements:

    • optional SCM, such as CVS or Subversion where your source code resides. 指定源码在哪。

    Note: In software engineering, software configuration management (SCM) is the task of tracking and controlling changes in the software.

    • optional triggers to control when Jenkins will perform builds. 指定Jenkins何时触发一次build。
    • some sort of build script that performs the build (ant, maven, shell script, batch file, etc.) where the real work happens 触发build时,使用的脚本文件,比如ant。在这个脚本文件里,我们能够从svn下载最新代码,删除上次build的暂时文件,创建必要文件夹,编译源码,执行,打包等一系列工作。
    • optional steps to collect information out of the build, such as archiving the artifacts and/or recording javadoc and test results. 收集log信息。
    • optional steps to notify other people/systems with the build result, such as sending e-mails, IMs, updating issue tracker, etc. 通知相关人员build的结果。

    比如我们使用的build script就是ant,在Jenkins中执行ant。在脚本文件里,能够直接使用Jenkins提供的一些变量。

    同一时候,每一个job能够有多个step。比如:将执行程序定义为step1,执行单元測试定义为step2,生成coverage报告定义为step3。

    同一时候还能够定义post-build action。比如:生成javadoc,或清理程序执行的暂时文件文件夹等。


    自己主动执行Build

    触发一个build有三种方式:

    • Builds in Jenkins can be triggered periodically (on a schedule, specified in configuration) 这里定义schedule的语法是unix常见的cron语法。
    • Or when source changes in the project have been detected

    能够设置Jenkins定时检查SVN是否发生了变化,也能够手动检查:http://YOURHOST/jenkins/job/PROJECTNAME/pollong。也能够设置Jenkins为post-commit,这个方式尤其适用于那些检查是否代码改变会花费非常长时间的情况。

    • Or they can be automatically triggered by requesting the URL:

    http://YOURHOST/jenkins/job/PROJECTNAME/build


    Distributed builds

    Jenkins supports the "master/slave" mode, where the workload of building projects are delegated to multiple "slave" nodes, allowing single Jenkins installation to host a large number of projects, or provide different environments needed for builds/tests.

    在现实中须要使用distributed builds情况非常多,比如:一个web application的build,须要分别验证firefox和IE的行为,那么就须要到windows机器上执行IE。

    或由于性能问题,将build分布到多个slave节点去。


    到Jenkins的管理界面,就能够方便的加入节点。配置节点时,须要提供节点所在的机器,登陆usernamepassword,使用的文件夹等。

    可是slave并不须要再安装Jenkins。jenkins会自己主动启用slave agent,将build须要tools考到远程机器上。

    须要注意的是:the build results and artifacts will always end up on the master server. 因此不须要跑到各个节点去查看build产生的文件,log等。

    事实上在slave节点,会创建一个本地的workspace,并在执行时使用这个workspace。由于毕竟build执行在slave节点上,所以这个节点肯定要有执行build须要的全部因素。

    总之加入节点并远程执行build真是太方便了~


    加入节点后,在master Jenkins home文件夹下会出现关于该节点的配置文件。

    Jenkins将自己主动决定在哪个节点上执行build,依据下列策略:

    Some slaves are faster, while others are slow. Some slaves are closer (network wise) to a master, others are far away. So doing a good build distribution is a challenge. Currently, Jenkins employs the following strategy:

    1. If a project is configured to stick to one computer, that's always honored.
    2. Jenkins tries to build a project on the same computer that it was previously built.
    3. Jenkins tries to move long builds to slaves, because the amount of network interaction between a master and a slave tends to be logarithmic to the duration of a build (IOW, even if project A takes twice as long to build as project B, it won't require double network transfer.) So this strategy reduces the network overhead.

    Jenkins通过执行slave agents来完毕分布式build。最常见的情况是:slave agent执行在各个slave 节点。master通过SSH远程启动/停止slave agent,进而控制各个节点的行为。

    一共同拥有下列4种方式启动slave agent:

    •The master starts the slave agents via ssh
    • Starting the slave agent manually using Java Web Start
    • Installing the slave agent as a Window service
    • Starting the slave agent directly from the command line on the slave machine from the command line

    须要注意的是这4种方式适用于不同的情况,比如slave节点在防火墙后,导致master无法通过SSH启停slave agent。此时仅仅能用后三种方式,可是往往有一些弊端,比方master无法自己主动停止/重新启动 slave agent.

    一旦加入node成功,你就能够在job中指定这个job在哪个node执行:

    Restrict where this project can be run (假设不指定则由Jenkins自行决定,即能够在slave节点执行,也能够在master节点执行,甚至在一次build中就能够自行来回切换)。


    配置Jenkins,让它收集很多其它的log

    https://wiki.jenkins-ci.org/display/JENKINS/Logging

    我想这对于初学Jenkins的人,用来推断问题所在太实用了。


    在流行持续集成的今天,我们在各个环境:alpha,beta和production 都部署了唯一的Jenkinsserver。

    Jenkinsserver统一负责该环境内全部组件的持续集成。也就是说,一个Jenkinsserver会有非常多个build。所以有时会用到上面提到的”Monitor an external job“。

    但不是Distributed Builds。


    同一时候因为Jenkins会进入每一个环境,包含production,因此会使用auto deployment的方式,自己主动完毕Jenkins在各个环境的部署。


    用户管理

    毫无疑问Jenkins中须要实用户管理的功能,由于除开发者外,有多种角色的人须要查看build的结果。

    在Jenkins中的系统管理中,能够设置“不论什么用户能够做不论什么事” 或 “登录用户能够做不论什么事”。

    因此前一个选项意味着,不论什么浏览JenkinsURL的用户都能够改动Jenkins。或仅仅有登录用户才干做改动。

    所以我把Jenkins中的用户划分为两类:可登录用户和不可登录用户。

    1,仅仅要是改动过repository,即对build产生过影响的用户,都会被Jenkins记录在本地的database中。这类用户我们能够在Jenkins界面->查看用户中浏览。

    但这类用户不是正式的Jenkins用户,也不能登录Jenkins。这类用户的权限由上面说的系统管理中的配置决定。通常仅仅有查看build的权限,没有改动权限。

    2,仅仅有在Jenkins中明白注冊的用户,才可以登录Jenkins,而且有权限控制。同一时候注冊过的用户,在JENKINS_HOME文件夹下的users文件夹下,都有一个单独的文件夹来存储相关信息。我不太清楚是否可以通过copy/paste将用户部署到其它地方去,回头測试一下。


    既然要满足各种人对jenkins使用的各种需求,因此权限管理远没有这么简单。详细大家还得自己去看啊~


    Jenkins Script Console

    Jenkins提供了一个script console Groovy script which allows to run arbitrary scripts on the Jenkins server or on slave nodes. This feature can be accessed from the "manage Jenkins" link。

    也能够通过URL直接訪问:http://myserver:8080/hudson/script

    可惜仅仅能用Groovy 反证我不懂。












  • 相关阅读:
    ASP.NET MVC 异常捕获
    Jquery 扩展方法
    Spring.NET笔记1
    ASP.NET MVC Ninject 实现依赖注入
    ASP.NET MVC Unity实现依赖注入
    windows service
    反射用法
    抽象工厂核心反射
    (C#)中的DataSet、string、DataTable等对象转换成Json
    .NET批量删除代码前的行号
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4077842.html
Copyright © 2011-2022 走看看