An overview of the

Recently, I was working on a front-end large-screen visualization project with Echarts, and found it very difficult to manually deploy each update on the server, so I thought of Jenkins to automate the deployment and chose to place the code on Gitee and use WebHooks to trigger the updates. The back-end nodeJs code uses PM2 to daemon the process. It greatly improves the work efficiency and is very convenient, so I recorded the whole process on the mining for other people’s reference, because there are many pits encountered in the whole process, which need to be investigated one by one. I hope my article can reduce the time of other people’s troubleshooting.

Lead to

This article focuses on Jenkins installation and use, which is the core of the entire automated deployment, but other tools are also important, but due to space limitations, the detailed installation process is not documented in this article, I hope you can install the software on the server ahead of time:

  1. Registration code cloud account;
  2. Nginx.
  3. NodeJs;
  4. Pm2.
  5. java jdk
  6. git

The remote connection tool used in this article is Secure CRT.

NodeJs installation

Download nodejs from the official website.

Upload to the server and decompress

The tar XVF - node - v10.15.3 - Linux - x64. Tar. XzCopy the code

Configure the soft connection so that the node command can be used globally

Map the node source file to the node file ln -s /usr/local/node/node-v14.12.0-linux-x64/bin/node /usr/bin/node ln -s under usr/bin / usr/local/node/node - v14.12.0 - Linux - x64 / bin/NPM/usr/bin/NPMCopy the code

/usr/local/node/nodes-v14.12.0-linux-x64 /bin/node is my installation directory, you can choose in your own.

Check whether the installation is successful

[root@izbp19pml4ffssyx17ulvlz bin]# node -v
v14.12.0

[root@izbp19pml4ffssyx17ulvlz bin]# npm -v
6.14.8
Copy the code

To install other services, such as PM2, using Node in Linux, you also need to perform soft connection operations.

The JDK installation

Since Jenkins is written in Java, the Java JDK must be installed for normal use.

Prepare the JDK installation package

The JDK’s official website to download address: www.oracle.com/technetwork…

Before choosing a version, check to see how many bits your Linux server has:

[root@izbp19pml4ffssyx17ulvlz ~]# getconf LONG_BIT
64
Copy the code

After entering the following interface, first check “Agree” in the small box, and then select the version in the long box (I chose the one in the box, it can be different)

Upload and extract

Select secure CRT’s SFTP tool or another upload tool to upload the JDK installation package to the server.

  • Create a folder named Java mkdir /usr/local/java
  • Use the mv command to move to the file you just created
  • Run the tar command to decompress tar -xzvf

Configuring Environment Variables

Open environment variables and configure the following information: vi /etc/profile

JAVA_HOME = / usr/local/Java/jdk1.8.0 _221Copy the code

Above is my installation path, you can choose yours. Yes Run the source /etc/profile command for the configuration to take effect.

The installation is complete. Check whether the installation is correct

java -version
Copy the code

If the above interface appears, JDK8 has been installed.

Install git

Go to the Git release page on Github github.com/git/git/rel… You can find all the published versions of Git on this page. Here we choose the latest version of tar.gz.

Decompress and install

Wait patiently for the download to complete. We can see that the file name after downloading is v2.30.0. it is not the format of the package, don’t worry, it is just the link problem, manually change the file name to v2.30.0.tar.gz.

The mv v2.30.0. Tar. GzCopy the code

Decompress the package.

The tar - ZXVF v2.30.0. Tar. GzCopy the code

Go to the decompressed folder.

CD v2.30.0Copy the code

Once we have the unzipped source code we need to compile the source code, but before we do that we need to install the dependencies required for compilation. Enter the following command.

How to configure yum is not mentioned here. That’s the basics.

Note: when you install and compile source dependencies, yum automatically installs git for you, so you need to uninstall the older version of Git first.

yum remove git
Copy the code

Wait for the deletion to complete. When prompted, type y and press Enter.

Compiling git source code

make prefix=/usr/local/git all
Copy the code

Wait patiently for the compile to complete. It may take a few minutes.

Install git to /usr/local/git

make prefix=/usr/local/git install
Copy the code

Open the environment variable configuration file

vim /etc/profile
Copy the code

Add git related configuration at the bottom

PATH=$PATH:/usr/local/git/bin
export PATH
Copy the code

Refresh environment variables

source /etc/profile
Copy the code

Enter git –version to check whether git is safe to complete and to see its version number. At this point, download the latest source code from Github and install Git.

Here are just a few important ones, as for Nginx or any other software, refer to other articles on the web.

Install Jenkins

Download Jenkins

Create the Jenkins folder in /usr/local/. Enter themirrors.jenkins-ci.org/redhat/To download the RPM installation package of the new version.Be sure to download a newer installation package, otherwise Jenkins will initialize with a message indicating that many plugins cannot be installed due to an earlier Jenkins version

The installation

Upload the RPM package to the server and run the “RPM -ivh Jenkins RPM package name” command to install the RPM package:

RPM - the ivh Jenkins - 2.263.1-1.1 noarch. RPMCopy the code

Run the RPM -ql Jenkins command to query Jenkins files.

The/usr/lib/Jenkins Jenkins. War war file/etc/sysconfig/Jenkins configuration file/var/lib/Jenkins/JENKINS_HOME directory by default The/var/log/Jenkins Jenkins. Log Jenkins log file/etc/init. D/Jenkins Jenkins startup script filesCopy the code

If you do not modify the configuration file and add the JDK directory, service Jenkins start will report an error:

Job for jenkins.service failed because the control process exited with error code
Copy the code

Then modify the /etc/init.d/ Jenkins file to configure the JDK installation path.

In /etc/sysconfig/jenkins file, you can change the boot port of Jenkins service to 8081, because 8080 is often used by many services.

When the configuration is complete, start Jenkins again.

[root@izbp19pml4ffssyx17ulvlz jenkins]# service jenkins start
Starting jenkins (via systemctl):                          [  OK  ]
Copy the code

Added chkconfig Jenkins on.

access

Let’s go to the browser:http://server IP address :8081/

When you access Jenkins, there is an initial password that you need to enter. Here is how to find the initial password:

After input, access is successful, install the plug-in according to the default configuration:

Wait for plug-in installation to complete:

Create an administrator account, complete the configuration, and log in to Jenkins:

Install the Gitee plug-in

System Management -> Plug-in Management -> Optional Plug-ins -> Filter Gitee-> Select direct installation and restart Jenkins service after successful installation

Gitee makes private tokens

inGitee.com/profile/per…Generates a private token

Add Gitee(code cloud) link configuration (System Management -> System Configuration ->Gitee Configuration)

After the test is added, select Test. If the test succeeds, the test is added successfully.

Install JDK and Git

Then select System Management => Global Tool Configuration, uncheck automatic installation in JDK installation, and enter the root directory of Java JDK installation in JAVA_HOME. My root directory is /usr/Install Git. My Git installation directory is /usr/local/git/bin/git

A new task

Fill in the task name, select Build Free-style software project, and click OK.

New project

Configure the project general Settings

Configure the project source Settings

Configure project credentials

Building a trigger

Build the next steps

After the trigger is successful, go to the Node project directory, delete the old front-end package file, and copy the dist directory of Jenkins’ triggered file to the Node project directory. Nginx represents this directory.

After all the configuration is complete, before exit, be sure to click the save button, otherwise the work falls short.

Gitee(code cloud) configuration

Enter the URL and password generated by the build trigger in the Gitee project management WebHooks management interface. After you submit a new file to this directory via Git, Gitee will send a post request to Jenkins using this URL. Jenkins know that you have submitted a new file, will trigger pull operation, complete, in your/var/lib/Jenkins/workspace/directory will have on your gitee new project file.

test

If you look at the Jenkins build record, if you look at the project commit code on Gitee, you’re going to add a build record here. Blue indicates success.

Looking at the Node Project Vision project, the shell script in the Jenkins Vision task has copied the dist file to the dist directory. Next, you can statically proxy through NGINx.

I encountered an error at build time that the sudo command required a password, so I did not proceed with the build, and finally found the method to execute sudo Visudo, adding the following information at the end of the file

jenkins ALL=(ALL) NOPASSWD: ALL
Copy the code

Use nginx to broker front-end projects

In/usr/local/node/project/vision/dist directory is packaged good file, we as long as the agent for the files, let agent by pointing to the index. The HTML file.

Listen on port 91, when the nginx listening to the browser connection port 91, will be the default return/usr/local/node/project/vision/dist directory index. The HTML files, and to monitor contains URL/API/address, And forwards it to http://127.0.0.1:8888, the URL used by the back-end KOA2 framework building service to return display data to the front end. With the NGINx reverse proxy, cross-domain problems can be resolved.

server { listen 91; # server_name localhost; root /usr/local/node/project/vision/dist/; index index.html index.htm login.html ; Location/API / {proxy_pass http://127.0.0.1:8888; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } error_page 404 /40x.html; location = /40x.html { root 40x.html; }}Copy the code

The project interface has been displayed in the browser, but no data has been returned because the backend service has not been deployed on the server.

Deploying background Services

Next, deploy the background services and daemon the process through PM2. Create a new task on Jenkins called KOA-Vision and bind it to the KOA-Vision repository on Gitee. For details, see the tutorial above.

Install the pm2

Run the NPM install pm2 -g command to install pm2 globally and configure the soft connection.

Ln -s/usr/local/node/node - v14.12.0 - Linux - x64 / bin/pm2 / usr/local/bin/pm2Copy the code

Run the “sudo pm2 start app.js” command to start the app, and the “sudo: pm2: command not found” message is displayed.

After some searching, the reason is that when the sudo command is run, the system sets secure_path (secure path) as configured in sudo Visudo to restrict user behavior. The default secure path is as follows:

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Copy the code

A common solution on the web is to cancel env_reset and reset the PATH variable used by the sudo command with an alias in bashrc. However, in consideration of security limitations and this particular problem, we created two soft connections in /usr/bin, pm2 and node, to point to the corresponding commands in the node installation directory:

Ln -s /usr/local/node/node-v14.12.0-linux-x64/bin/pm2 /usr/bin/pm2 ln -s / usr/local/node/node - v14.12.0 - Linux - x64 / bin/node/usr/bin/nodeCopy the code

Be sure to link node to this directory as well, because pm2 will eventually call the node command to start the node service.

Next set up the post-build shell script for the KOA-Vision task:

cd /usr/local/node/project/vision/koa-vision/
sudo rm -rf *
sudo cp -r /var/lib/jenkins/workspace/koa-vision/* ./
sudo pm2 stop vision; sudo pm2 start app.js --name vision
Copy the code

And trigger executionRun the sudo pm2 list command to view:View the front interface:

The background data is already displayed in the browser. At this point, the entire Jenkins automated integration deployment configuration is complete.

conclusion

The whole project deployment process is still quite cumbersome, for people who are not familiar with Linux, will encounter many pits. So, I’ve documented the entire deployment process for your reference.