1. 程式人生 > >Jenkins基本結構和功能

Jenkins基本結構和功能

啟動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