研发小伙伴可能对下列操作步骤会深有体会
写代码--》提交代码--》打包--》发布
在项目调试测试阶段,可能经常需要重复上面的步骤,以便将最新代码部署到特定环境供测试人员或其他人员使用
CI即持续集成的提出及各种解决方案,减轻了很多最初简单但繁琐的工作
本文将通过提交代码到git,然后通过webHook触发jenkins打包并发布到相应容器中,开发人员只需提交代码,后续打包发布都自动实现
git :我使用的码云 我的主页为: 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]#
配置好环境后,在tomcat中运行jenkins,并设置好用户名密码确保能正常登录使用
操作步骤:
1.安装插件 Generic Webhook Trigger Plugin、Deploy to container Plugin、Git plugin 由于这里暂时未发布到docker中,通过Deploy to container Plugin发布到tomcat中的
2.上传maven项目到https://gitee.com
3.jenkins中新建项目ci
3.1 配置提交的代码地址,Add有权限的用户名和密码(我的是私有项目),指定分支
3.2:配置maven命令以及发布到的tomcat
注意:发布项目的tomcat需要设置用户信息,即修改conf下的user.xml 具体可以参考我很早之前写的 http://www.cnblogs.com/xiaochangwei/p/4952644.html
这样就可以手动构建项目了,请确保手动构建能成功后再进行下列操作
遇见的问题及解决方案:
如果构建时提示权限不对
生成公钥: ssh-keygen -t rsa 一直回车直结束
cat ~/.ssh/id_rsa.pub 将内容增加到码云上
如果还提示没权限
手动先在jenkins运行的机器上clone一次代码 如 git clone git@gitee.com:xiaochangwei/ci-demo.git 注意输入yes
然后再看.ssh目录,多了一个known_hosts 里面有码云了,然后再手动构建,应该就能成功了
至此,手动打包并部署就已经实现了
------------------------------自动部署 --------------------------------------------------
1. 安装上述插件后勾选Generic Webhook Trigger 增加post参数 ref expression值为 $.ref 注意有个点
增加optional filter
其实上面这部分不用设置也行,尤其是只会在jenkins中部署一个项目的一个分支时,只需要勾选上Generic Webhook Trigger就可以的
2. 在码云中增加hook:http://USER ID:API TOKEN@jenkins部署的地址:端口号/jenkins/generic-webhook-trigger/invoke (红色这部分不要变)
(这里不用api token 直接用jenkins的登录密码也可以, api token查看地址为:jenkins-用户-点击用户名-api token)
同时勾选push或者其他你认为需要触发部署的事件
提交后点击测试,如果返回ok,则表示成功,切换到jenkins,查看项目是否能够自动部署
我自己写了一个接口用来获取点击测试后,到底请求了些什么
格式化下内容如下:
{ "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" } }
其实多建立几个项目然后获取信息后会发现,不同项目间存在差异的就是project.git_ssh_url 和分支 ref
至此,单个项目的自动部署就完了
如果你在jenkins中有多个项目,你触发其中的一个webHook你就会发现,所有的项目都在构建?
这就有点不科学了,本来只想构建A项目,结果Abc项目都构建了? 要解决这个就需要用到上面提到的post param 和 filer 了
解析触发自动构建的请求参数,ref和project.git_ssh_url是差异性的东西,那就根据两个来区分项目
有这上面的两个post参数还不行,因为只是获取到了对应的参数值
其实要想区分不同的项目也简单,只要获取到的ref还有url 和需要构建的项目分支和 git地址相同就构建,否则就不构建
optional filter提供的方式就是最简单的正则匹配
Expression设置为 ^(refs/heads/master)_(git@gitee.com:xiaochangwei/ci-demo.git)$ 注意修改为自己的项目地址
Text 设置为 $ref_$project.git_ssh_url
保存,再次构建,是不是只触发了一个项目了,而不是所有项目了
测试:
1.提交代码, 提交代码后看到jenkins就自动编译打包了
2.查看编译日志:jenkins收到了webhook请求,并且拉取了代码,提交信息和我们提交代码时输入信息一致
有同学质疑我这里为啥失败了,是不是自动构建不可用,不是哈, 我这里是用的阿里服务器,内存不够导致了自动发布失败
完整编译并正确部署的日志如下:
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
查看效果
总结
以上所述是小编给大家介绍的docker利用WebHook实现持续集成,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]