First, use scenarios
For projects packaged by appassembler-Maven-plugin package plug-in, automated deployment by Jenkins is realized
Two, basic environment preparation
-
Build a working Jenkins
Setup process……. omit
Set up the server you want to deploy in Jenkins
Configure the SSH server address from the main window
-
The flow chart
$\color{red}{note: The server you want to deploy remotely must be open to the server where Jenkins is currently located trust each other}$Copy the code
-
Configure the project using the packaged plug-in appassembler-Maven-plugin
<build> <plugins> <! --> <plugin> <! -- http://www.mojohaus.org/appassembler/appassembler-maven-plugin/usage-daemon-generatorconfig.html --> < groupId > org. Codehaus. Mojo < / groupId > < artifactId > appassembler -- maven plugin < / artifactId > < version > 2.0.0 < / version > <executions> <execution> <id>generate-jsw-scripts</id> <phase>package</phase> <goals> <goal>generate-daemons</goal> </goals> <configuration> <repositoryLayout>flat</repositoryLayout> <encoding>${project.build.sourceEncoding}</encoding> <! --<includeConfigurationDirectoryInClasspath>true</includeConfigurationDirectoryInClasspath>--> <! -- copy the src/main/resources directory to /src --> <! ConfigurationDirectory >conf</configurationDirectory> <! - the position of the corresponding system configuration files in the source code - > < configurationSourceDirectory > SRC/main/resources < / configurationSourceDirectory > <copyConfigurationDirectory>true</copyConfigurationDirectory>
<logsDirectory>logs</logsDirectory>
<daemons>
<daemon>
<id>${daemon-name}</id>
<mainClass>${daemon-mainClass}</mainClass>
<jvmSettings>
<initialMemorySize>${daemon-JAVA_Xmx}</initialMemorySize>
<maxMemorySize>${daemon-JAVA_Xmx}</maxMemorySize> <extraArguments> <! -- Note :if the value is empty the plugin will throw NullPointerException-->
<extraArgument>-Djava.wrapper=1 ${daemon-JAVA_OPS}</extraArgument> <extraArgument>-verbose:gc</extraArgument> <extraArgument>-XX:+PrintHeapAtGC</extraArgument> <extraArgument>-XX:+PrintGCDetails</extraArgument> <extraArgument>-XX:+PrintGCDateStamps</extraArgument> <extraArgument>-XX:+PrintGCTimeStamps</extraArgument> <extraArgument>-XX:+PrintTenuringDistribution</extraArgument> <extraArgument>-XX:+PrintGCApplicationStoppedTime</extraArgument> <extraArgument>-Xloggc:logs/gc.log</extraArgument> <extraArgument>-XX:+HeapDumpOnOutOfMemoryError</extraArgument> <extraArgument>-XX:HeapDumpPath=logs/gc.hprof</extraArgument> <extraArgument>-XX:ErrorFile=logs/java_error_%p.log</extraArgument> <! --<extraArgument>-Xbootclasspath/p:lib/alpn-boot-${alpn.boot.version}.jar</extraArgument>--> </extraArguments> </jvmSettings> <platforms> <platform>jsw</platform> </platforms> <generatorConfigurations> <generatorConfiguration> <generator>jsw</generator> <includes> <include>linux-x86-32</include> <include>linux-x86-64</include> <include>macosx-universal-64</include> <include>macosx-universal-32</include> <include>windows-x86-32</include> <include>windows-x86-64</include> </includes> <configuration> <property> <name>configuration.directory.in.classpath.first</name> <value>conf</value> </property> <property> <name>set.default.REPO_DIR</name> <value>lib</value> </property> <property> <name>wrapper.java.command</name> <value>%JAVA_HOME%/bin/java</value> </property> <property> <name>wrapper.logfile</name> <value>logs/wrapper.log</value> </property> <property> <name>wrapper.startup.timeout</name> <value>7200</value><! -- 2 hours --> </property> <property> <name>wrapper.ping.timeout</name> <value>3600</value><! </property> <property> <name> wrapper.ntService. name</name> <value>${wrapper.ntservice.name}</value>
</property>
<property>
<name>wrapper.ntservice.displayname</name>
<value>${wrapper.ntservice.displayname}</value>
</property>
<property>
<name>wrapper.ntservice.description</name>
<value>${wrapper.ntservice.description}</value>
</property>
<property>
<name>wrapper.console.title</name>
<value>${wrapper.ntservice.name}</value> </property> <property> <! Pidfile. Strict </name> <value>TRUE</value> </property> </configuration> </generatorConfiguration> </generatorConfigurations> </daemon> </daemons> </configuration> </execution> </executions> </plugin> </plugins> </build>Copy the code
Note: If you package manually, do not use the built-in plugin (install directive) of IDEA, sometimes there will be a pit
Write the execution script
-
Write execution scripts when the environment is ready
Create a test.sh script file and write the followingMake some modifications according to your own project)
#! /bin/sh
####### Variable Settings
Output interval identifier
DASH_LINE_TAG="-- -- -- -- -- -- -- -- -- -- -- --"
# Target machine
TARGET_SERVER="Fill in the IP address of the corresponding server"
# Target users
TARGET_USER=root
# Jenkins workspace directory
JENKINS_WORKSPACE="/${TARGET_USER}/.jenkins/workspace"
Make some adjustments according to the final package structure of my project, where data-server-test represents the name of the project you built in Jenkins, followed by the package path of the project
JENKINS_SOURCE_TARGET_HOME="${JENKINS_WORKSPACE}/ Jenkins build project name/package plugin package directory structure"
# change the wrapper. Conf JDK path for workspace
sed -i "% s \ \ % JAVA_HOME/bin/Java \ \ deploy machine the JDK location (for example/soft/jdk1.8 / Java) \ \ g" ${JENKINS_SOURCE_TARGET_HOME}/conf/wrapper.conf
# path where app is located
APP_HOME="/soft/data-server"
Back up the app directory
APP_BACKUP="/soft/Backup/"
# Backup file directory
CONF_BAKUP_HOME="/soft/perioperative-jenkins/data-server/conf_bak"
Execute file service name
SERVICE_NAME=data-server
######## Remote execution process
# stop service
echo "${DASH_LINE_TAG}Stop service:${APP_HOME}/bin/${SERVICE_NAME} stop"
ssh ${TARGET_USER}@${TARGET_SERVER} "${APP_HOME}/bin/${SERVICE_NAME} stop"
# backup file
echo "${DASH_LINE_TAG}Backup files to:${APP_BACKUP}"
ssh ${TARGET_USER}@${TARGET_SERVER} "rm -rf ${APP_BACKUP}data-server"
ssh ${TARGET_USER}@${TARGET_SERVER} "cp -rf ${APP_HOME}/ ${APP_BACKUP}"
Delete the original file
echo "${DASH_LINE_TAG}Delete the original file:${APP_HOME}"
ssh ${TARGET_USER}@${TARGET_SERVER} "rm -rf ${APP_HOME}/ *"
Copy the new file to the directory
echo "${DASH_LINE_TAG}Copy new file to directory:${JENKINS_SOURCE_TARGET_HOME} ---> ${TARGET_USER}@${TARGET_SERVER}:${APP_HOME}"
scp -r ${JENKINS_SOURCE_TARGET_HOME}/ *${TARGET_USER}@${TARGET_SERVER}:${APP_HOME}
This is the configuration file address you set. The configuration file will use different configuration files for different machines. Once it is set up, it will be automatically replaced when it is automatically packaged, so you don't need to manually change it every time you deploy
echo "${DASH_LINE_TAG}Copy the backup configuration file to a directory:${CONF_BAKUP_HOME}/conf"
ssh ${TARGET_USER}@${TARGET_SERVER} "cp -rf ${CONF_BAKUP_HOME}/ *${APP_HOME}/conf/"
# change execute file permissions
echo "${DASH_LINE_TAG}Change execution file permissions:${APP_HOME}/bin/"
ssh ${TARGET_USER}@${TARGET_SERVER} "chmod +x ${APP_HOME}/bin/*"
# start service
echo "${DASH_LINE_TAG}Start the service:${APP_HOME}/bin/${SERVICE_NAME} start"
ssh ${TARGET_USER}@${TARGET_SERVER} "${APP_HOME}/bin/${SERVICE_NAME} start"
Copy the code
Start the automatic deployment configuration
- Click New Task on the home page
- Enter the task name and select build a free-style software project
- Then perform related configurations
Add build steps after filling in the maven configuration and select Execute Shell
Five, the final operation
-
Very stressed
One step in the script is to copy Jenkins’ packaged local file to a remote server
Our remote server must have this folder, otherwise an error will prompt said can’t find the directory, Jenkins build will fail just an error, this time we can choose to manually create (behind only need time, need not), or optimize the above script, copy Jenkins packaged good file to the remote server before perform the corresponding operation Go ahead and deal with the directory