SpringBoot e-Commerce project mall (25K + STAR) address: github.com/macrozheng/…

Abstract

There is a fairly complex mechanism behind any simple operation. This paper will take the packaged deployment of SpringBoot application in Docker environment as an example to explain in detail how to use Jenkins one-click packaged deployment of SpringBoot application.

Jenkins profile

Jenkins is the leader in open source CI&CD software, providing over 1,000 plug-ins to support build, deployment, automation, and any project needs. We can use Jenkins to build and deploy our projects, for example by taking code from our code repository, packaging our code into executable files, and then running our projects through remote SSH tool scripts.

Jenkins installation and configuration

Docker installation environment

  • Download Jenkins’ Docker image:
docker pull jenkins/jenkins:lts
Copy the code
  • Run Jenkins in Docker container:
docker run -p 8080:8080 -p 50000:5000 --name jenkins \
-u root \
-v /mydata/jenkins_home:/var/jenkins_home \
-d jenkins/jenkins:lts
Copy the code

Jenkins configuration

  • Run after a successful login to access the address Jenkins, first login to enter the administrator password: http://192.168.6.132:8080/

  • To log in using the administrator password, you can use the following command to obtain the administrator password from the container startup log:
docker logs jenkins
Copy the code
  • Obtaining the administrator password from logs:

  • Select the plug-in installation mode, here we directly install the recommended plug-in:

  • Enter the plug-in installation interface and wait for plug-in installation through networking:

  • After the installation is complete, create an administrator account:

  • For example configuration, configure Jenkins’ URL:

  • Click System Administration -> Plug-in Management to install some custom plug-ins:

  • Make sure the following plug-ins are installed correctly:

    • Role-based Authorization Strategy plug-in for role-based Authorization management
    • SSH Plugin for remote use of SSH
  • Go to System Administration -> Global Tools Configuration to configure global tools, such as Maven:

  • New Maven installation configuration:

  • Add the global SSH configuration in System Administration > System Configuration so Jenkins can use SSH to execute remote Linux scripts:

Role Rights Management

We can use Jenkins role management plug-in to manage Jenkins users. For example, we can grant all permissions to the administrator, the operation and maintenance personnel the relevant permissions to execute tasks, and other personnel only the viewing permissions.

  • Enable role-based permission management in System Administration > Global Security Configuration:

  • Choose System >Manage and Assign Roles.

  • Add the relationship between roles and permissions:

  • Assign roles to users:

Package and deploy the SpringBoot application

Here we use the mall-tiny-Jenkins module code from the Mall-Learning project to demonstrate how to enable Jenkins to deploy the SpringBoot application in one click.

Upload the code to your Git repository

  • First we need to install Gitlab (of course you can also use Github or Gitee), then upload the code from Mall tiny-Jenkins to Gitlab

  • Mall-tiny-jenkins project source address: github.com/macrozheng/…

  • The display effect in Gitlab after upload is as follows:

  • Have a little needPay attention toTo change the dockerHost address in pom.xml to your own Docker repository address:

Preparing for executing scripts

  • willmall-tiny-jenkins.shUpload the script file/mydata/shIn the directory, the script content is as follows:
#! /usr/bin/env bash
app_name='mall-tiny-jenkins'
docker stop ${app_name}
echo '----stop container----'
docker rm ${app_name}
echo '----rm container----'
docker run -p 8088:8088 --name ${app_name} \
--link mysql:db \
-v /etc/localtime:/etc/localtime \
-v /mydata/app/${app_name}/logs:/var/logs \
-d mall-tiny/${app_name}: 1.0 the SNAPSHOTecho '----start container----'
Copy the code
  • Add executable permission to the. Sh script:
chmod +x ./mall-tiny-jenkins.sh  
Copy the code
  • When you upload the. Sh script on Windows to Linux, you need to change the file format; otherwise, the script cannot be executed because of special formats.
# Use vim editor to modify
vi mall-tiny-jenkins.sh
# Check the file format, Windows upload default DOS
:set ff 
Change the file format to Unix
:set ff=unix 
# Save and exit
:wq
Copy the code
  • Run the. Sh script for testing, but do not run it:
./mall-tiny-jenkins.sh
Copy the code

Create execution tasks in Jenkins

  • First we need to create a new task:

  • Select build a free-style software project after setting the task name:

  • Then add in the source code management we git repository address: http://192.168.6.132:1080/macrozheng/mall-tiny-jenkins

  • At this point, we need to add a credential, which is the password of our Git repository account:

  • After filling in, select this credential and you can connect to git repository normally.

  • Then we need to add a build that calls the top-level Maven target, which is mainly used to package our source code into a Docker image and upload it to our Docker image repository:

  • Select our Maven version, then set the Maven command and specify the POM file location:

  • Then add a build that executes a remote shell script to execute the.sh script that starts the Docker container after our image is packaged:

  • The shell command to be executed is as follows: /mydata/sh/mall-tiny-jenkins

  • Then click save operation, our task is created, in the task list we can click Run to execute the task;

  • We can view the entire task execution through the console output:

  • After the success of the operation, access to the address to view the API documentation: http://192.168.6.132:8088/swagger-ui.html

Project source code address

Github.com/macrozheng/…

The public,

Mall project full set of learning tutorials serialized, attention to the public number the first time access.