Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Docker uses WebHook to realize continuous integration

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Research and development partners may have a deep understanding of the following steps

Write code-- "submit code--" package-- "release"

During the debugging and testing phase of a project, you may often need to repeat the above steps to deploy the latest code to a specific environment for use by testers or others

CI, the proposal of continuous integration and various solutions, reduces a lot of the initial simple but tedious work.

In this article, we will submit the code to git, then trigger the jenkins package through webHook and publish it to the corresponding container. The developer only needs to submit the code, and the subsequent packaging and release will be realized automatically.

Git: my home page is: https://gitee.com/xiaochangwei

Jenkins:Jenkins ver. 2.89.2

Tomcat 8 jdk8 maven3.5

[root@iZnz7e74o4ag3oZ webapps] # java-versionjava version "1.8.0mm 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-versionApache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426 2017-04-04T03:39:06+08:00) Maven home: / usr/local/src/mavenJava version: 1.8.0mm 121, vendor: Oracle CorporationJava home: / usr/local/src/jdk/jreDefault locale: en_US, platform encoding: UTF-8OS name: "linux", version: "3.10.0-693.2.2.el7.x86_64", arch: "amd64", family: "unix" [root@iZnz7e74o4ag3oZ webapps] #

After configuring the environment, run jenkins in tomcat and set the user name and password to ensure that you can log in properly

Procedure:

1. Install plug-ins Generic Webhook Trigger Plugin, Deploy to container Plugin, Git plugin because it has not been released to docker at the moment, and released to tomcat through Deploy to container Plugin.

two。 Upload maven project to https://gitee.com

New project ci in 3.jenkins

3.1 configure the submitted code address, the user name and password that Add has permission (mine is a private project), specify the branch

3.2Configuring maven commands and tomcat published to

Note: to publish the tomcat of the project, you need to set the user information, that is, to modify the user.xml under conf, please refer to the http://www.cnblogs.com/xiaochangwei/p/4952644.html I wrote a long time ago.

In this way, you can build the project manually, make sure that the manual build is successful before doing the following

Problems encountered and solutions:

If you are prompted to have incorrect permissions when building

Generate the public key: ssh-keygen-t rsa all the way back to the end

Cat ~ / .ssh/id_rsa.pub adds the content to the cloud

If it is also prompted that there is no permission

Manually clone the code such as git clone git@gitee.com:xiaochangwei/ci-demo.git on the machine where jenkins is running. Enter yes.

Then look at the .ssh directory, there is a code cloud in the known_hosts, and then build it manually. It should be successful.

At this point, manual packaging and deployment has been implemented

-automatic deployment-

1. After installing the above plug-in, check Generic Webhook Trigger to increase the post parameter ref expression value to $. Ref note that there is a point

Add optional filter

In fact, the above part does not need to be set, especially if only one branch of a project is deployed in jenkins, you only need to check Generic Webhook Trigger.

two。 Add the hook: http://USER ID:API TOKEN@jenkins deployment address to the code cloud: Port number / jenkins/generic-webhook-trigger/invoke (do not change the red part)

(it is OK to use the login password of jenkins directly without api token here. The api token view address is: jenkins- user-Click user name-api token)

Also check push or other events that you think need to trigger deployment

Click Test after submission. If ok is returned, it is successful. Switch to jenkins to check whether the project can be deployed automatically.

I wrote an interface to get what I requested after the click test.

The formatted content is as follows:

{"hook_name": "push_hooks", "total_commits_count": 1, "before": "000000000000000000000000000000000000000000000000000000000000000000", "user_name": "Brother Xiao", "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 ":" Brother Xiao / 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": "Brother Xiao", "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": "Brother Xiao", "id": 372286, "time": "2018-01-11T12:38:38+08:00", "user": "xiaochangwei", "email": "317409898@qq.com", "url": "https://gitee.com/xiaochangwei"}

In fact, after setting up a few more projects and getting the information, you will find that the differences between different projects are project.git_ssh_url and branch ref.

At this point, the automatic deployment of a single project is over.

If you have multiple projects in jenkins and you trigger one of the webHook, you will find that all the projects are being built?

This is a little unscientific. I only wanted to build Project A, but as a result, all Abc projects have been built. To solve this, you need to use the post param and filer mentioned above.

Parsing the request parameters that trigger automatic construction. Ref and project.git_ssh_url are different things, so distinguish the project according to the two.

It is not good to have the above two post parameters, because only the corresponding parameter values are obtained.

In fact, it is easy to distinguish between different projects, as long as the ref and url are obtained and the project branches to be built are the same as the git address, otherwise they will not be built.

The way that optional filter provides is the simplest regular matching.

Set Expression to ^ (refs/heads/master) _ (git@gitee.com:xiaochangwei/ci-demo.git) $and change it to your own project address.

Text is set to $ref_$project.git_ssh_url

Save and build again, does it trigger only one project, not all projects?

Test:

1. Submit the code and automatically compile and package it when you see jenkins after submitting the code

two。 Check the compilation log: jenkins received the webhook request and pulled the code. The submission information is the same as the input information when we submitted the code.

Some students questioned why I failed here, whether the automatic build is not available, is not ha, I am using the Ali server, not enough memory led to automatic release failure

The logs that are fully compiled and deployed correctly are as follows:

Generic CauseBuilding in workspace/ root/.jenkins/workspace/ciGenericWebhookEnvironmentContributor Received: {"before": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 "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=10Fetching changes from the remote Git repository > git config remote.origin.url git@gitee.com:xiaochangwei/ci-demo.git # timeout=10Fetching upstream changes from git@gitee.com:xiaochangwei/ci-demo.git > git-- version # timeout=10using 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=10Checking out Revision 2eca30803759e021f658c92c136aa72dc026c3be (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout-f 2eca30803759e021f658c92c136aa72dc026c3beCommit message: "test auto package and deploy" > git rev-list-- no-walk 2eca30803759e021f658c92c136aa72dc026c3be # timeout=10Parsing POMsEstablished 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 44276channel startedExecuting Maven:-Bf / 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.warchannel stoppedDeploying / 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

View the effect

Summary

The above is the editor to introduce to you the docker to achieve continuous integration using WebHook, I hope to help you, if you have any questions, please leave me a message, the editor will reply to you in time. Thank you very much for your support to the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report