Scenario: One-click deployment of a front-end static site on a CentOS server using Jenkins and Github.

The main process: Jenkins pulls the code from Github and performs operations such as packaging, running unit tests, etc. Then deploy the system to the CentOS server.

Preliminary knowledge

Git nodejs nginx: juejin.cn/post/684490…

CentOS common commands: juejin.cn/post/684490…

Using Vi/Vim: juejin.cn/post/684490…

Nginx configuration: juejin.cn/post/684490…


Jenkins Installation and Configuration

The installation

1. Install the Java dependency package first

yum install java

2. Install the wget download tool

yum install wget -y

3. Download Jenkins dependency

sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo

4. Import the secret key

sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

5. Install the Jenkins

yum install jenkins

6. Jenkins

  • /usr/lib/jenkins/: Jenkins installation directory where the WAR package will be stored

  • /etc/sysconfig/jenkins: Jenkins profile, “port”, “JENKINS_HOME”, etc can be configured here.

  • /var/lib/jenkins/: the default JENKINS_HOME.

  • /var/log/jenkins/jenkins.log: Jenkins log file.


configuration

1. Modify permissions

In order to avoid any problems due to permissions, change the user to root directly here.

vim /etc/sysconfig/jenkins

Let’s go to JENKINS_USER and change it to “root”




2. Change Jenkins primary catalog ownership

chown -R root:root /var/lib/jenkins

chown -R root:root /var/cache/jenkins

chown -R root:root /var/log/jenkins


3. Start Jenkins

Jenkins Start, restart, stop command:


service jenkins startstart


service jenkins restartrestart


service jenkins stopstop


service jenkins statusCheck the status

service jenkins restart



Enter your server IP address and Jenkins’ default port number 8080 in the browser to see the Jenkins unlock page.




4. Initialize Jenkins

Look at the screenshot above. To enter the administrator password, the red code block path is the password file.

Command line input:

vim /var/lib/jenkins/secrets/initialAdminPassword



Copy the password to the input box and go to the next step.



Select the recommended plug-in and wait for the installation



Creating an Administrator User



Without further comment, enter good information and click continue.

The next step is default.



The initialization is successful. If you encounter problems such as plug-in installation failure, try again.


use

1. Configure the NodeJS plug-in

Install the NodeJS plug-in

Select Manage Jenkins -> Manage Plugins



As follows:




Wait until the installation is complete.



NodeJS configuration:

Select Manage Jenkins -> Global Tool Configuration, scroll down to find NodeJS Configuration, and then Save.




2. Configure the SSH plug-in

To install the SSH plugin, go to Manage Jenkins -> Manage Plugins



Configure SSH: Select Manage Jenkins -> Configure System to find Publish over SSH, as shown below




4. Create a task

A. Click New Item and select Free Style Task

B. Enter the project description



C. Configure projects related to Github



Create a Github certificate



D. Select the nodeJS to use



E. The last step is to build the configuration



At this point, the task creation is complete. There’s a big hole to fill before we hit build.

Jenkins kills all spawn processes after build by default! Jenkins needs to be prohibited from killing spawn processes.

Command line input:

vim /etc/sysconfig/jenkins

Find JENKINS_JAVA_OPTIONS plus – Dhudson. Util. ProcessTree. Disable = true




If you are not deploying a front-end static site and you are starting a NodeJS interface service, the process is the same as above, and the build configuration needs to be modified to start the NodeJS service with pM2. For the nginx configuration of nodeJS service, see:
Juejin. Cn/post / 684490…Background interface configuration

The build configuration for the nodejs service is as follows:




5. Perform the task

Go back to the home page and select the task



Click Build Now to start a one-click Build release.



Each build is logged so you know what the build process is.



Here is another way to create a task.

Line ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Creating a pipeline Task







The process code is as follows:

pipeline {
    agent any

    stages {

        stage('Checkout repository') {
            steps {
                git branch: 'master', credentialsId: '5b1799f5-79d9-44cd-a635-212cf61e2860', url: '[email protected]:liuwei9413/vuecli3-test.git'
            }
        }

        stage('Install Deps and build project') {
            steps {
                sh 'rm -rf node_modules/'
                sh 'npm install'
                sh 'npm run build'
            }
        }

        stage('Run Unit Tests') {
            steps {
                sh 'npm test'
            }
        }

        stage('Deploy project') {
            steps {
                echo "Deploy project"
                sh "cp -rp dist/* /data/www/test/web1"
            }
        }

        stage('Check Service') {
            steps {
                sh 'service nginx restart'
                echo "success"}}}}Copy the code

After writing the process execution code, click Save.