Linux system uses docker to install jenkins: realize automatic deployment of springboot project, realize automatic deployment of maven project, realize automatic deployment of vue project, realize automatic deployment of WeChat applet project

Linux system uses docker to install jenkins: realize automatic deployment of springboot project, automatic deployment of maven project, automatic deployment of vue project, automatic deployment of WeChat applet project

2023-03-23

Recently, the automated deployment of the project has been deployed to the linux server. The server network is more stable and online 24 hours a day. No reliance on personal developer computers anymore.

This article records in detail the installation and configuration of jenkins using docker under the linux system.

And realize the automatic deployment of springboot project, maven project, vue project, WeChat applet project

It is more convenient to install jenkins with docker, and it can be done with a few commands, and it is also convenient for later migration.

Article directory

  • Linux system uses docker to install jenkins: realize automatic deployment of springboot project, realize automatic deployment of maven project, realize automatic deployment of vue project, realize automatic deployment of WeChat applet project
    • @[toc]
    • 1. Install docker
    • 2. Use docker to pull the jenkins image
    • 3. Use docker to start the jenkins container
    • 4. Visit the jenkins page and configure it
    • 5. Enter the jenkins container and set the Shanghai time zone
    • 6. Install the jinkins plugin
    • Seven, configure jdk and maven in jenkins
    • 8. Configure Publish over SSH of jenkins
    • Nine, create maven automatic packaging task
    • 10. Create Vue automatic packaging tasks
    • 11. Create an automatic update task for WeChat applets
    • postscript:

1. Install docker

  1. Install

    yum -y install docker-io # Install docker dependent packages, only this command can be done;
    
  2. start docker

    service docker start # Start docker
    
  3. Join boot

    chkconfig docker on # Add to boot
    
  4. view version

    docker version # View docker version
    
    Client:
     Version: 1.13.1
     API version: 1.26
     Package version: docker-1.13.1-208.git7d71120.el7_9.x86_64
     Go version: go1.10.3
     Git commit: 7d71120/1.13.1
     Built: Mon Jun 7 15:36:09 2021
     OS/Arch: linux/amd64
    
    Server:
     Version: 1.13.1
     API version: 1.26 (minimum version 1.12)
     Package version: docker-1.13.1-208.git7d71120.el7_9.x86_64
     Go version: go1.10.3
     Git commit: 7d71120/1.13.1
     Built: Mon Jun 7 15:36:09 2021
     OS/Arch: linux/amd64
     Experimental: false
    
  5. docker installation complete

2. Use docker to pull the jenkins image

  1. Log in to the mirror library
    https://hub.docker.com/explore/

  2. search jenkins

  3. pull image

     docker pull jenkins/jenkins # pull jenkins image
    

  4. View local mirrors

    docker images
    

    It is found that there is already a jenkins image in this machine.

3. Use docker to start the jenkins container

  1. Write a script to start the container

    # The script to start the jenkins container can be saved to the .sh file
    docker run -d -uroot -p 9096:8080 -p 50006:50000 --name jenkins --privileged=true -v /docker_chen/jenkins9096/jenkins_home:/var/jenkins_home jenkins/jenkins
    
  2. View container running status

    docker ps -a
    

    up 28 hours , indicating that it is starting normally and has been running for 28 hours

4. Visit the jenkins page for configuration

  1. Since port 9096 is mapped at startup, use IP:9096 to access
    For example: http://localhost:9096
    After entering, the page is as follows, plug-in installation, skip all plug-ins, do not install. Wait for the plug-in to be installed later.

  2. Go to the data volume installation directory and find the initial password
    Data volume directory, or go to the login container to find the corresponding password

    # docker data volume directory, or log in to the container to find the corresponding password
    cd /docker_chen/jenkins9096/jenkins_home/secrets
    
    # Or enter the container to find the corresponding password
    docker exec -it jenkins bash
    
    [root@csbl secrets]# cat initialAdminPassword
    4d14082024d1433dbf8d418d3ac545cb
    
  3. Set a new account password, any setting
    root 8d3ac545cb

5. Enter the jenkins container and set the Shanghai time zone

  1. Enter the jenkins container

    # enter the container
    docker exec -it jenkins bash
    
  2. Set the time zone to Shanghai

    # Set the time zone to Shanghai
    echo 'Asia/Shanghai' >/etc/timezone
    
  3. Restart the container

    # Restart the container
    docker restart jenkins
    #On the System Information page, you can see that the time zone has been set successfully user.timezone Asia/Shanghai
    

6. Install jinkins plugin

  1. Configure the jenkins plug-in mirror address
    Set as the mirror of Tsinghua University: https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

  2. Search for the plugin, then click Download to install
    It is recommended to install the following plugins, which are more commonly used:
    Chinese voice: Chinese (Simplified) Version
    Credential preservation: Credentials Binding
    maven packaging: Maven Integration
    svn pull code: Subversion
    SSH remote server: Publish Over SSH
    Node environment Vue packaging: NodeJS Plugin

7. Configure jdk and maven in jenkins

  1. Put maven, jdk1.8 and node.js in the shared data volume, and then configure it in the jenkins page
    The process of installing maven and jdk on the host linux system and the process of installing node.js are skipped here

  2. Click Global Tool Configuration to configure tools such as jdk/maven/node.js

  3. The configuration reference is as follows:

    ?

?

?

  1. Note that there is a 5-minute silent period for maven packaging
    It means that packaging with a time interval of less than 5 minutes will be ignored and will not be packaged.

8. Configure Publish over SSH of jenkins

Since jinkins runs in the container, and the deployed project is in the host, in this case, the container needs SSH to communicate with the host, upload files to the host and execute scripts of the host, etc.

  1. Click System Configuration to enter the settings for SSH

  2. Set SSH address and account password

9. Create maven automatic packaging task

  1. create task

  2. Set the packaging record to keep for 7 days, with a maximum of 10, to prevent the server disk from being full

  3. Configure the SVN source code address, and set the account credentials of svn at the same time

  4. Other configuration references for svn

  5. Set build triggers
    Satisfy certain conditions to trigger packaging operations
    Here, a simple polling scheme is directly used to check whether the svn version has changed every 10 minutes. If there is a new version of the code, the packaging action will be triggered.

  6. Set the maven instruction for Build packaging

    clean install -Dmaven.test.skip=true
    

  7. Configure SSH Publishers

    After the packaging is successful, use SSH to connect to the host, and then execute the host’s project deployment script

    Script reference:

    Core Business:

    1. Back up the package of the original project

    2. Copy the package packaged by maven in the target to the specified directory for deployment;

    3. After that, restart the project

    #[root@csbl jenkins9096]# cat jenkins_bs_java_server.sh
    #!/bin/bash
    ## For jenkin to package and call the automatic deployment project
    # @version 2023-03-16 chen
    # Jenkins packaged directory
    target="/docker_chen/jenkins9096/jenkins_home/workspace/JAVA_SERVER/taxation-sns-server/bootstrap/target"
    
    # The directory where the project is deployed
    bs_target="/root/taxation-sns-server-28585"
    
    # test directory
    #bs_target="/root/taxation-sns-server-28585/testLib"
    # Does the folder exist?
    echo "$target/lib"
    if [ -d "$target/lib" ];
    then
            timeVar=`date + %Y-%m-%d-%H.%M.%S`
            echo -e "\033[32m 【$timeVar】 $target/lib Exist,Begin Copy... \033[0m" #green
    
    
            # Back up the original file and modify the original folder to a timestamp suffix
            mv $bs_target/lib $bs_target/lib.bak.$timeVar
    
            # copy target file
            cp -r $target/lib $bs_target
            cp $target/taxation-sns-server-*.jar $bs_target/lib
     
            timeVar=`date + %Y-%m-%d-%H.%M.%S`
            echo -e "\033[32m 【$timeVar】 $target/lib Copy... SUCCESS \033[0m" #green
    
            # Out of service
            cd $bs_target/bin
            ./stop.sh
            # start the service
            ./start.sh
    
            timeVar=`date + %Y-%m-%d-%H.%M.%S`
            echo -e "\033[32m 【$timeVar】 $target/lib ReStart SUCCESS \033[0m" #green
    else
            echo -e "\033[31m $target/lib File Not Exist! Please check \033[0m" #red
    the fi
    

10. Create Vue automatic packaging task

  1. Create tasks, choose free style

  2. configure
    Discard old builds, SVN configurations, build triggers, etc., the same as creating maven automatic packaging, pay attention to replace the svn warehouse address.

  3. Build environment settings, select Provide Node environment

  4. post-build configuration
    After the construction and packaging are completed, SSH connects to the host server and calls the server script

  5. Server script, refer to:
    Script reference:

    Core Business:

    1. Back up the package of the original project
    2. Copy the dist package packaged by npm to the specified directory for deployment;
    3. The deployment is complete. Since the front end is deployed by Nginx, only the first two steps are required here.
    #[root@csbl jenkins9096]# cat jenkins_bs_vue_dist.sh
    #!/bin/bash
    ## For jenkin to package and call the automatic deployment project
    # @version 2023-03-16 chen
    # Jenkins packaged directory
    target="/docker_chen/jenkins9096/jenkins_home/workspace/VUE_DIST"
    
    # The directory where the project is deployed
    bs_target="/root/"
    
    # test directory
    #bs_target="/root/taxation-sns-server-28585/testLib"
    # Does the folder exist?
    echo "$target/dist"
    if [ -d "$target/dist" ];
    then
            timeVar=`date + %Y-%m-%d-%H.%M.%S`
            echo -e "\033[32m 【$timeVar】 $target/dist Exist,Begin Copy... \033[0m" #green
    
    
            # Back up the original file and modify the original folder to a timestamp suffix
            mv $bs_target/dist $bs_target/dist.bak.$timeVar
    
            # copy target file
            cp -r $target/dist $bs_target
     
            timeVar=`date + %Y-%m-%d-%H.%M.%S`
            echo -e "\033[32m 【$timeVar】 $target/dist Copy... SUCCESS \033[0m" #green
    
    else
            echo -e "\033[31m $target/dist File Not Exist! Please check \033[0m" #red
    the fi
    
    

Eleven, create a WeChat applet automatic update task

Main implementation ideas:

  1. Jenkins pulls the latest version of the WeChat applet code
  2. Use miniprogram-ci to update to the WeChat Mini Program platform
  3. After jenkins triggers to pull the code, it triggers the js script to be called
  1. Create automated grid tasks

  2. Discard old builds, SVN configurations, build triggers, etc., the same as creating vue automatic packaging, pay attention to replace the svn warehouse address

  3. build environment

    Since the applet does not need to be packaged separately, it can be skipped here.

  4. Post-build action, call server script

  5. Server bash script jenkins_bs_mini.sh reference:

    Core Business:

    1. This script is triggered by jenkins call
    2. The core business of this script is: to call the upload.js script
    #[root@csbl jenkins9096]# cat jenkins_bs_mini.sh
    #!/bin/bash
    ## For jenkin to package and call the automatic deployment project
    # @version 2023-03-16 chen
    # Jenkins packaged directory
    target="/docker_chen/jenkins9096/jenkins_home/workspace/MINI"
    
    # The directory where the project is deployed
    bs_target="/root"
    
    # Get the first two parameters
    iVersion=$1
    iDesc=$2
    
    # test directory
    #bs_target="/root/taxation-sns-server-28585/testLib"
    # Does the folder exist?
    echo "$target/node_modules"
    if [ -d "$target/node_modules" ];
    then
            timeVar=`date + %Y-%m-%d-%H.%M.%S`
            echo -e "\033[32m 【$timeVar】MINI $target Exist, Begin CI... \033[0m" #green
    
            node -v
            cd /docker_chen/jenkins9096/miniTool/src
            node upload.js $iVersion $iDesc
    
            timeVar=`date + %Y-%m-%d-%H.%M.%S`
            echo -e "\033[32m 【$timeVar】 $target MINI SUCCESS \033[0m" #green
    
    else
            echo -e "\033[31m $target MINI File Not Exist! Please check \033[0m" #red
    the fi
    
    
    
  6. Prepare the materials for WeChat applet automation CI
    Log in to the applet management background, obtain the APPID of the applet, deploy the private key private.xxx.key, etc.

  7. Write upload.js script
    In the host machine, create the directory miniTool

    mkdir miniTool
    

    Enter the miniTool folder and install the miniprogram-ci dependency

    cd miniTool
    npm install miniprogram-ci --save
    

    After the installation is complete, as shown in the figure:
    Note: private.xxx.key needs to be downloaded from the WeChat applet management background.

    Create a new src directory and write the upload.js file

    mkdir src
    cd src
    vi upload.js
    

    The upload.js file is as follows:

    [root@csbl src]# cat upload.js
    // Script for automatic deployment of WeChat applets
    // /docker_chen/jenkins9096/jenkins_home/workspace/MINI
    // npm install miniprogram-ci --save
    
    const ci = require('miniprogram-ci')
    
    
    // The first and second digits of the array, the node.exe path and upload.js path (the file to be executed) are received by default,
    // Starting from the third digit, it corresponds to each parameter (in order) carried by the running command node.
    
    // call script: node upload.js 6.6.0.svnV "description"
    // The recommended version is set to 6.6.0.svnV: 6.6 is the major version, 0 is the repair minor version, and svnV is the version managed by svn
    
    const version = process.argv[2];
    // custom notes
    const desc = process.argv[3];
    
    console.log(version, desc);
    
    const project = new ci. Project({<!-- -->
        appid: 'wx3ecaa6216dc9b271',
        type: 'miniProgram',
        // projectPath: "D:\work\space2\taxation-sns-mini-source/",
        projectPath: "/docker_chen/jenkins9096/jenkins_home/workspace/MINI",
        privateKeyPath: '../private.wx3ecaa6216dc9b271.key',
        ignores: ['node_modules/**/*'],
    })
    // Build npm when needed
    const warning = ci.packNpm(project, {<!-- -->
        ignores: ['pack_npm_ignore_list'],
        reporter: (infos) => {<!-- --> console.log(infos)}
    })
    console. warn(warning)
    
    
    const uploadResult = ci.upload({<!-- -->
        project,
        version: version,
        desc: desc,
        setting: {<!-- -->
            es6: true, // es6 to es5
            minifyJS: true, //minify JS code
            minifyWXML: true, //minify WXML code
            minifyWXSS: true, //minify WXSS code
            minify:true //Compress all codes, corresponding to "compressed codes" of Mini Program Developer Tools
        },
        onProgressUpdate: console. log,
    })
    
    console. log(uploadResult)
    
  8. Execution of the upload.js file

    # The core program is: node executes js file
    node upload.js
    
  9. Trigger the automatic deployment of the WeChat applet successfully
    Log in to the WeChat management background, you can view the effect:

Postscript:

Programmers, always develop software that saves your time

Save yourself time while improving your level!