An overview of the

Jenkins is an automatic build tool, which can automatically pull code from Git after configuring some application servers on Jenkins server

The project is then deployed and started into the application. The purpose of this article is to go through how to configure the entire deployment environment.

I’ve been doing some work on how the company’s real environment is configured. For reference only, the company is a small company, if there is any inadequacy of the place welcome to point out, I good improvement ~

The deployment plan

Jenkins construction method

  • The node way

    The node information configured by Jenkins was used for construction, which will be described in details later.

    The Jenkins server is easy to configure, but the application server needs git and Maven installed. Because you build on the application server

  • Publish plug-in mode

    Build by installing the plug-in Publish Over SSH, as described below.

    The Jenkisn server is complex to configure, but the application server only needs to install the JDK

Server Planning

The IP address type software Build a way
121.40 XX. XX Application server jdk git maven The node way
192.168.1.112 Application server jdk Publish plug-in mode
192.168.1.233 Application server jdk Publish plug-in mode
192.168.1.254 Jenkins server jdk git maven jenkins Jenkins server
120.79 XX. XX Application server jdk git maven The node way

Jenkins installs and starts

Liverpoolfc.tv: Jenkins. IO/useful/download…

Docker installation mode

  1. To install the docker

  2. Boot image

    docker run \
      -u root \
      --rm \
      -d \
      -p 8080:8080 \
      -p 50000:50000 \
      -v /data/jenkins:/var/jenkins_home \
      -v /var/run/docker.sock:/var/run/docker.sock \
      --privileged=true \
      jenkinsci/blueocean
    Copy the code

    Reference: Jenkins. IO/useful/doc/book…

    Running is may report errors Permission Denied, so added – ring = true \, refer to: www.jianshu.com/p/1ed499037…

Installation in WAR package mode

First you need to download the WAR package:

Address: Jenkins. IO/useful/download…

After downloading the file, run the following command:

nohup java -jar jenkins.war &
Copy the code

Through the ps – ef | grep Jenkins locate startup process:

[root@localhost ~]# ps -ef | grep jenkins
jenkins    1445      1  0 Apr08 ?        10:21:30 /usr/local/jdk/bin/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8080 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
root      89082  88082  0 13:07 pts/0    00:00:00 grep --color=auto jenkins
Copy the code

-djenkins_home = /var/lib/Jenkins -djenkins_home = /var/lib/Jenkins -djenkins_home = /var/lib/Jenkins

If you need to specify additional parameters, such as specifying a port for startup, you can run the following command to start the startup:

java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /data/jenkins/jenkins.war --logfile=/var/logs/jenkins/jenkins.log --webroot=/var/cache/jenkins/war --daemon --httpPort=8989 --debug=5 --handlerCountMax=100 --handlerCountMaxIdle=20
Copy the code

Start and initialize

Related start and stop commands

$ http://yourdomain:8080/exit        # stop service
$ http://yourdomain:8080/restart     # restart service
$ http://yourdomain:8080/reload      Reload the configuration information
Copy the code

Initialize the Jenkins

Visit Jenkins at http://localhost:8080

The replication password is required for the first unlock:

The figure above shows the location of the password in the server. If installed using Docker, the password file is under the mounted directory

# vim /data/jenkins/secrets/initialAdminPassword 

8079fb5d3e3f444d857fa5c8e7add6e9
Copy the code

Click “Continue” and click “Install recommended plug-ins”.

The plug-in is being installed, wait a moment

System management

After logging in Jenkins, there is a “System Administration” menu in the right menu, click it

The red boxes in the following figure are mandatory Settings for deployment. The rest depends:

So let’s set these guys up.

System Settings

Here is some basic information by default

Configuring Access Information

To configure the pulish over SSH plug-in, you need to configure the user name and password, create directory/WMS /services/ Jenkins on the application server 192.168.1.233, and click Test Configuration to verify.

The directory/WMS /services/ Jenkins in SSH Server node is very important.

Two of the later build tasks are based on this directory.

Of course, it doesn’t matter if the SSH Server node configuration is not configured if it is not built through the SSH Publishers plug-in.

192.168.1.112 The application Server is built in Publish mode, and the SSH Server information configured here is associated:

Global Tool Configuration

The Jenkisn server needs maven, JDK, and Git installed. I am pre-installed, you can also try to check automatic installation here.

Plug-in management

Ensure that the Git and Publish Over SSH plug-ins are installed successfully

Node management

Reviewing the server planning above, 120.79.xx. XX and 121.40.xx. XX are built in node mode, so nodes need to be configured separately here:

Taking 121.40.xx. XX as an example, click the right gear and then click Configure from node to view the node to be configured:

JVM options: -djava.awt. headless=true -djava.encoding =UTF-8

Configuring environment Variables

After saving, return to the node list. Click on the upper right corner of the allow automatic refresh, found normal connection, the effect is like the node list in the morning.

Begin to build

After the system administration is set up to start building, I’ve summarized two ways to build

Construction mode 1: Node mode

This mode needs to be configured in the system management configuration node, and then packaged and started in the remote management.

General Basic Configuration

Specify the running node and configure the node in System Management node Management

Source code management

Use SSH to specify the URL and authentication information of Git

Git basic information is specified in System Administration global Tool Configuration

The build

Invoke the top-level Maven target

The same as SSH.

  • Maven version

    Maven1 is the name of the added Maven environment. The Maven environment is configured in System Settings

  • The target

    Package submodules of the parent module by using maven command

    clean package -pl zhirui-zuul-server/ -am -Dmaven.test.skip=true
    Copy the code

    If there are submodules under submodules, the package command is as follows:

    clean package -pl zhirui-zmgj-business/zhirui-zmgj-server/ -am -Dmaven.test.skip=true
    Copy the code

Execution of the shell

Because it runs through the designated node, there is no need to deploy through the SSH plug-in. [System Management] [Node Management] Node configuration:

  1. Based on the above node configurationRemote working directory, JenkinsRemote working directory + workspace + task nameLet’s compile. So the jar path also needs to spell git project path.
  2. The path where the actual deployment is running

Execute the following script in shell:

#! /bin/bash

#Jenkins pushes the package to the directory on the application server
jenkins="/root/.jenkins/workspace/zhirui-zmgj-server-pro/zhirui-zmgj-business/zhirui-zmgj-server/target"

#Publish directory (change to the corresponding publish directory)
deployDir="/wms/services/zhirui-zmgj-server"

#Jar name (change to the corresponding JAR name)JarName = "zhirui ZMGJ - server - 0.0.1 - the SNAPSHOT. Jar"

#Fixed configuration
jarLib=$deployDir"/lib"
jarBin=$deployDir"/bin"
jarBak=$deployDir"/bak"
jarLog=$deployDir"/logs"

#Directory to buildif [ ! -d ${jarLib}"/" ]; then mkdir ${jarLib} -p fi if [ ! -d ${jarBin}"/" ]; then mkdir ${jarBin} -p fi if [ ! -d ${jarBak}"/" ]; then mkdir ${jarBak} -p fi if [ ! -d ${jarLog}"/" ]; ${jarLog} then mkdir -p fi echo "* * * * * * * * * * * * * * * * * * * * * * basic configuration information * * * * * * * * * * * * * * * * * * * * * * *" Java - version echo "publish project: "${jarName} echo "Post directory: "${jardir} echo "bin directory: "${jarBin} echo" Lib directory: "${jarLib} echo "bak directory: "${jarBak} echo "Log directory: "${jarLog} echo "*******************************************************"
#Check whether new reports have been pushed to the local PC. If yes, perform subsequent operations
if [ "$(ls ${jenkins}/${jarName} 2> /dev/null | wc -l)" != "0" ]; then
	#Backup fileOldJarName = "` ls ${jarLib} | grep. Jar | sort - nr | ` head - 1" echo "start backup jar package: "${jarLib}"/"${oldJarName} bakJarName=${oldJarName}$(date +%s) echo "Backup" ${jarLib} "/" ${oldJarName} "to" ${jarBak} "/" ${bakJarName} CD ${deployDir} if [" $(ls "lib/" *. Jar 2 > / dev/null | wc -l)" ! = "0"); Then mv ${jarLib}"/"${oldJarName} ${jarBak}" else echo" CD ${Jenkins} echo "${jarLib} cp ${Jenkins}"/"${jarName} ${jarLib} echo"	 #Copy start.sh and restart.sh from /var to the bin directory of the projectCp /var/env.start. sh. cp /var/env.restart. sh. chmod 775 * sh restart.sh else echo" ${jenkins}/${jarName}" exit 1 fiCopy the code

Construction method 2: Publish plug-in

This build is based on the plugin Publish Over SSH, which I call the Publish plugin approach. In this way, Jenkins server is packaged into JAR and then pushed to template server for running.

Click New Task

General Basic Configuration

SSH specifies the node to be run as master

Source code management

You need to go to Git and pull the code and compile it, and you need to add identity information to access Git

build

Invoke the top-level Maven target
  • Maven version

    Maven2 is the name of the added Maven environment. The Maven environment is configured in System Settings

  • The target

    Build individual modules using maven commands

    clean package -pl zhirui-zuul-server/ -am -Dmaven.test.skip=true
    Copy the code

    If it is a secondary submodule, the build command is as follows:

    clean package -pl zhirui-wms-business/zhirui-handle-file-server/ -am -Dmaven.test.skip=true
    Copy the code
  • Configuration files and global configuration

    Select the default Maven Settings, that is, the Maven environment configured in System Settings global Tools Configuration.

Send files or execute commands over SSH

The Jenkins plugin publishes Over SSH. It is used to configure the operation after the plug-in is pushed to the template application server. Several paths are easy to confuse, understand a little, and then try it yourself.

Before performing this step, you need to add the following SSH server configurations in System Management system Settings:

The instructions are as follows:

A: IP address of the SSH Server service Server

B and D: The Jenkins server pushes the JAR to the business server with the user account password information

C: The remote base directory is stored in the JAR on the SSH Server. I will refer to it as the remote base directory

Associate the following SSH servers with the SSH servers configured in System Management and System Settings:

Illustrated by the red box numbers in the figure above:

  1. SSH Server name, which indicates the Server on which the SSH Server is executed
  2. Based on Jenkins in system Management and System SetupHome directory ` ` / var/lib/JenkinsUnder theworkspace, the compiled JAR package is located in the Jenkins server workspace. For example, the full name of the directory is actually/ var/lib/Jenkins/workspace/zhirui - ZMGJ - request - service/zhirui - request - service/target/zhirui - request - service - 0.0.1 - the SNAPSHOT. The jar. Jenkins did by default/var/lib/jenkins/workspace/zhirui-zmgj-oauth-service/, includingzhirui-zmgj-oauth-serviceThis is the buildThe name of the task.
  3. Based on the directory in 2, willtargetGet rid of that prefix.
  4. Remote DirectoryIs a relative path required and for this buildThe name of the taskTo be consistent, Maven compiles the JAR to the target server (i.e192.168.1.112The server)Remote base directory + The name of the taskThe jar will not be found in subsequent shell scripts. For example, the full path to which the JAR is actually copied is/wms/services/jenkins/zhirui-zmgj-oauth-service/target, which is the path at position 6 in the red box below.
  5. Directory where the application server deployment is running
  6. The full path of the JAR under the relative path in 4. Copy to 5 for deployment.

The script for Exec Command is as follows:

Script description:

  1. Modify several according to the actual situationRelease directory.The name of the project.Jenkins push package catalog.
  2. jarBinThe directory needs to be placedStart a shell scriptStart. Sh and restart.sh in /var/env.Click to view the startup shell script.
#! /bin/bash

#Publish directory (change to the corresponding publish directory)
deployDir="/wms/services/zhirui-wms-erp-server"
#Project name (change to corresponding service name)JarName = "zhirui WMS - erp server - 0.0.1 - the SNAPSHOT. Jar"
#Jenkins push package catalog
jenkins="/wms/services/jenkins/zhirui-wms-erp-server/target"

#Fixed configuration
jarLib=$deployDir"/lib"
jarBin=$deployDir"/bin"
jarBak=$deployDir"/bak"
jarLog=$deployDir"/logs"

#Directory to buildif [ ! -d ${jarLib}"/" ]; then mkdir ${jarLib} -p fi if [ ! -d ${jarBin}"/" ]; then mkdir ${jarBin} -p fi if [ ! -d ${jarBak}"/" ]; then mkdir ${jarBak} -p fi if [ ! -d ${jarLog}"/" ]; ${jarLog} then mkdir -p fi echo "* * * * * * * * * * * * * * * * * * * * * * basic configuration information * * * * * * * * * * * * * * * * * * * * * * *" Java - version echo "publish project: "${jarName} echo "Post directory: "${jardir} echo "bin directory: "${jarBin} echo" Lib directory: "${jarLib} echo "bak directory: "${jarBak} echo "Log directory: "${jarLog} echo "*******************************************************"
#Check whether new reports have been pushed to the local PC. If yes, perform subsequent operations
if [ "$(ls ${jenkins}/${jarName} 2> /dev/null | wc -l)" != "0" ]; then
	#Backup fileOldJarName = "` ls ${jarLib} | grep. Jar | sort - nr | ` head - 1" echo "start backup jar package: "${jarLib}"/"${oldJarName} bakJarName=${oldJarName}$(date +%s) echo "Backup" ${jarLib} "/" ${oldJarName} "to" ${jarBak} "/" ${bakJarName} CD ${deployDir} if [" $(ls "lib/" *. Jar 2 > / dev/null | wc -l)" ! = "0"); Then mv ${jarLib}"/"${oldJarName} ${jarBak}"/"${bakJarName} echo	#Copies the new package to the specified directory${Jenkins} cp ${Jenkins}"/"${jarName} ${jarLib}	#Restart the service
	cd ${jarBin}
	#Copy start.sh and restart.sh from /var to the bin directory of the projectCp /var/env.start. sh. cp /var/env.restart. sh. chmod 775 * sh./restart.sh else echo ": ${jenkins}/${jarName}" exit 1 fiCopy the code
Comparison between server directories and configuration parameters:

I feel that the path configured here is very complicated, I will try to make it clear, if you feel more round, you can skip, more tests can be.

Parameters to be configured:

Description:

  1. 1 The red box corresponds to the red box shown in the following figure. The path you want to fill in is based on the home directory +workspace

  2. The red boxes in 2.1 and 2.2 correspond to the red boxes in figure 2. The path of the JAR after it is pushed to the application server is filled in. The 2.1 red box is the path based on the remote base directory. The jar installation 2.1 configuration is pushed to the target server. The result is the 2.2 red box path.

  3. Red box 3 corresponds to red box 3 in the following figure. After the shell script configured in Exec Command is executed, the JAR will be copied to the path in red box 3 for execution, which is the actual running path of the program.

Some directory structure diagrams for the application server and Jinkins server:

Description:

  1. The Jenkins server is packaged into jars and stored inHome directory ` ` workspaceThe position of the bottom.
  2. After being pushed to the service server, based onRemote base directoryPost position.
  3. When the service server runs the JAR, it executes a shell script, copies the script to this location, and starts the jar.

Configure the service server environment

All application servers need to be configured except the Jenkins server

Set the environment variables used by the program

Sh must be created in the /var/env directory. The following startup and restart scripts will load /var/env.sh.

These environment variables are used by our program eureka Server and ConfigServer, so they need to be configured. If not, do not configure it.

$vim setEnv.sh
#! /bin/bash
source ~/.bash_profile
export BUILD_ID=dontkillme
# for jdk env
#exportJAVA_HOME = / usr/jdk1.8.0 _112
#export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
#export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH
#The specified profile
export ENV_OPTS="-Dconfig.profile=dev"
#Specify the Eureka addressExport WMS_EUREKA_SERVER_URL = "http://192.168.1.254:8090/eureka/"#Specify the configuration center addressExport WMS_CONFIG_SERVER_URL = "http://192.168.1.254:8861" export WMS_DEVELOPER_NAME = "" export WMS_CONFIG_GIT_USER="config-read" export WMS_CONFIG_GIT_PWD="config-read" export WMS_CONFIG_GIT_URI = "http://192.168.1.254:8000/config/config-file.git" export WMS_CONFIG_GIT_SEARCHPATHS = "dev"Copy the code

Set the startup script and restart script

Environment introduction

After manual or Jenkins package push, the actual directory of the application is/WMS /services, and the structure is as follows:

[root@localhost services]# ll total 8 drwxr-xr-x. 11 root root 4096 Jul 24 19:02 Jenkins #SSH server when configured directory ---------- 1 root root 632 Jul 2 10:55 setenv. sh # Drwxr-xr-x. 6 root root 99 Apr 9 18:08 zhirui-console-server drwxr-xr-x. 6 root root 51 Jul 2 00:04 zhirui-oauth-service-zmgj drwxr-xr-x. 6 root root 51 Jul 1 23:42 zhirui-platform-sysmanager-server-zmgj drwxr-xr-x. 6 root root 51 Jul 24 17:57 zhirui-zmgj-oauth-service drwxr-xr-x. 6 root root 51 Jul 1 19:28 zhirui-zmgj-server drwxr-xr-x. 6 root root 51 Jul 1 23:09 zhirui-zuul-server-zmgjCopy the code

Go to one of the application projects and view the directory:

  • Bak: The directory of the previous JAR is automatically backed up at build time
  • Bin: Two shell scripts, start.sh and restart.sh, are required. For Jenkins startup, two scripts need to be placed in /var/env. Start. sh and restart.sh in /var/env will be copied to the bin directory. If you manually start the program, add it in bin.
  • Lib: The actual running JAR, which will be placed here automatically when building
  • Logs: Used to store logs. Whether to store logs depends on the log path configuration in the application
[root@localhost services]# cd zhirui-zuul-server-zmgj/ [root@localhost zhirui-zuul-server-zmgj]# ll total 4 drwxr-xr-x. 2 root root 4096 Jul 24 14:02 bak drwxr-xr-x. 2 root root 40 Jul 1 23:09 bin drwxr-xr-x. 2 root root 51 Jul 24 14:02 lib  drwxr-xr-x. 2 root root 6 Jul 1 23:09 logs [root@localhost zhirui-zuul-server-zmgj]#Copy the code

scripting

Sh and start.sh are as follows:

Create the file directly after the copy and paste can be, the script has loaded the above environment variables:

source $DEPLOY_DIR/.. /setEnv.shCopy the code

Two scripts require authorization: chmod 775 *

Start. Sh:

#! /bin/bash
source ~/.bash_profile
echo "Sping Cloud ops script.wms" 

function green() {
 echo -e "\033[32m$1 \033[0m"
}

function red() {
 echo -e "\033[31m$1 \033[0m"
}

# set dircd `dirname $0` DISK_DIR=/wms/logs BIN_DIR=`pwd` DEPLOY_DIR=`cd .. ; pwd` CONF_DIR=${DEPLOY_DIR}/conf LIB_DIR=${DEPLOY_DIR}/lib LOGS_DIR=${DEPLOY_DIR}/logs LOGS_ROOTPATH=${DISK_DIR}/${SERVER_NAME}

# set env
# source $DEPLOY_DIR/.. /setEnv.sh
source /var/env/setEnv.sh
# check config

# set APP's OPT
JAR_NAME="`ls ${LIB_DIR} | grep .jar | sort -nr | head -1`"
# for jcdz project
RUN_JAR_PATH=${LIB_DIR}/${JAR_NAME}
# echo descecho "################################################################################" java -version echo "JAVA_HOME=$JAVA_HOME" echo "DEPLOY_DIR=$DEPLOY_DIR" echo "RUN_JAR_PATH=$RUN_JAR_PATH" echo "# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #"
# check process
PIDS=`ps -ef | grep java | grep "$LIB_DIR" |awk '{print $2}'`
if [ -n "$PIDS" ]; then
 red "ERROR: The [$DEPLOY_DIR:$SERVER_NAME] already started! PID is $PIDS!"
 exit 1
fi

#init log
ERROR="-Dpro.log.level=ERROR"


# init logs dirif [ ! -d $LOGS_ROOTPATH ]; then mkdir -p $LOGS_ROOTPATH fi JAVA_OPTS="${ENV_OPTS} -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8" JAVA_DEBUG_OPTS="" if [ "$1" = "debug" ]; then JAVA_DEBUG_OPTS=" -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n " fi JAVA_JMX_OPTS="" if [ "$1" = "jmx" ]; then JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false " fi JAVA_MEM_OPTS="" BITS=`java -version 2>&1 | grep -i 64-bit` if [ -n "$BITS" ]; then JAVA_MEM_OPTS=" -server -Xms512m -Xmx512m -XX:MaxMetaspaceSize=512m -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 " else JAVA_MEM_OPTS=" -client -Xms512m -Xmx512m -XX:MaxMetaspaceSize=512m -XX:SurvivorRatio=2 -XX:+UseParallelGC " fi green "Starting the $SERVER_NAME ... \c" nohup java -Dlog.rootPath=$LOGS_ROOTPATH $ERROR $JAVA_OPTS $JAVA_MEM_OPTS -jar $RUN_JAR_PATH > /dev/null 2>&1 & green "OK! \c" PIDS=`ps -ef | grep java | grep "$LIB_DIR" | awk '{print $2}'` green "PID: $PIDS" green "LOGS_FILE: $LOGS_ROOTPATH"Copy the code

Restart. Sh:

#! /bin/bashfunction green() { echo -e "\033[32m$1 \033[0m" } function red() { echo -e "\033[31m$1 \033[0m" } DEPLOY_DIR=`cd ..; pwd` LIB_DIR=${DEPLOY_DIR}/lib echo "${LIB_DIR}"


# check processPIDS=`ps -ef | grep java | grep "${LIB_DIR}" |awk '{print $2}'` if [ -n "$PIDS" ]; then red "ERROR: already started! PID is $PIDS!" Kill -9 "$PIDS" green "Restarting..." fi sh ./start.shCopy the code

Other operating

Password reset

I recently forgot the administrator password while working with Jenkins. Reset:

Stop the tomcat service first, and then vim/root /. Jenkins/users/admin_ * * * / config. XML files,

Find the field and replace it with:

#jbcrypt:$2a$10$MiIVR0rr/UhQBqT.bBq0QehTiQVqgNpUGyWW2nJObaVAM/2xSQdSq
Copy the code

In this way, the password of the administrator account becomes 123456. Start Tomcat and open Jenkins to log in.

Integration with project management tool JIRA

Reference: blog.csdn.net/qq_40554364… Reference: blog.csdn.net/boonya/arti…