做互聯網應用的各位tx都已經被部署過程的繁瑣煩透了吧?

先要壓縮,然后一臺機器一臺機器的上傳,接著還要停止服務器,解壓縮,重新啟動。。。。。。

如果只有兩天臺機器,那么還可以忍受;但是如果你有七八臺機器要部署時,你的感覺一定是要瘋掉了。更何況如工程比較大的話,壓縮、上傳都要花費很多的時間,我們寶貴的時間就這樣溜走了。更可惡的是我們很多時候需要加班也是因為部署的效率太慢。

廢話太多了,還是直接進入主題吧,讓你和我們一起分享部署的樂趣吧!

 

首先說明幾個主要的ant命令:

  1. scp:一個optional task,用來上傳文件到遠程服務器上;
  2. sshexec:ssh大家很熟悉吧?這個任務就是用來執行一個ssh腳本命令,可以執行遠程服務器的命令;
  3. fileset:這個大家應該也比較熟悉,用來設置一個文件的集合,關鍵的是它的一個子元素date可以用來設定文件的日期范圍,比如<date datetime="2007.12.18 00:00:00" pattern="yyyy.MM.dd HH:mm:ss" when="after" />指定2007年12月18日以后的文件,具體的說明請參見http://ant.apache.org/manual/index.html

主要就是這幾個命令,很簡單吧。

接著我們分析一下我們部署的一般流程吧。

  1. 我們要先把本地的文件壓縮到一個壓縮文件中,實現增量部署的關鍵也是在這里(感謝付成睿的幫助)。最好的方式是每次只壓縮最近修改的內容,而那些沒有修改的內容沒有必要再次上傳。壓縮一個時間點以后的文件可以通過date來實現,不過我們需要自動的記錄上一次部署的時間。現在有兩種方式可以做這件事,一種是付成睿的,比較簡單,但會修改配置文件;另外一種就是我的方式,也就是將日期保存到一個文件中,如果沒有文件就認為是全部部署,并創建文件。
    具體的代碼如下:
     <target name="appZipModified">
      <!-- 判斷文件是否存在 -->
      <condition property="local.app.timestamp.present">
       <available file="${app.zip.timestamp.file}" type="file" />
      </condition>
      <!-- 如果文件不存在則創建文件 -->
      <antcall target="mkStampFile">
       <param name="zipFilePresent" value="${local.app.timestamp.present}" />
       <param name="newZipFile" value="${app.zip.timestamp.file}" />
      </antcall>
      <!-- 從文件中獲取上次部署的時間 -->
      <echo>Load the old timestamp from the disk file</echo>
      <loadfile property="old.zip.timestamp" srcFile="${app.zip.timestamp.file}" />
      <!-- 獲取當前時間 -->
      <tstamp>
       <format property="this.zip.timestamp" pattern="${ts.pattern}" />
      </tstamp>
      <echo>zip the new modified files &amp; folders that after ${old.zip.timestamp} to ${appzip} </echo>
      <!-- 先刪除上次的zip文件,這樣保證上次的壓縮的文件不會再次上傳 -->
      <delete file="${appzip}" />
      <!-- 執行壓縮操作 -->
      <zip destfile="${appzip}">
       <fileset dir="../WebRoot">
        <include name="**/*" />
        <!-- 這個語句是關鍵,只壓縮old.zip.timestamp以后修改的文件 -->
        <date datetime="${old.zip.timestamp}" pattern="${ts.pattern}" when="after" />
       </fileset>
      </zip>
      <echo>Replace the old timestamp with the new timestamp</echo>
      <!-- 最后將當前的時間更新到文件中 -->
      <replace file="${app.zip.timestamp.file}" token="${old.zip.timestamp}" value="${this.zip.timestamp}" />
     </target>
     <!-- 創建文件的操作,unless的含義是zipFilePresent為false時才執行,與之對應的是if -->
     <target name="mkStampFile" unless="zipFilePresent">
      <echo>Create txt file to store timestamp</echo>
      <!-- 創建一個文件 -->
      <touch file="${newZipFile}" datetime="12/19/2007 21:20 pm" />
      <!-- 應用正則表達式的replace命令,寫入一個很早的時間(正則真是太神奇了!) -->
      <replaceregexp file="${newZipFile}" match=".*" replace="2000.01.01 00:00:00" byline="true" />
     </target>

    這種方式還是比較復雜的,使用付成睿的方法則更簡單。只是需要在properties文件中增加last.zip.timestamp的設置。
     <target name="zipModified">
      <echo>zip modified files</echo>
      <echo>get current time stamp</echo>
      <tstamp>
       <format property="this.zip.timestamp" pattern="${ts.pattern}" />
      </tstamp>
      <echo>current time stamp: ${this.zip.timestamp}</echo>
      <echo>zip modified files aflter ${last.zip.timestamp}</echo>
      <zip destfile="${local.testZip}">
       <fileset dir="./test">
        <date datetime="${last.zip.timestamp}" pattern="${ts.pattern}" when="after" />
       </fileset>
      </zip>
      <echo>save this zip time stamp</echo>
      <replace file="build.properties" token="${last.zip.timestamp}" value="${this.zip.timestamp}" />
     </target>
  2. 有了zip文件后下面就需要把文件上傳到服務器上,方法如下:
     <!-- 上傳zip壓縮文件 -->
     <target name="uploadZipApp" description="upload ziped app file to remote server">
      <echo>upload ziped app file to remote server</echo>
      <scp file="${appzip}" todir="user
    @${host}:/path/test.zip" password="${pass}" trust="yes" />
     </target>
    很簡單吧?
  3. 上傳完后就是在服務器的操作了(例子用的是unix),包括解壓縮、停止服務器、重新啟動等,主要還是如何遠程調用服務器命令。
    下面的例子是將文件解壓縮,最關鍵的是sshexec命令的用法。
      <!-- 解壓縮備份文件 -->
     <target name="unZipBackupResource" description="decompress the backup tar file back to the file system">
      <echo>decompress the backup tar file back to the file system</echo>
      <sshexec host="${host}" username="user" command="tar xvf test.tar" password="${pass}" trust="yes" />
     </target>
    做其他的操作只要把上面例子中的黑體字部分去掉就可以了。
  4. 最后最好將一個完整的部署流程封裝到一個target中,這樣部署一臺服務器只要輸入相應的密碼就可以了。
  5. 而現在部署時你要做的操作只是在outline模式下在一個部署的target上點擊右鍵,然后run就可以了。

這就是完整的流程。

如果部署的是jsp文件或者靜態文件,那就更簡單了,直接上傳解壓縮就可以了。

其他tz有什么更好的方法也一起分享啊,我在這算是拋磚引玉了。。。。。。