1. What is Jenkins

1.1 Why do we need Jenkins

In a system development, the following steps are often required:

  1. Write the code
  2. Submit code
  3. Ask around if anyone didn’t submit code
  4. Project packaging (usually JAR packages and WAR packages)
  5. Upload the file to the Linux server
  6. If the project is running, close it
  7. Start the project you just deployed
  8. If someone suddenly raises their hand and says there’s something wrong with my code! Repeating steps 1-7 is equally painful

One might ask, is there a tool that automatically packages deployment?

Correct answer :Jenkins

Jenkins is a continuous integration tool based on Java, which can help users to better achieve continuous integration

1.2. Jenkin implementation principle

  1. Programmers submit code

  2. Version control servers (Git, SVM) integrate the code base

  3. The version control server informs Jenkins to automate the deployment

  4. Jenkins called the git/ SVN plug-in to get the source code

  5. Jenkins calls the Maven plugin to package a WAR package or jar package

  6. Jenkins invokes the command to deploy the project to the server

  7. User: test/front end


2. Jenkins builds

2.1. Jenkins environment construction (taking CentOS7.5 as an example)

  1. jdk
  2. maven/gradle
  3. git/svm

The above is the basic environment for deploying Java projects, please set up first

2.2. Installation Jenkins

  1. Download the installation package,Jenkins official website download is slow, here provides the domestic download address :Jenkins

  2. In the installation package root path, enter: nohup java -jar – Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true jenkins.war –httpPort=8080 &

    • nohup …………. & Background boot
    • Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true To turn off CSRF, you cannot turn off CSRF after Jenkins2.2, causing a project pull failure
    • Jenkins. War Downloaded the war package
    • –httpPort=8080 Specifies the Jenkins admin page port
  3. There will be a long after the initial installation password, if can forget in ~ /. Jenkins/secrets/initialAdminPassword file viewer

  4. Type http://localhost:8080 in your browser

  5. After entering the password, you are advised to click the recommended plug-in to ensure that the basic functions are available

  6. After the installation is complete, set the first administrator user and password

2.3. Jenkins initial configuration

2.3.1. Chinese Plug-in (optional)

After Linux was installed, I opened it for the first time and found that the interior was all in English

Steps of Sinicization:

  1. Click on the Manage Jenkins

  2. Click Mange Plugins

  3. Click the Advanced, set the Update Sites URL to: mirrors.tuna.tsinghua.edu.cn/jenkins/upd… Using a domestic image source to install plug-ins is fast

  4. Plug-in installation (either old or new version)

    • Install the Locale plugin, then go to Manage Jenkins -> Configure System, find Locale, type zh_CN, and check the box below

    • The installation of the new version can be Localization:Chinese(Simplified) plug-in (author’s own use,Jenkins version 2.277)

  5. Drag it to the bottom and hit Save

  6. Restart (recommended way: http://localhost:8080/restart) is really very convenient

2.3.2. Global Tool Configuration (Under System Administration)

JDK, Git, Maven

Note:

  1. If you do not have git configuration items, install the Git plug-in in the plug-in. For specific installation methods, refer to the above steps for Localization
  2. Whether the automatic installation of their own consideration, online reading other people’s advice is to use their own (their own is the best)
  3. Remember to pull it to the bottom and save it
  4. Replace your own directory configuration items
  5. Remember to replace Mrror in Maven settings. XML with Ali source
  6. Remember to configure git as the full path to the Git executable

2.3.3. Install the remote server plug-in and other plug-ins

A total of three plug-ins need to be installed (refer to the above Steps for installation):

  1. Publish Over SSH Is used to connect to the remote server
  2. Deploy to Container is used to publish packaged applications to remote servers
  3. Maven Integration is used to build Maven projects

2.4. Configure SSH for connecting to the remote server

  1. Choose System Management > System Configuration
  2. Select Publish over SSH from the bottom and fill it out as shown below

Reference:

The name Corresponding to the content
Passphrase Server password
Path to key Path to the remote server key file
Key SSH Key (see note below for details)
Name Custom server name
HostName Server IP Address
UserName Server username
Remote Directory The directory for transferring files

Note:

  1. If Publish over SSH cannot be found, it indicates that the plug-in is not installed. Install it yourself by referring to the Procedure for Chinese translation

  2. Login method:

    • For username and password login, enter Passphrase
    • If you use SSH to log in, enter Path to Key and Key
  3. The Key is in the ~/. SSH /id_rsa.pub file. If the file does not exist, run the ssh-keygen command and press Enter to automatically generate the Key.

  4. Remote Directory is where your packaged projects (JAR/WAR) are stored, so remember to use it later

  5. There is a Test Configuration in the lower right corner. You can click it to see if Success exists


3. Guard programmer’s smile — Automated Integration Deployment (here he comes, here he comes)

3.1 Project Construction

Take building a Maven project as an example. The steps are as follows:

  1. A new task

  2. Building a Maven project

  3. Source management configuration (select the project on Github)

  4. Trigger configuration (github will do this later)

  5. Post-build operation

3.2. Making configuration

3.3 Adding scripts to the Server

Go to the Remote Directory configured in 2.4 and create two scripts (modify the scripts to suit your needs).

stop.sh:

#! / bin/bash pid = ps - ef | grep springboot - demo - 0.0.1 - the SNAPSHOT. Jar | awk '{print $2}' if [-n] "$pid" then kill 9 $pid fiCopy the code

start.sh:

#! /usr/local/bash chmod 777 /usr/local/java-projects/springboot-demo-0.0.1 -snapshot. jar CD /usr/local/java-projects nohup Java - jar springboot - demo - 0.0.1 - the SNAPSHOT. Jar &Copy the code

3.4 test

  1. Write a HelloController
		@RestController
		public class HelloController {
		    @GetMapping("/hello")
		    public String hello(a){
		        return "Hello World!!!!!!!!!!!!"; }}Copy the code
  1. Push the project

  2. Jenkins began automated deployment of the project

  3. Click into the project to view the console

  4. Visit the project path beanchan.cn/hello (mine, sneak peek for you)


At this point, my first work is also a small end, the content is only a beginning, the subsequent part will be gradually improved, if there is any problem, or this article can be improved, or you still can not use the normal after finishing according to my instructions, welcome to discuss with me, thank you for watching.