docker利用WebHook實(shí)現(xiàn)持續(xù)集成
研發(fā)小伙伴可能對(duì)下列操作步驟會(huì)深有體會(huì)
寫代碼--》提交代碼--》打包--》發(fā)布
在項(xiàng)目調(diào)試測試階段,可能經(jīng)常需要重復(fù)上面的步驟,以便將最新代碼部署到特定環(huán)境供測試人員或其他人員使用
CI即持續(xù)集成的提出及各種解決方案,減輕了很多最初簡單但繁瑣的工作
本文將通過提交代碼到git,然后通過webHook觸發(fā)jenkins打包并發(fā)布到相應(yīng)容器中,開發(fā)人員只需提交代碼,后續(xù)打包發(fā)布都自動(dòng)實(shí)現(xiàn)
git ?。何沂褂玫拇a云 我的主頁為: https://gitee.com/xiaochangwei
jenkins:Jenkins ver. 2.89.2
tomcat 8 jdk8 maven3.5
[root@iZnz7e74o4ag3oZ webapps]# java -version java version "1.8.0_121" Java(TM) SE Runtime Environment (build 1.8.0_121-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode) [root@iZnz7e74o4ag3oZ webapps]# mvn -version Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00) Maven home: /usr/local/src/maven Java version: 1.8.0_121, vendor: Oracle Corporation Java home: /usr/local/src/jdk/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "3.10.0-693.2.2.el7.x86_64", arch: "amd64", family: "unix" [root@iZnz7e74o4ag3oZ webapps]#
配置好環(huán)境后,在tomcat中運(yùn)行jenkins,并設(shè)置好用戶名密碼確保能正常登錄使用
操作步驟:
1.安裝插件 Generic Webhook Trigger Plugin、Deploy to container Plugin、Git plugin 由于這里暫時(shí)未發(fā)布到docker中,通過Deploy to container Plugin發(fā)布到tomcat中的
2.上傳maven項(xiàng)目到https://gitee.com
3.jenkins中新建項(xiàng)目ci
3.1 配置提交的代碼地址,Add有權(quán)限的用戶名和密碼(我的是私有項(xiàng)目),指定分支
3.2:配置maven命令以及發(fā)布到的tomcat
注意:發(fā)布項(xiàng)目的tomcat需要設(shè)置用戶信息,即修改conf下的user.xml 具體可以參考我很早之前寫的 http://www.cnblogs.com/xiaochangwei/p/4952644.html
這樣就可以手動(dòng)構(gòu)建項(xiàng)目了,請(qǐng)確保手動(dòng)構(gòu)建能成功后再進(jìn)行下列操作
遇見的問題及解決方案:
如果構(gòu)建時(shí)提示權(quán)限不對(duì)
生成公鑰: ssh-keygen -t rsa 一直回車直結(jié)束
cat ~/.ssh/id_rsa.pub 將內(nèi)容增加到碼云上
如果還提示沒權(quán)限
手動(dòng)先在jenkins運(yùn)行的機(jī)器上clone一次代碼 如 git clone git@gitee.com:xiaochangwei/ci-demo.git 注意輸入yes
然后再看.ssh目錄,多了一個(gè)known_hosts 里面有碼云了,然后再手動(dòng)構(gòu)建,應(yīng)該就能成功了
至此,手動(dòng)打包并部署就已經(jīng)實(shí)現(xiàn)了
------------------------------自動(dòng)部署 --------------------------------------------------
1. 安裝上述插件后勾選Generic Webhook Trigger 增加post參數(shù) ref expression值為 $.ref 注意有個(gè)點(diǎn)
增加optional filter
其實(shí)上面這部分不用設(shè)置也行,尤其是只會(huì)在jenkins中部署一個(gè)項(xiàng)目的一個(gè)分支時(shí),只需要勾選上Generic Webhook Trigger就可以的
2. 在碼云中增加hook:http://USER ID:API TOKEN@jenkins部署的地址:端口號(hào)/jenkins/generic-webhook-trigger/invoke (紅色這部分不要變)
(這里不用api token 直接用jenkins的登錄密碼也可以, api token查看地址為:jenkins-用戶-點(diǎn)擊用戶名-api token)
同時(shí)勾選push或者其他你認(rèn)為需要觸發(fā)部署的事件
提交后點(diǎn)擊測試,如果返回ok,則表示成功,切換到j(luò)enkins,查看項(xiàng)目是否能夠自動(dòng)部署
我自己寫了一個(gè)接口用來獲取點(diǎn)擊測試后,到底請(qǐng)求了些什么
格式化下內(nèi)容如下:
{ "hook_name": "push_hooks", "total_commits_count": 1, "before": "0000000000000000000000000000000000000000", "user_name": "肖哥哥", "project": { "path": "ci-demo", "git_svn_url": "svn://gitee.com/xiaochangwei/ci-demo", "path_with_namespace": "xiaochangwei/ci-demo", "name": "ci-demo", "namespace": "xiaochangwei", "default_branch": "master", "git_http_url": "https://gitee.com/xiaochangwei/ci-demo.git", "name_with_namespace": "肖哥哥/ci-demo", "url": "https://gitee.com/xiaochangwei/ci-demo", "git_ssh_url": "git@gitee.com:xiaochangwei/ci-demo.git" }, "repository": { "name": "ci-demo", "description": "", "url": "https://gitee.com/xiaochangwei/ci-demo.git", "homepage": "https://gitee.com/xiaochangwei/ci-demo" }, "commits_more_than_ten": false, "ref": "refs/heads/master", "password": "", "commits": [{ "author": { "name": "肖哥哥", "time": "2015-11-06T13:21:07+08:00", "email": "317409898@qq.com" }, "id": "ec7159240a346fa5988913aa3057b902a4acb126", "message": "A Test For WebHooks", "url": "https://gitee.com/xiaochangwei/ci-demo/commit/ec7159240a346fa5988913aa3057b902a4acb126", "timestamp": "2015-11-06T13:21:07+08:00" }], "after": "ec7159240a346fa5988913aa3057b902a4acb126", "user": { "name": "肖哥哥", "id": 372286, "time": "2018-01-11T12:38:38+08:00", "user": "xiaochangwei", "email": "317409898@qq.com", "url": "https://gitee.com/xiaochangwei" } }
其實(shí)多建立幾個(gè)項(xiàng)目然后獲取信息后會(huì)發(fā)現(xiàn),不同項(xiàng)目間存在差異的就是project.git_ssh_url 和分支 ref
至此,單個(gè)項(xiàng)目的自動(dòng)部署就完了
如果你在jenkins中有多個(gè)項(xiàng)目,你觸發(fā)其中的一個(gè)webHook你就會(huì)發(fā)現(xiàn),所有的項(xiàng)目都在構(gòu)建?
這就有點(diǎn)不科學(xué)了,本來只想構(gòu)建A項(xiàng)目,結(jié)果Abc項(xiàng)目都構(gòu)建了? 要解決這個(gè)就需要用到上面提到的post param 和 filer 了
解析觸發(fā)自動(dòng)構(gòu)建的請(qǐng)求參數(shù),ref和project.git_ssh_url是差異性的東西,那就根據(jù)兩個(gè)來區(qū)分項(xiàng)目
有這上面的兩個(gè)post參數(shù)還不行,因?yàn)橹皇谦@取到了對(duì)應(yīng)的參數(shù)值
其實(shí)要想?yún)^(qū)分不同的項(xiàng)目也簡單,只要獲取到的ref還有url 和需要構(gòu)建的項(xiàng)目分支和 git地址相同就構(gòu)建,否則就不構(gòu)建
optional filter提供的方式就是最簡單的正則匹配
Expression設(shè)置為 ^(refs/heads/master)_(git@gitee.com:xiaochangwei/ci-demo.git)$ 注意修改為自己的項(xiàng)目地址
Text 設(shè)置為 $ref_$project.git_ssh_url
保存,再次構(gòu)建,是不是只觸發(fā)了一個(gè)項(xiàng)目了,而不是所有項(xiàng)目了
測試:
1.提交代碼, 提交代碼后看到j(luò)enkins就自動(dòng)編譯打包了
2.查看編譯日志:jenkins收到了webhook請(qǐng)求,并且拉取了代碼,提交信息和我們提交代碼時(shí)輸入信息一致
有同學(xué)質(zhì)疑我這里為啥失敗了,是不是自動(dòng)構(gòu)建不可用,不是哈, 我這里是用的阿里服務(wù)器,內(nèi)存不夠?qū)е铝俗詣?dòng)發(fā)布失敗
完整編譯并正確部署的日志如下:
Generic Cause Building in workspace /root/.jenkins/workspace/ci GenericWebhookEnvironmentContributor Received: {"before":"0000000000000000000000000000000000000000","after":"ec7159240a346fa5988913aa3057b902a4acb126","ref":"refs/heads/master","user_name":"\u8096\u54e5\u54e5","user":{"id":372286,"email":"317409898@qq.com","name":"\u8096\u54e5\u54e5","user":"xiaochangwei","url":"https://gitee.com/xiaochangwei","time":"2018-01-12T08:56:18+08:00"},"repository":{"name":"ci-demo","url":"https://gitee.com/xiaochangwei/ci-demo.git","description":"","homepage":"https://gitee.com/xiaochangwei/ci-demo"},"commits":[{"id":"ec7159240a346fa5988913aa3057b902a4acb126","message":"A Test For WebHooks","timestamp":"2015-11-06T13:21:07+08:00","url":"https://gitee.com/xiaochangwei/ci-demo/commit/ec7159240a346fa5988913aa3057b902a4acb126","author":{"name":"\u8096\u54e5\u54e5","email":"317409898@qq.com","time":"2015-11-06T13:21:07+08:00"}}],"project":{"name":"ci-demo","path":"ci-demo","url":"https://gitee.com/xiaochangwei/ci-demo","git_ssh_url":"git@gitee.com:xiaochangwei/ci-demo.git","git_http_url":"https://gitee.com/xiaochangwei/ci-demo.git","git_svn_url":"svn://gitee.com/xiaochangwei/ci-demo","namespace":"xiaochangwei","name_with_namespace":"\u8096\u54e5\u54e5/ci-demo","path_with_namespace":"xiaochangwei/ci-demo","default_branch":"master"},"total_commits_count":1,"commits_more_than_ten":false,"enterprise":null,"hook_name":"push_hooks","password":""} Contributing variables: ref = refs/heads/master project.git_ssh_url = git@gitee.com:xiaochangwei/ci-demo.git > git rev-parse --is-inside-work-tree # timeout=10 Fetching changes from the remote Git repository > git config remote.origin.url git@gitee.com:xiaochangwei/ci-demo.git # timeout=10 Fetching upstream changes from git@gitee.com:xiaochangwei/ci-demo.git > git --version # timeout=10 using GIT_ASKPASS to set credentials > git fetch --tags --progress git@gitee.com:xiaochangwei/ci-demo.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10 Checking out Revision 2eca30803759e021f658c92c136aa72dc026c3be (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 2eca30803759e021f658c92c136aa72dc026c3be Commit message: "test auto package and deploy" > git rev-list --no-walk 2eca30803759e021f658c92c136aa72dc026c3be # timeout=10 Parsing POMs Established TCP socket on 44276 [ci] $ /usr/local/src/jdk/bin/java -cp /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven35-agent-1.12-alpha-1.jar:/usr/local/src/maven/boot/plexus-classworlds-2.5.2.jar:/usr/local/src/maven/conf/logging jenkins.maven3.agent.Maven35Main /usr/local/src/maven /usr/local/src/tomcat/webapps/jenkins/WEB-INF/lib/remoting-3.14.jar /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven35-interceptor-1.12-alpha-1.jar /root/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.12-alpha-1.jar 44276 <===[JENKINS REMOTING CAPACITY]===>channel started Executing Maven: -B -f /root/.jenkins/workspace/ci/pom.xml clean package -Dmaven.test.skip=true [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building docker-demo 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ ci-demo --- [INFO] Deleting /root/.jenkins/workspace/ci/target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ci-demo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ ci-demo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 6 source files to /root/.jenkins/workspace/ci/target/classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ci-demo --- [INFO] Not copying test resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ ci-demo --- [INFO] Not compiling test sources [INFO] [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ ci-demo --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-war-plugin:2.6:war (default-war) @ ci-demo --- [INFO] Packaging webapp [INFO] Assembling webapp [ci-demo] in [/root/.jenkins/workspace/ci/target/ci-demo] [INFO] Processing war project [INFO] Webapp assembled in [298 msecs] [INFO] Building war: /root/.jenkins/workspace/ci/target/ci-demo.war [INFO] [INFO] --- spring-boot-maven-plugin:1.5.9.RELEASE:repackage (default) @ ci-demo --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 18.729 s [INFO] Finished at: 2018-01-12T08:56:46+08:00 [INFO] Final Memory: 30M/72M [INFO] ------------------------------------------------------------------------ Waiting for Jenkins to finish collecting data [JENKINS] Archiving /root/.jenkins/workspace/ci/pom.xml to com.xiao/ci-demo/0.0.1-SNAPSHOT/ci-demo-0.0.1-SNAPSHOT.pom [JENKINS] Archiving /root/.jenkins/workspace/ci/target/ci-demo.war to com.xiao/ci-demo/0.0.1-SNAPSHOT/ci-demo-0.0.1-SNAPSHOT.war channel stopped Deploying /root/.jenkins/workspace/ci/target/ci-demo.war to container Tomcat 8.x Remote with context /ci Redeploying [/root/.jenkins/workspace/ci/target/ci-demo.war] Undeploying [/root/.jenkins/workspace/ci/target/ci-demo.war] Deploying [/root/.jenkins/workspace/ci/target/ci-demo.war] Finished: SUCCESS
查看效果
總結(jié)
以上所述是小編給大家介紹的docker利用WebHook實(shí)現(xiàn)持續(xù)集成,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解Docker數(shù)據(jù)管理(數(shù)據(jù)卷&數(shù)據(jù)卷容器)
容器中管理數(shù)據(jù)主要有兩種方式,這篇文章主要介紹了詳解Docker數(shù)據(jù)管理(數(shù)據(jù)卷&數(shù)據(jù)卷容器) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Docker存儲(chǔ)目錄問題以及如何修改Docker默認(rèn)存儲(chǔ)位置
在Docker中,默認(rèn)情況下數(shù)據(jù)存儲(chǔ)路徑為/var/lib/docker,隨著容器和鏡像數(shù)量的增加,這可能會(huì)占用大量磁盤空間,這篇文章主要給大家介紹了關(guān)于Docker存儲(chǔ)目錄問題以及如何修改Docker默認(rèn)存儲(chǔ)位置的相關(guān)資料,需要的朋友可以參考下2024-08-08基于jib-maven-plugin插件快速構(gòu)建微服務(wù)docker鏡像的方法
這篇文章主要介紹了基于jib-maven-plugin快速構(gòu)建微服務(wù)docker鏡像的方法,對(duì)于?Java?程序來說使用?jib-maven-plugin?插件來構(gòu)建鏡像還是非常方便的,本文通過實(shí)例代碼詳細(xì)講解,需要的朋友可以參考下2023-02-02docker部署zookeeper集群方式(單主機(jī)、多主機(jī))
這篇文章主要介紹了docker部署zookeeper集群方式(單主機(jī)、多主機(jī)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04Docker安裝MySQL并使用Navicat連接的使用示例
在Docker里運(yùn)行MySQL的方式還是很方便的,本文主要介紹了Docker安裝MySQL并使用Navicat連接的使用示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10解決Docker中的error during connect異常情況
這篇文章主要介紹了解決Docker中的error during connect異常情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11