First, the use scene
For use appassembler-maven-plugin package plug-in projects packaged project, implemented using automated deployment jenkins
Second, the basic environment ready
-
Build jenkins available
Build process ....... omitted
Set the server you want to deploy in the jenkins
Enter from the main page to configure ssh server address
-
flow chart
配置完毕以后,点击Test Configuration测试一下是否可以连接($\color{red}{注意:你要远程部署的服务器必须给当前jenkins所在的服务器开放互相信任}$)
复制代码
-
Configuration using packaged plug-appassembler-maven-plugin in the project
<build>
<plugins>
<!-- 目前采用wrapper打包部署 -->
<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>
<!--源代码中对应的系统配置文件的位置-->
<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小时 -->
</property>
<property>
<name>wrapper.ping.timeout</name>
<value>3600</value><!-- 1小时 -->
</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>
<!-- 如果pid文件已经存在则不启动程序 -->
<name>wrapper.pidfile.strict</name>
<value>TRUE</value>
</property>
</configuration>
</generatorConfiguration>
</generatorConfigurations>
</daemon>
</daemons>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
复制代码
Note : If you manually packaged, do not use the built-in plug-in idea (install instructions), and sometimes there will be pit
Third, script writing execution
-
After writing the relevant environment is ready to execute the script
Test.sh create a script file, write the following content ( to be modified according to your project )
#!/bin/sh
#######变量设置
#输出间隔标识
DASH_LINE_TAG="------------"
#目标机器
TARGET_SERVER="192.168.6.225"
#目标用户
TARGET_USER=root
#jenkins的workspace目录
JENKINS_WORKSPACE="/${TARGET_USER}/.jenkins/workspace"
#打包出来的目录地址 详细的修改根据自己项目的最终打包出来的结构做一定调整 其中data-server-test表示的你在jenkins中构建项目的名称,后面才是项目打包的路径
JENKINS_SOURCE_TARGET_HOME="${JENKINS_WORKSPACE}/data-server-test/data-server/target/generated-resources/appassembler/jsw/data-server"
#修改workspace对应项目conf下的wrapper.conf jdk路径
sed -i "s\\%JAVA_HOME%/bin/java\\/soft/dorcst/environment/jdk8/bin/java\\g" ${JENKINS_SOURCE_TARGET_HOME}/conf/wrapper.conf
#app所在路径
APP_HOME="/soft/data-server"
#备份app的目录
APP_BACKUP="/soft/Backup/"
#备份文件所在目录
CONF_BAKUP_HOME="/soft/perioperative-jenkins/data-server/conf_bak"
#执行文件服务名
SERVICE_NAME=data-server
########远程执行流程
#停止服务
echo "${DASH_LINE_TAG}停止服务: ${APP_HOME}/bin/${SERVICE_NAME} stop"
ssh ${TARGET_USER}@${TARGET_SERVER} "${APP_HOME}/bin/${SERVICE_NAME} stop"
#备份文件
echo "${DASH_LINE_TAG}备份文件到: ${APP_BACKUP}"
ssh ${TARGET_USER}@${TARGET_SERVER} "rm -rf ${APP_BACKUP}data-server"
ssh ${TARGET_USER}@${TARGET_SERVER} "cp -rf ${APP_HOME}/ ${APP_BACKUP}"
#删除原文件
echo "${DASH_LINE_TAG}删除原文件:${APP_HOME}"
ssh ${TARGET_USER}@${TARGET_SERVER} "rm -rf ${APP_HOME}/*"
#复制新文件到目录
echo "${DASH_LINE_TAG}复制新文件到目录:${JENKINS_SOURCE_TARGET_HOME} ---> ${TARGET_USER}@${TARGET_SERVER}:${APP_HOME}"
scp -r ${JENKINS_SOURCE_TARGET_HOME}/* ${TARGET_USER}@${TARGET_SERVER}:${APP_HOME}
#复制备份的配置文件到目录 这是你设置的配置文件地址,配置文件会根据部署不同机器使用不同的配置文件,一开始设置好,自动打包的时候自动替换,就不用每次部署都手动修改了
echo "${DASH_LINE_TAG}复制备份的配置文件到目录: ${CONF_BAKUP_HOME}/conf"
ssh ${TARGET_USER}@${TARGET_SERVER} "cp -rf ${CONF_BAKUP_HOME}/* ${APP_HOME}/conf/"
#更改执行文件权限
echo "${DASH_LINE_TAG}更改执行文件权限:${APP_HOME}/bin/"
ssh ${TARGET_USER}@${TARGET_SERVER} "chmod +x ${APP_HOME}/bin/*"
#启动服务
echo "${DASH_LINE_TAG}启动服务: ${APP_HOME}/bin/${SERVICE_NAME} start"
ssh ${TARGET_USER}@${TARGET_SERVER} "${APP_HOME}/bin/${SERVICE_NAME} start"
复制代码
Fourth, start the automatic deployment configuration
- Click on the home page of the New Task
- Enter the task name and select the software project to build a free-style
- Then perform configuration
Fill out the relevant configuration maven later add build steps, you can run shell
Fifth, and finally operation
-
Very stressed
In the script step is to jenkins packaged local file, copy to a remote server
Our remote server must have this folder, otherwise it will prompt an error that can not find this directory, jenkins build error will fail this time we can choose to manually create (only once, do not back up), or optimization of the above script, jenkins packaged files to perform the corresponding operation before the remote server to handle this directory