jenkins+Gitlab+maven+tomcat realizes automatic integration, packaging and deployment jenkins+Gitlab+maven+tomcat realizes automatic integration, packaging and deployment

Go to https://www.cnblogs.com/lidong94/p/7427923.html

Copy it first, and then you can write your own blog after you go back to the experiment.

jenkins+Gitlab+maven+tomcat realizes automatic integration, packaging and deployment

I. Introduction

First of all, out of improving my technical level and thinking about what I can do to improve work efficiency, the online work is boring and repetitive (manual compilation, packaging, package transfer, restart), so I thought of jenkins, jenkins continuous integration is nothing new There are a lot of articles on the Internet. As the saying goes, there are a lot of articles in the world, basically three or five articles. I believe that if people who have not been in contact with them before have two or three days, they will not be able to get it out, especially as an operation and maintenance post. I still don’t understand some things about development. I really don’t understand the concepts of continuous integration, continuous construction and continuous deployment before building. Maybe I am stupid. Thinking of this character, the purpose of writing this is not why, I hope that the latecomers will not step on the pit and make a record for themselves.

2. Environment

1. Paste a picture I saw on the Internet that I think is easy to understand

2. Server environment

A jenkins server: 192.168.4.117

A gitlab server: 192.168.4.81

One test server: 192.168.4.91

3. Environment Construction

1. gitlab installation: Since the blog about gitlab installation has been written before, I will not repeat it here, and paste the address: http://www.cnblogs.com/lidong94/p/7161717.html

2. Jenkins installation

(1) The installation can use the war package or the rpm package, but personally think that the war package is easy to use and convenient, first download the war package of jenkins, address: http://mirrors.jenkins.io/war-stable/2.32. 3/

(2) To install jdk, it is recommended to use a version above 1.8. After installing jdk, execute: vim /etc/profile Add the following code at the end of this file:

export JAVA_HOME=/usr/local/jdk1.8.0_131
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH

 (3) Then execute the following command. This is to use the default port 8080, or you can modify it to use other ports. Of course, this war package can also be started in tomcat, and the way to start tomcat will not be introduced here.

java -jar jenkins.war

(4) Jenkins has been successfully installed here, and can be accessed through the browser: http://192.168.4.117:8080

Four, jenkins configuration

(1) This page will appear when you visit for the first time

The path marked in red by cat, paste the content into the text box, click continue, after a minute, you will enter the following page

Click on the first image to install the plugin. PS: The difference between these two pictures is that one is a custom installation plug-in, and the other is a plug-in that you choose to install by yourself. It is recommended to choose the first one, and which ones to use later are installed by yourself.

Wait for about ten minutes for the plug-in installation to complete. After the plug-in installation is complete, the following figure appears

Create the first user of jenkins, then click save and finish

Click to start using jenkins

This is the main interface of jenkins, and then start the journey of jenkins

Five, jenkins interface configuration

I believe that many people do not understand what to do after reaching this step. I have built here four or five times, but every time I create a new project, I am ignorant. In that sentence, before you do anything, you must first understand what you want. Well, without further ado, our original intention is to realize that as long as you develop and submit code to gitlab, jenkins will automatically detect and automatically build (merge, package), after the construction is completed, transfer the packaged package (war or jar package) to the webapps directory of tomcat through the jenkins plug-in, and then restart tomcat to realize automatic packaging and deployment.

(1) First, we need to configure the global configuration of jenkins, including (jdk, maven)

Click System Management-----global tool configuration

After entering, the picture is as follows

The maven configuration is default, the jdk configuration alias can be written casually, java_home is the home directory of jdk, if you don't know, you can use java -version to query

The git settings are shown in the figure above. Next, configure maven. You can install maven automatically here or download and install it yourself, but it is recommended to download and install it yourself. Download address: wget http://mirrors.cnnic.cn/apache/maven/ maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz is downloaded and decompressed, you don't need too much operation, and then use mvn -v to check whether the installation is successful and the installation information, my installation information Remember this path as shown in the figure, this path needs to be configured in the maven home in the figure below

 

 

 The above configurations are all in the global tool configuration, click save after configuration

(2) Next, configure the ssh connection information, first install the ssh plugin: publish over ssh

Click System Management - Manage Plugins

After clicking in, as shown in the figure below, click the optional plug-in, then enter publish over ssh in the filter text box, select the previous check box and click to install directly

 

 After the installation is completed, as shown in the figure below, check and restart jenkins after installation, and this plugin will take effect. Since my plugin has been installed, this picture is just an example

After the installation is complete, configure ssh, which is the test server ssh information to be uploaded.

First, we need to generate a secret key on the jenkins server, and then use the secret key to perform password-free login

Execute ssh-keygen -t rsa on the jenkins server, and you can press Enter all the way. If the password is empty, the two files id.rsa (private key) and id are generated under /root/.ssh/. rsa.pub (public key), we need to write the content of the public key to the /root/.ssh/authorized_keys file of the test server (on the tomcat server), if not, create a new one, and then we start to configure

Click System Management---System Settings

After entering, find the Publish over SSH item, as shown below

 

Detailed explanation: Passphrase refers to the password set when the secret key is generated. Since we all came down with the carriage return, we do not need to write it here.

path to key refers to the location of the private key /root/.ssh/id.rsa

key refers to the content of the private key path to key and key can choose one of them

 name refers to the name you want to remote to tomcat, you can write it casually, in order to distinguish later, so I wrote a test server

hostname refers to the ip of the test server

username is the user of the test server

The remote directory refers to the directory to be placed on the test server, where the tomcat deployment directory is written

Click Advanced in the lower left corner of the first picture to modify the port, click test configuration to test the connection, the following picture shows that the test is successful, indicating that we can now use jenkins to connect to the server

 

 (4) Now jenkins can communicate with the remote server, but it has not yet communicated with gitlab. Next, we configure the connection between gitlab and jenkins. First, install the gitlab related plug-ins. The installation of plug-ins has been written above.

Next, we create a new job, because only the job can be connected to gitlab first, as shown in the figure below, fill in a name such as test here, choose to build a free-style software project, and click ok

After clicking the picture below, remember to write the description. At the beginning, it was not written. When building the project, an error will be reported, and the report cannot be configured with name[ ]. Just write it here.

Then click on the source code management as shown below, we choose git, repository url to write the address of the gitlab warehouse, credentials means trust, we need to add the public key of jenkins in gitlab, fill in the public key of the jenkins server in the key in the figure below, and fill in the title jenkins, then you can see the credentials on jenkins, you can select jenkins, and then click the build triggers tab

 

After clicking the Triggers tab, as shown in the figure below, we click

After clicking the build tab, the following figure is shown: Select the maven version at the maven version and fill in the maven command in the goals of the maven name we configured at the global tool configuration. Because we want to package, fill in the clean package. If you don’t understand, you can learn the maven command, and then click Post-build actions tab

Click on the post-build tab as shown below, select send build artifacts over ssh , select the test server we configured in the name, special attention should be paid to transfers here, I have been stuck here for a long time, because the articles written on the Internet are ambiguous source files Fill in the things you want to transfer to the remote server, which can be directories, war packages, jar packages, etc. This path is equivalent to the workspace path. The workspace directory can be found in /root/.jenkins/workspace/, so there is no need to write it here. /root/.jenkins/workspace/ Because the war package or jar package we built is under /root/.jenkins/workspace/job name/target/, target/war package name.war or **/* can be written here. war,

The remove prefix refers to removing the path before the war package filled in the source files when the war package is sent to the server. If it is not removed, the war package will be built under a target target in the webapps of tomcat. Of course, this is not what we want. need

remove directory refers to the directory that is remote to the server. This is not configured here because we have configured it in the system settings. It can be called directly here.

exec command refers to the command to be executed after passing the package, and the script can also be called here.

 Then click save and you're done, so we have finished building a job. Some people may remember that gitlab has not been configured yet, good, do you still remember the url address to remember when building the job? Next, we need to set it up in gitlab Find the option of web hooks, different gitlab web hooks are in different places, mine is click integrations in the figure below

After clicking integrations as shown below, you need to fill in the url of jenkins: http://192.168.4.117:8080/project/test, and then click Add web hooks, the function of this web hooks is that jenkins detects that gitlab has code submissions will trigger jenkins to build

At this point, jenkins is automatically packaged, and the configuration is completed after uploading. Next, let's verify it. I submit the code to gitlab, and the console output is as follows,

We went to the webapps of the test server to verify the following, the jar package has been transferred to tomcat

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325260037&siteId=291194637