转载

记录一次自己使用jenkins搭建自动部署

针对使用appassembler-maven-plugin打包插件对项目进行打包的项目,实现使用jenkins进行自动化部署

二 、基本环境准备

  • 搭建一个可用的jenkins

    搭建过程.......省略

    在jenkins中设置你要部署的服务器

    从主页面中进入到配置ssh服务器地址

  • 流程图

记录一次自己使用jenkins搭建自动部署
配置完毕以后,点击Test Configuration测试一下是否可以连接($/color{red}{注意:你要远程部署的服务器必须给当前jenkins所在的服务器开放互相信任}$)
复制代码
  • 在项目中配置使用打包插件appassembler-maven-plugin

<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> 
   
复制代码

注意: 如果手动打包的时候,不要使用idea内置的插件(install指令),有时候会有坑

三、书写执行脚本

  • 环境准备完毕以后书写相关执行脚本

    创建一个test.sh 脚本文件,写入下面内容( 根据自己项目进行一定的修改 )
#!/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"

复制代码
原文  https://juejin.im/post/5cf613ede51d45105d63a4c2
正文到此结束
Loading...