Jenkins, a 3W analytical continuous integration and deployment tool

why? Why?

The process of manually deploying a project is painful and tedious, and a continuous integration and deployment tool can help free your hands to focus on other things.

In addition, Jenkins is open source and recognized as the leader in CI (Continuous Integration) tools. Until 2020, the position is still unshakeable, widely used in many types of project deployment, automated build, test and deployment functions. Jenkins, although written in Java, integrates and deploys projects written in almost all popular programming languages through plug-ins.

how? How does it work?

First of all, it is necessary to understand the whole deployment process of the project, and then realize Jenkins by combining the plug-in warehouse provided by Jenkins for each process. An example of a Maven project written in the Java language using Git for version management:

Git repository pull code → Maven compile and package → upload the package via FTP to the server → execute the startup script to run the project

This process will use some plug-ins provided by Jenkins to achieve the function, Git Parameter(parameterized retrieval code), Maven Integration (using Maven build code), and Publish Over SSH (SFTP transfers files and executes shell commands) are used to complete the deployment process.

what? What is?

So what exactly is Jenkins? Look at its self-introduction!

Hahaha, its manifesto is: Build great, do anything

Jenkins is the leader in open source CI&CD software, providing over 1,000 plug-ins to support build, deploy, automate, and meet the needs of any project.

Since 6, so that you know about the using Jenkins to a full range of systems, this blog will introduce Jenkins from installation to the front and back end project deployment, and to allocate and use the common problems in the process, in principle, basically meet the needs of enterprise daily process can also be through optimization, expanding and other plug-ins, Without more words, the whole begins. Examples of front-end and back-end deployment in this article:

Backend example:

Programming language: Java

Build tool: Maven

Code management: Git

Front-end examples:

Project type: Vue project

Build tool: Node

Code management: Git

Set up Jenkins and tool configuration

1. Install the Jenkins

● Docker install Jenkins

docker run -itd \
-p 8080:8080 -p 50000:50000 \
--name jenkins --privileged=true \
-v /data/jenkins:/var/jenkins_home \
docker.io/jenkins/jenkins:lts
Copy the code

● Install Jenkins with WAR package

  1. Download the WAR package:

    Mirrors. Jenkins. IO/war – stable /…

  2. Activation:

    java -jar jenkins.war --httpPort=9090

● RPM Install Jenkins

1. Download Jenkins’ RPM package

PKG. Jenkins. IO/redhat – stab…

  1. The installation

    Sudo RPM - ih Jenkins - 2.235.1-1.1 noarch. RPM

  2. Start the

    systemctl start jenkins

Docker and RPM are recommended for installation!

After the installation is complete, the browser can visit http://localhost:8080 to start the visit, as shown in the picture below that familiar old man:

This process will take a few minutes, at which point Jenkins will automatically do some initialization. If you are careful, you will notice that many files have been generated in the jenkins_home folder on the server. (Please pay attention to this detail, the author will not do any useless work, will be used in the plug-in acceleration step)

config.xml hudson.model.UpdateCenter.xml jenkins.install.UpgradeWizard.state jobs nodeMonitors.xml plugins secret.key secrets userContent war copy_reference_file.log identity.key.enc jenkins.telemetry.Correlator.xml logs nodes queue.xml.bak secret.key.not-so-secret updates users

After completion, the browser page will ask you to enter Jenkins’ initialization password as follows:

Enter the server: the cat/var/jenkins_home/secrets/initialAdminPassword, copy and paste to can!

2. Configure plug-in acceleration

After entering this page, the plug-in download will probably timeout due to the use of an external address, resulting in download failure.

How to solve it?

1. Go to the jenkins_home directory and check whether the updates folder exists. If not, go to the address bar:

http://localhost:8080/pluginManager/advanced

2. Check whether the check Now button under the Update Site is red. If it is not red, go to the next step directly.

Change the URL address to:

Mirrors.tuna.tsinghua.edu.cn/jenkins/upd…

Submit and Check now!

(note: after the switch to the address in essence does not solve the problem of plug-in download, specific reasons can reference: blog.csdn.net/s78365126/a…).

3. Go to the Jenkins _HOME /updates directory on the server and replace the address of the plug-in in default.json.

#Go to the updates directory under jenkins_home

cd ${jenkins_home}/updates

#Replace the plug-in address in default.json

sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
Copy the code

Then reboot Jenkins!

Docker way: docker restart Jenkins | RPM: systemctl restart Jenkins

Download plug-in again, speed up a hundred times there is no! Visit: http://localhost:8080/, as shown in figure:

After downloading, enter the configuration of admin user and Jenkins address, skip and default respectively.

Three Jenkins deployed back-end projects

1. Back-end deployment principles and plug-in applications

Principle:

  1. First, Jenkins pulled the project code from Gitlab or Github or Gitee git repositories;

  2. For Java projects, source code is compiled and packaged using Maven or Gradle according to type needs;

  3. Jenkins uploads the packaged project to the server where it will be published to start with an execution script.

Jenkins provides a plug-in repository to facilitate the integration and support of various technologies, including the support of container technology Docker. In each link of Jenkins implementation, a variety of solutions are replaced and optimized. In combination with the common back-end project architecture, Jenkins is mainly used in the process of using:

JDK8, Maven3.6.3, Git, Shell, etc.

The plug-ins used are:

  • Maven Integration
  • Publish Over SSH
  • Git Parameter

After downloading the above three plug-ins from the plugin repository, it’s time to start the serious deployment backend project!

1. Configure the SSH Server

2. Configure global tools (JDK, Maven, git, etc.)

Go to System Management > Global Tool Configuration

2. Detailed explanation of project construction

1. Create projects

Click OK to get started

The restart script is as follows:

#! /bin/sh
file="test.jar"
pid=`ps -ef| grep $file | grep -v grep | awk '{print $2}'`
log="log.log"
time=$(date +%Y%m%d%H%M%S)

#cp $file $file".bak."$time

if [ -n $pid ]
then
        echo "kill "$file" success"
        kill -9 $pid
else
        echo "kill "$file" fail"
fi


nohup java -jar $file  > $log 2>&1 &
Copy the code
2. Start the build project

Jenkins deploys front-end projects

1. Front-end deployment principles and plug-in applications

Principle:

1. Firstly, Jenkins pulled the project code from Gitlab or Github or Gitee git repository;

2. Use Node to download modules for front-end projects;

3. Use WebPack to package the project;

4. Package the generated dist folder and transfer it to the remote server to be deployed.

5. Use shell scripts or commands to transfer the files to the nginx proxy folder.

Jenkins’ main role in front-end project deployment is to use Node to install and build, and then transfer the built files to the remote server. After that, Nginx performs proxy and access. In this demonstration, front-end is mainly used in Jenkins as follows:

Node.js v12.16.2, Git, Shell, etc.

The plug-ins and tools used are:

  • Publish Over SSH: to support a plugin that allows Jenkins to remotely Publish files to other servers

  • Git Parameter: a plugin that allows Jenkins to pull code from different branches of the Git repository

  • Node.js: To support continuous integration of Node.js projects, paths are configured in global tools by default

2. Detailed explanation of front-end project construction

User management and permission allocation

1. Download the permission extension plug-in and configure it

Management-jenkins → Manage Plugins

Plug-in name: Role-based Authorization Strategy

Plugin version: 3.0

Manage Jenkins → Configure Global Security

Instead, go to Manage Jenkins → Manage and Assign Roles and click Manage Roles

Click Add to add a role;

Admin: All rights have been granted

Operator: has the rights to view and deploy data

Reader: has the view permission

2. Create a user and assign rights to it

Go to Manage Jenkins → Manage Users and create a user

Go to Manage Jenkins → Manage and Assign Roles, then click Assign Roles to Assign the Roles you created earlier

Analysis of common Problems

1. The Jenkins version installed using Docker is old, and the plug-in upgrade fails or is incompatible

As you can see in the figure, the original Jenkins image has not been updated for two years as of July 2020. To install Jenkins, we recommend using Jenkins: LTS image.

2. Jenkins could not download the plug-in after successful installation, and “Instance offline” was displayed.

The blog can be reference: blog.csdn.net/s78365126/a…

In addition, if it is constructed in docker mode, you can try whether the container can ping the external network to see if it is in network mode.

3. If the number of files to be transferred is 0, the file transfer fails

Check whether the directory to upload files in the Publish over SSH edit area is correct, and check whether the directory to upload files in the workspace is correct. This problem is basically caused by the directory cannot find the corresponding files.

It is possible that the environment does not match the name of the label in the Publish over SSH edit area.

4. Run the shell script or the command Permission denied

This problem can occur in a number of ways:

  • Scenario 1: The current user does not have the permission to create directories for other users. Solution:

    Run the chown and CHGRP commands to switch the user and user group of a file.

  • Scenario 2: The directory to be operated does not have read/write permission or execute permission. Solution:

    Chmod Grants permission to operate folders or files.

  • Scenario 3: Unable to kill threads started by another user, solution:

    Switch to the root user kill original process, the current login user restarts.

5. Failed to find the packed file after using Maven to package the project

First, check whether the label name in the POM file in the project is consistent with that in the shell command. Second, check whether the path of the packaged file is consistent with that in the shell command.