接上回繼續,本文以我托管在bitbucket上的一個開源項目spring-boot-rest-framework做為演示,講解如何創建自動化部署。
一、創建item
點擊ok繼續,item的詳情頁面很長,我們分成一段段來講解,最上面這部分,除了名字要填寫外,其它全留空即可
然后是源代碼管理部分,這是一個git項目,先在Repository URL這里填寫上項目的git地址(即:紅字1的位置),bitbucket是需要用戶名密碼才能訪問的,所以Credentials這里要選擇相應的用戶名、密碼(即:紅字2的部分),紅字3的部分為git獲取的源代碼分支名稱,一般為master主分支,也可以改成自己希望的分支。
可能有同學會問,紅字1處的git地址怎么得到? 這個很簡單,bitbucket網站項目右上角里就有,見下圖:
另外,紅字2處對應的下拉框,首次進來時,應該是空的,可點擊右側的Add,添加用戶名,密碼,見下圖:
然后在Build的部分,點擊Add Build Step下拉框
本文中的這個項目是maven項目,所以我們選擇Invoke Maven3
然后輸入maven構建必要的參數,參考下圖:
然后保存,創建完成。
二、開始構建
保存后,系統會返回首頁,每個項目旁邊有一個小三角箭頭,選擇Build Now
稍等片刻,左側就會出現一個進度條,表示正在Building...
點擊這個進度條上的鏈接,會看到構建詳情
繼續點擊進度條前的#x的序號,會看到左側有一個Console Output
點擊output,可以實時看到構建過程中的所有輸出,內容大致如下:
SuccessConsole Output Started by user 楊俊明 Building in workspace /Users/yjmyzz/.jenkins/workspace/spring-boot-rest-framework > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url https://yangjunming@bitbucket.org/yangjunming/spring-boot-rest-framework.git # timeout=10 Fetching upstream changes from https://yangjunming@bitbucket.org/yangjunming/spring-boot-rest-framework.git > git --version # timeout=10 using .gitcredentials to set credentials > git config --local credential.username yangjunming@huijiame.com # timeout=10 > git config --local credential.helper store --file=/var/folders/sk/2f7knc1518g57382y5s4zxsr0000gn/T/git8289517448278554241.credentials # timeout=10 > git -c core.askpass=true fetch --tags --progress https://yangjunming@bitbucket.org/yangjunming/spring-boot-rest-framework.git +refs/heads/*:refs/remotes/origin/* > git config --local --remove-section credential # timeout=10 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision b9f197e3214a6af98a5b039912cf062997593116 (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f b9f197e3214a6af98a5b039912cf062997593116 > git rev-list b9f197e3214a6af98a5b039912cf062997593116 # timeout=10 Jenkins Artifactory Plugin version: 2.4.7 Artifactory integration is disabled [spring-boot-rest-framework] $ /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdK/Contents/Home/bin/java -classpath /Users/yjmyzz/app/apache-maven-3.3.3/boot/plexus-classworlds-2.5.2.jar -Dmaven.home=/Users/yjmyzz/app/apache-maven-3.3.3 -Dclassworlds.conf=/Users/yjmyzz/app/apache-maven-3.3.3/bin/m2.conf -Dmaven.multiModuleProjectDirectory=/Users/yjmyzz/.jenkins/workspace/spring-boot-rest-framework/pom.xml org.codehaus.plexus.classworlds.launcher.Launcher -f pom.xml clean package -Dmaven.test.skip=true [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building spring-boot-rest-framework 1.0.0 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ spring-boot-rest-framework --- [INFO] Deleting /Users/yjmyzz/.jenkins/workspace/spring-boot-rest-framework/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ spring-boot-rest-framework --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 2 resources [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ spring-boot-rest-framework --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 20 source files to /Users/yjmyzz/.jenkins/workspace/spring-boot-rest-framework/target/classes [WARNING] /Users/yjmyzz/.jenkins/workspace/spring-boot-rest-framework/src/main/java/spring/boot/rest/common/aop/ServiceMonitor.java: /Users/yjmyzz/.jenkins/workspace/spring-boot-rest-framework/src/main/java/spring/boot/rest/common/aop/ServiceMonitor.java uses unchecked or unsafe operations. [WARNING] /Users/yjmyzz/.jenkins/workspace/spring-boot-rest-framework/src/main/java/spring/boot/rest/common/aop/ServiceMonitor.java: Recompile with -Xlint:unchecked for details. [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ spring-boot-rest-framework --- [INFO] Not copying test resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ spring-boot-rest-framework --- [INFO] Not compiling test sources [INFO] [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ spring-boot-rest-framework --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:2.5:jar (default-jar) @ spring-boot-rest-framework --- [INFO] Building jar: /Users/yjmyzz/.jenkins/workspace/spring-boot-rest-framework/target/spring-boot-rest-framework-1.0.0.jar [INFO] [INFO] --- spring-boot-maven-plugin:1.3.0.RELEASE:repackage (default) @ spring-boot-rest-framework --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4.234 s [INFO] Finished at: 2016-01-18T23:41:28+08:00 [INFO] Final Memory: 30M/273M [INFO] ------------------------------------------------------------------------ Build step 'Invoke Maven 3' changed build result to SUCCES Finished: SUCCESS
從輸出內容可以看出構建的整個過程:先從git上拉取源碼,然后調用maven命令進行構建。只不過這一切是在UI界面完成的,以后項目的Build工作就可以交由運維甚至測試人員直接來做了,程序員只管提交代碼即可。
三、遠程分發
編譯成功只是第一步,接下來要將編譯后的文件打包上傳到遠程服務器,通常在上傳前會壓縮一下,即節省帶寬,也方便傳輸。
參考剛才添加maven參數的做法,再次點擊Add build step,只不過這次選擇Execute shell,即執行腳本,腳本內容如下:
即:進入工作目錄下的target目錄,然后將編譯后的jar包壓縮成.tar.gz包,大家可以參考圖中的命令自行調整。
接下來要上傳到遠程服務器,理論上講,應該繼續在上面的腳本里追加一行scp deploy.tar.gz user@remote_server:/remote_path之類的命令,大家可以嘗試一下,最后執行時多半會報沒有權限,原因是jenkins當前運行的賬號通常不是root賬號,權限不夠。更好的做法是利用scp插件,以證書方式連接到遠程服務器(具體配置請參考上節SSH相關的部分),參考下圖:
在post-build actions下拉框里,選擇Publish articifacts to SCP Repository
然后參考下圖填寫相應參數
然后保存,再次build,觀察Console Output
... Build step 'Invoke Maven 3' changed build result to SUCCESS [spring-boot-rest-framework] $ /bin/bash -xe /var/folders/sk/2f7knc1518g57382y5s4zxsr0000gn/T/hudson5867298139164607521.sh + cd /Users/yjmyzz/.jenkins/workspace/spring-boot-rest-framework + cd target + tar zcf deploy.tar.gz spring-boot-rest-framework-1.0.0.jar [SCP] Connecting to deploy [SCP] uploading file: '/opt/deploy/deploy.tar.gz' Finished: SUCCESS
從輸出看,上傳文件已經ok
四、遠程應用重啟
最后就是遠程將上傳文件解壓,并啟動應用,我們可以事先寫一個shell腳本,放在服務器上(或者參考上一步的做法,用scp插件上傳到服務器),腳本內容參考下面這樣:
#!/bin/bash export JAVA_HOME=/opt/app/jdk1.8.0_65 echo ${JAVA_HOME} dir_base=/opt/deploy cd ${dir_base} echo 'deploying...' if [ -r 'deploy.tar.gz' ];then tar -zxvf deploy.tar.gz fi rm -rf *.gz rm -rf ${dir_base}/logs jar_name='spring-boot-rest-framework-1.0.0.jar' if [ $(pgrep -f ${jar_name} | wc -l) -gt 0 ]; then pkill -9 -f ${jar_name} fi echo ${dir_base}/${jar_name} nohup ${JAVA_HOME}/bin/java -jar ${dir_base}/${jar_name} >/dev/null 2>&1 & sleep 2 echo 'ok!'
這段腳本的大概意思是,先檢測指定目錄下有無deploy.tar.gz文件,有的話就解壓,本文中的項目是一個spring-boot的項目,解壓開后就是一個可獨立運行的jar包,然后先檢測當前進程中是否已經在運行,如果有,則先kill掉進程,最后用nohup以后臺方式運行jar包。假設該腳本文件名為deploy.sh,具備執行權限,我們可以在jenkins中通過插件遠程調用它,參考下圖的配置:
最后build下,還是觀察console output的變化:
Build step 'Invoke Maven 3' changed build result to SUCCESS [spring-boot-rest-framework] $ /bin/bash -xe /var/folders/sk/2f7knc1518g57382y5s4zxsr0000gn/T/hudson3712003549124736229.sh + cd /Users/yjmyzz/.jenkins/workspace/spring-boot-rest-framework + cd target + tar zcf deploy.tar.gz spring-boot-rest-framework-1.0.0.jar [SCP] Connecting to deploy [SCP] uploading file: '/opt/deploy/deploy.tar.gz' SSH: Connecting from host [yangjunmingdeMacBook-Pro.local] SSH: Connecting with configuration [deploy] ... SSH: EXEC: STDOUT/STDERR from command [/opt/deploy/deploy.sh] ... /opt/app/jdk1.8.0_65 deploying... spring-boot-rest-framework-1.0.0.jar tar: spring-boot-rest-framework-1.0.0.jar: time stamp 2016-01-20 22:52:15 is 143461.67416312 s in the future /opt/deploy/spring-boot-rest-framework-1.0.0.jar ok! SSH: EXEC: completed after 2,230 ms SSH: Disconnecting configuration [deploy] ... SSH: Transferred 0 file(s) Finished: SUCCESS
從第16行輸出的ok來看,遠程部署已經成功。
最后上一張圖,大致描述了整個流程:
文章列表