jenkins automated deployment (node server)

1. Nginx installation and startup

If we want to install a software package, we can do the following:

# Search for packages
dnf search package-name

# View package information
dnf info package-name

# Install packages
dnf install package-name

Deployment will use nginx, so you need to install nginx first:

dnf install nginx

Start nginx:

# Start nginx
systemctl start nginx
# Check startup status
systemctl status nginx
# Configure boot and restart
systemctl enable nginx

If an error is reported at startup: Job for nginx.service failed because the control process exited with error code, please refer to Nginx – Nginx startup error Job for nginx.service failed because the control process exited with error code-CSDN blog

2. Install Node.js

# Install nodejs
dnf install nodejs

3. Install MySQL

If the project does not use MySQL, you can omit this step.

3.1 Install MySQL

# Install MySQL. Adding -y here means that the dependent content is also installed.
dnf install mysql-server -y

3.2 Start mysql-server

# Start MySQL background service
systemctl start mysqld
?
# Check the MySQL service: active (running) indicates successful startup
systemctl status mysqld
?
# Start with the system
systemctl enable mysqld

3.3 Configuring MySQL

Configure MySQL account and password:

mysql_secure_installation
?
# Next there are some options, such as password strength and so on.
# Starting from MySQL8, password strength is usually set to be stronger, select 2
# Other options can be selected by yourself

Now, you can operate MySQL directly in the server:

But what if we want to connect directly to MySQL on our own computer?

  • That is, establish a remote connection with MySQL;

  • For example, directly connect to MySQL in the Navicat tool;

At this time, root must be configured to connect remotely:

# Use mysql database
use mysql;
# Check the user table for connection permissions. By default, root is localhost.
select host, user from user;
# Modify permissions
update user set host = '%' where user = 'root';
?
#Configuration takes effect
FLUSH PRIVILEGES;

Note: You need to configure the security group of 3306

4. Install pm2

In the actual deployment process, a tool pm2 will be used to manage the process of Node:

  • PM2 is a process manager for Node;

  • You can use it to manage the background process of Node;

  • In this way, when the terminal is closed, the Node process will continue to execute, and the server can continue to provide services;

Install pm2:

npm install pm2 -g

pm2Commonly used commands:

# Name the process
pm2 start app.js --name my-api
# Display all process status
pm2 list
# Stop the specified process
pm2 stop 0
# Stop all processes
pm2 stop all
# Restart all processes
pm2 restart all
# Restart the specified process
pm2 restart 0
?
# Kill the specified process
pm2 delete 0
# Kill all processes
pm2 delete all
?
#Run pm2 in the background and start 4 app.js to achieve load balancing
pm2 start app.js -i 4 

5. Jenkins configuration and installation process

5.1 Install Java environment

Jenkins itself depends on Java, so you need to install the Java environment first. The Java 11 environment is installed here. The new version of jenkins requires at least java 11 version or above.

dnf install java-11-openjdk

5.2 Connecting to Jenkins warehouse

Because Jenkins itself is not in the software warehouse package of dnf, you need to connect to the Jenkins warehouse:

  • wget is a tool for downloading files in Linux. -O means outputting to a folder and naming the file;

  • rpm: The full name is The RPM Package Manage, which is the next software package manager for Linux;

wget –O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
?
#Import GPG key to ensure your software is legal
rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
# or
rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key

5.3 Edit jenkins.repo file

Can be edited through vim

vi /etc/yum.repos.d/jenkins.repo
?
# i means insert
i
?
# Paste the following content
[jenkins]
?
name=Jenkins-stable
?
baseurl=http://pkg.jenkins.io/redhat
?
gpgcheck=1
?
# Press esc to exit editing, but not completely exited at this time
?
# Press shift and add:, then enter wq to save the edited content and exit

5.4 Install Jenkins

dnf install --nogpgcheck jenkins

6. Jenkins startup, access and installation of plug-ins

6.1 Start the Jenkins service

systemctl start jenkins
systemctl status jenkins
systemctl enable jenkins

Jenkins uses the 8080 port to provide services by default, so you need to join the security group:

6.2 Access Jenkins

At this time, enter ip:8080 in the browser to test whether the jenkins service is started successfully. If it is still inaccessible, refer to: https://www.cnblogs.com/luoshuai7394/ p/17341966.html

View the administrator password:

cat /var/lib/jenkins/secrets/initialAdminPassword

6.3 Install plugins recommended by Jenkins

Install the default recommended Jenkins plug-in:

7. Configure Jenkins

7.1 Create administrator user

7.2 Install git

for the server

dnf install git

7.3 Configure Node environment for Jenkins

Configure the Node environment: Restart Jenkins after successful configuration

Install the Node plugin:

8. Automated project deployment

8.1 Install Gitee Plugin for Jenkins and configure Jenkins URL

8.2 Install the Publish Over SSH plug-in for Jenkins and configure it

The main purpose of using this plug-in here is to restart the project after installing the project dependencies.

This step refers to this article: Instructions for using the jenkins plug-in [Publish Over SSH]_jenkins over ssh_Xiao Ming Daqiang’s blog-CSDN blog, thank you to this brother.

Install:

Configuration (password mode):

Then click Advanced, check [Use password authentication, or use a different key] to log in with a password, fill in the password, port, and connection timeout:

Click [Test Configuration] to test the connection and display Success

8.3 Create project directory

First, you need to create a project directory for the server. The directory location is arbitrary. Here, it is created in the root directory.

mkdir xxx

8.4 Configure nginx.conf file

The directory where the Nginx configuration file is located

/etc/nginx/nginx.conf

Configure user:

- /etc/nginx/nginx.conf
?
- user nginx;
 + user root
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

Restart Nginx:

systemctl restart nginx

8.5 Implement automated deployment

Create new task:

Configuration items and retention policies:

Source code management:

  • If the project is a private project, you need to add a user. If it is an account and password, enter the account and password corresponding to gitee or github.

Build trigger:

Build environment:

Add shell script:

pwd
node -v
npm -v
?
npm install
?
# Delete all contents in the /root/xxx folder
rm -rf /root/xxx/*
?
# Copy all the contents in the root directory to xxx
cp -rf ./* /root/xxx/

Add build steps:

The Exec command used here depends on the specific situation.

cd exercise_demo/
pm2 stop demoServer
pm2 start ./src/main.js --name=demoServer

Save application

8.6 GitEE settings WebHooks management

8.7 Add access permissions to jenkins user

The third plan is effective in personal testing.

8.7.1 Option 1: Modify the default user
- /etc/sysconfig/jenkins









?
JENKINS_JAVA_CMD=""
?
## Type: string
## Default: "jenkins"
## ServiceRestart: jenkins
#
# Unix user account that runs the Jenkins daemon
# Be careful when you change this, as you need to update
# permissions of $JENKINS_HOME and /var/log/jenkins,
# and if you have already run Jenkins, potentially other
# directories such as /var/cache/jenkins .
#
- JENKINS_USER="jenkins"
 + JENKINS_USER="root"
8.7.2 Add Jenkins to the root group
sudo usermod -a -G root jenkins
8.7.3 Give Jenkins directory permissions
chown -R jenkins path
# Example: chown -R jenkins /root/yc_airbnb
8.7.4 Restart Jenkins
systemctl restart jenkins

7.8 Push code

After pushing the code to the warehouse, check whether Jenkins has packaged it. If it does, it means it was successful.

9. Enable scheduled construction (not necessary)

#Build every half hour OR check the remote code branch every half hour, and build if there are updates
H/30 * * * *
?
#Build every two hours OR check the remote code branch every two hours and build if there are updates
H H/2 * * *
?
#Build regularly at two o'clock every morning
H 2 * * *
?
#Execute the build on the 15th of every month
H H 15 * *
?
#Working days, executed at 9 a.m. sharp
H 9 * * 1-5
?
#Every week on 1, 3, 5, starting from 8:30 and ending at 19:30, built every 4 hours and 30 minutes
H/30 8-20/4 * * 1,3,5