CruiseControl Enterprise 最佳實踐 (3) : Configuring CruiseControl the CruiseControl way
Posted on 2007-11-09 00:57 切爾斯基 閱讀(2064) 評論(0) 編輯 收藏英文原文: Configuring CruiseControl the CruiseControl way
I'm an Infrastructure Specialist at ThoughtWorks. In my role I make sure that we are building our software so it can successfully be deployed to production. In this series of blog posts I hope to pass on my top ten tips for using CruiseControl Enterprise effectively. I'm writing these with the developers or systems administrators in mind: the people who most often manage CruiseControl. However, I hope that anybody who is interested in Continuous Integration will get something from these articles.
# 3: Configuring CruiseControl the CruiseControl way : 用 CruiseControl 的方式來配置 CruiseControl.
你剛剛開始使用CruiseControl, 你用一個版本控制系統(tǒng)來管理你的代碼, 你把CruiseControl安裝在辦公室里一臺空閑的機器上.
一切都很不錯, 你的代碼改變后它能夠立刻給你反饋. 然后那臺空閑機器上的硬盤完蛋了, 于是你的這臺構(gòu)建服務器只好繼續(xù)著它以前空閑時門閂的角色.
"沒問題", 你想: "所有的代碼改變都在版本控制系統(tǒng)里, 我們能夠重新生成任何我們需要的構(gòu)件. 事實上, 我們需要的僅僅是那個配置文件
...". 是的, 那個配置文件. 那個硬盤上的配置文件再也不能工作了. 這篇blog將簡略敘述一下如何管理你的CruiseControl的配置而不必害怕失去它.
像許多工具一樣, CruiseControl將會失去作用, 如果沒有配置的話.
<?xml version="1.0"?>
<cruisecontrol>
<project name="config">
<labelincrementer defaultLabel="${project.name}-1" separator="-"/>
<listeners>
<currentbuildstatuslistener file="/var/spool/cruisecontrol/logs/${project.name}/
currentbuildstatus.txt"/>
</listeners>
<bootstrappers>
<svnbootstrapperlocalWorkingCopy="/etc/cruisecontrol"/>
</bootstrappers>
<modificationsetquietperiod="30">
<svnlocalWorkingCopy="/etc/cruisecontrol"/>
</modificationset>
<schedule interval="60">
<ant antWorkingDir="/etc/cruisecontrol" antscript="/var/spool/cruisecontrol/tools/apache-ant-1.6.5
/bin/ant" uselogger="true"/>
</schedule>
<publishers>
<artifactspublisher
file="${project.name}/build.log"
dest="logs/${project.name}"/>
</publishers>
</project>
</cruisecontrol>
這將機械的更新配置, 直到世界末日. 它簡單卻相當有效, 現(xiàn)在我們不再依賴那個能夠更改CruiseControl的家伙了.
突然之間他們就不需要代表團隊中其他人來做些瑣碎的更改了, 因為每個人都可以安全的改變配置. 如果有人確實check in了一個有問題的配置文件, 沒關系,
一切都在版本控制系統(tǒng)中. 你可以revert這次改動, 可以找到是誰做的修改, 可以試著理解為什么他們想這么改.
package org.juliansimpson;
import java.io.File;
import net.sourceforge.cruisecontrol.CruiseControlException;
import net.sourceforge.cruisecontrol.config.XMLConfigManager;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
public class ConfigValidator extends Task {
public String configFile;
public void execute() throws BuildException {
try {
File file = new File(configFile);
new XMLConfigManager(file);
} catch (CruiseControlException e) {
throw new BuildException("Invalid CruiseControl Config");
}
}
public void setConfigFile(String config) {
configFile = config;
}
}
這個validator使用CruiseControl自己內(nèi)部的一個類來校驗配置. 最好是能有一個公開的接口來做這件事--可以是一個命令行選項或者一個"官方"的Ant task. 我請求過CruiseControl Enterprise Team在以后的release中考慮一下這個問題. 這種校驗方式確實意味著你需要設置一下classpath, 以便讓你的validator能夠找到它引用的來自CruiseControl內(nèi)部的類. 但是你會發(fā)現(xiàn)它確實能夠保證目前的配置對于你正在使用的CruiseControl版本來說是有效的. 我傾向于以Ant task的方式來運行校驗. 它非常簡單, 并且每個人都能很容易的看到它做了什么. 下面是我把它放在一個簡單的Ant腳本中:
<project name="cruisevalidator" default="publish" >
<import file="build-library.xml"/>
<target name="validated-config" depends="cruise-validator.jar">
<taskdef name="validate" classname="org.juliansimpson.ConfigValidator" classpathref="main"/>
<echo message="validating ${config}" />
<validate configFile="${config}" />
</target>
<target name="publish" depends="validated-config">
<echo level="info" message="copying CruiseControl config to server" />
<copy file="${config}" todir="${cruisecontrol.dir}" failonerror="true"
description="Copy configuration to CruiseControl server" />
<echo level="info" message="forcing a reload of config on server" />
<get src="http://localhost:8000/invoke?operation=reloadConfigFile&objectname=CruiseControl+Manager%3Aid%3Dunique"
dest="${build.dir}/reload.html" />
</target>
</project>
它們合在一起像下面這樣工作: CruiseControl的BootsStrapper獲取最新的配置文件, 但是放在不同于CruiseControl安裝目錄的目錄中. 你依然還不清楚它是否是一個有效的配置文件. 然后"validated-config" target會調(diào)用ConfigValidator 這個Ant task.這將調(diào)用到CruiseControl中足夠的邏輯來確保配置是合法的,并且配置文件中涉及到的一些目錄都存在.如果通過了這一步,那么 "publish"target就會把配置拷貝到CruiseControlserver自身的目錄中覆蓋原來的文件.最后還是 "publish"target會發(fā)一個簡單的Http請求到JMX接口,來強制CruiseControl來重新加載一下配置.這將確保新配置會立即被 加載,這樣team就會知道新配置是合法的.謝謝我的同事Tim Brown的這個非凡的主意.
我不得不承認有時我會不小心弄壞XML配置文件. 這種方式對我來說工作的尤其好, 因為我有校驗的保護網(wǎng). 我對我的郵件服務器和web服務器做了相似的事情, 希望很快能有機會寫一下它們. 文中validator的源代碼和構(gòu)建腳本可以在我的網(wǎng)站上下載: http://www.juliansimpson.org/.
©Julian Simpson 2007. All rights reserved.
我想這個實踐的核心是:
1, 將CruiseControl的配置文件check in到版本控制系統(tǒng)中, 以解決意外損壞的問題
2, 使用專門的"project"來自動更新配置, 以解決每次需要有人專門登錄到build server上去更新的瓶頸問題.
3, 復用但不依賴于CruiseControl對配置文件的validation, 以同時獲得 "阻止有問題的配置文件被應用到build server的能力" 和 "迅速獲知有人check in了有問題的配置的能力"