Jenkins

Jenkins是一個開源軟件項目,旨在提供一個開放易用的軟件平臺,使持續集成變成可能。[1]
Jenkins,之前叫做Hudson,是基於Java開發的一種持續集成工具,用於監控秩序重複的工作,包括:
1、持續的軟件版本發佈/測試項目。
2、監控外部調用執行的工作。
Jenkins搭建
下載jenkins.war
有兩種啓動方法。
首先保證系統中已經安裝了jdk,最好是jdk1.5以上。
第一種啓動方法,切換到jenkins.war存放的目錄,輸入如下命令:
$ java -jar jenkins.war
然後在瀏覽器中(推薦用火狐)輸入http://localhost:8080,localhost可以是本機的ip,也可以是計算機名。就可以打開jenkins
第二種方法是用tomcat打開
解壓tomcat到某個目錄,如/usr/local,進入tomcat下的/bin目錄,啓動tomcat
將jenkins.war文件放入tomcat下的webapps目錄下,啓動jenkins時,會自動在webapps目錄下建立jenkins目錄,所以在地址欄上需要輸入的地址於上一種方法有點不一樣:
http://localhost:8080/jenkins。

從代碼倉庫獲取代碼用SVN,做單元測試用JUNIT,做代碼檢查用PMD、FINDBUGS,代碼編譯、打包用ANT,拷貝文件到部署服務器上用SHELL,出現BUG了通知開發人員要發EMAIL,工具一多了,整合起來用時,不斷切換界面,輸入命令的複雜性可想而知,有沒有一站式的工具可以代勞呢?

有,這就是JENKINS,有了她,你就可以:通過統一的WEB界面做各種配置工作、查看工具的LOG輸出、即使是歷史的日誌查看也沒問題;通知配置排程,可以自動的獲取代碼、編譯、打包和部署,發郵件,無須人工干預。

  1. 安裝JENKINS,其實就是把一個WAR包放到容器中

  2. 安裝插件:發郵件插件Jenkins Email Extension Plugin和部署插件Deploy to container Plugin

  3. 配置JDK和郵件服務器地址

  4. 新建任務:配置SVN地址/配置構建排程@hourly/配置構建步驟,如先用ANT編譯打包再傳到服務器/配置成功或失敗後發郵件通知的地址

  5. 手動執行任務或等待時間到了後執行任務

各種插件的安裝ANT:如果在JENKINS中使用了ANT-JUNIT,由於JENKINS是直接調用操作系統的ANT命令的,所以要在LINUX下安裝ANT,配置ANT_HOME,並將BIN加入到PATH中:
ANT_HOME=/opt/ant/apache-ant-1.8.2
JAVA_HOME
=/usr/java/jdk1.6.0_29
JRE_HOME
=/usr/java/jdk1.6.0_29/jre
PATH
=$ANT_HOME/bin:$JAVA_HOME/bin:JRE_HOME/bin:$PATH
CLASSPATH
=.:$JAVA_HOME/lib/jt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
export ANT_HOME JAVA_HOME JRE_HOME PATH CLASSPATH



官網: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目錄結構非常直白,簡潔。


備份和恢復

備份和恢復非常簡單,就是簡單的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都有自己單獨的目錄,我們可以很容易的:

   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的管理界面,就可以方便的添加節點。配置節點時,需要提供節點所在的機器,登陸用戶名密碼,使用的目錄等。

但是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 都部署了唯一的Jenkins服務器。

Jenkins服務器統一負責該環境內所有組件的持續集成。也就是說,一個Jenkins服務器會有很多個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 反證我不懂。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章