jenkins 部署例子 git

jenkins 所需要插件及下载地址:

  1. scm-api:scm-api.hpi(基础插件)
  2. git:git.hpigit-client:[git-client.hpi]
  3. git-client:(https://updates.jenkins-ci.org/download/plugins/git-client/)
  4. deploy:deploy.hpi(远程部署插件)

1.本地部署 jar

1.项目类型:maven 项目,jar 包启动 pom.xml 文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.dada.test</groupId>
    <artifactId>jarTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jarTest</name>
    <description>jenkinsTest</description>

    <build>
        <finalName>jarTest</finalName>

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <target>1.7</target>
                        <archive>
                            <manifest>
                                <mainClass>com.test.HelloWorld</mainClass>
                                <addClasspath>true</addClasspath>
                                <classpathPrefix>lib/</classpathPrefix>
                            </manifest>

                        </archive>
                        <classesDirectory>
                        </classesDirectory>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

2.jenkins 项目配置部署截图

3.Excute shell

#!/bin/bash 
export JAVA_HOME=/opt/tools/jdk1.7.0_67
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export BUILD_ID=dontKillMe

# 项目名称
project_name=jarTest
# jar 名称
jar_name=jarTest.jar
# maven 生成 jar 存放位置
file_path=~/.jenkins/jobs/$project_name/workspace/target
# 启动 jar 存放位置
run_path=/opt/tools/jarTest

# 1.删除进程
echo "kill process java -jar ${jar_name}"
ps -ef | grep ${jar_name} | grep -v grep | awk  '{print $2}' | xargs  kill -9

# 2.删除原来的 jar
echo "delete jar ${jar_name}"
rm -rf $run_path/${jar_name}

# 3.拷贝最新的 jar 过去
echo "copy ${file_path}/${jar_name} to ${run_path}"

cp $file_path/$jar_name $run_path

# 4.启动 jar
cd $run_path
nohup java -jar $jar_name 2>&1 &

pid=$(ps aux | grep "jarTest" | awk '{print $2}' | sort -n | head -n 1)

echo "ssh command is running, pid:${pid}" 

exit 0

2.远程部署 jar 包

1.同【本地部署 jar 包】
2.同【本地部署jar 包】
3. Excute shell

#!/bin/bash 
export BUILD_ID=dontKillMe

# 项目名称
project_name=remoteJar
# jar 名称
jar_name=jarTest.jar
# jar 存放位置
file_path=~/.jenkins/jobs/$project_name/workspace/target
# 启动 jar 存放位置
run_path=/opt/tools/jarTest
# 远程机器ip
remote_host=192.168.100.16

# 1.删除进程
echo "kill process java -jar ${jar_name}"
# 获取远程进程号
pid=$(ssh root@$remote_host "ps -ef | grep ${jar_name} | grep -v grep | awk '{print $2}'")|awk '{print $2}'
ssh root@$remote_host "kill -9 ${pid}"


# 2.删除原来的 jar
echo "delete jar ${jar_name}"
ssh root@$remote_host "rm -rf ${run_path}/${jar_name}"

# 3.拷贝最新的 jar 过去
echo "copy ${file_path}/${jar_name} to ${remote_host}:${run_path}"

scp $file_path/$jar_name root@$remote_host:${run_path}

# 4.启动 jar
ssh -f -n -l root $remote_host "export JAVA_HOME=/opt/tools/jdk1.7.0_67;export PATH=$JAVA_HOME/bin:$PATH;cd /opt/tools/jarTest;nohup java -jar jarTest.jar 2>&1 &"

# 5.获取进程号
pid=$(ps aux | grep "startJar.sh" | awk '{print $2}' | sort -n | head -n 1)


# 6.杀掉这个进程
echo "ssh command is running, pid:${pid}" 
sleep 3 && kill -9 ${pid} && echo "ssh command is complete" # 延迟3秒后执行kill命令,关闭ssh进程,延迟时间可以根据调用的命令不同调整 



exit 0

对于复杂一些的 jar 包部署,可能只是通过 Excute shell 来执行会比较麻烦,这时候我们可以在远程服务器上创建一个 启动脚本,然后在 Excute shell 脚本中写简单的 shell,把大部分的逻辑放在远程的 shell 脚本中。截图如下:

Excute shell 脚本:

#!/bin/bash 
export BUILD_ID=dontKillMe

# 项目名称
project_name=ync-sms
# jar 名称
jar_name=ync-sms-*.jar
# jar 存放位置
file_path=~/.jenkins/jobs/$project_name/workspace/target
# 启动 jar 存放位置
run_path=/opt/tools/ync-sms
lib_path=lib
# 远程机器ip
remote_host=192.168.100.16


# 2.删除原来的 jar
echo "delete jar ${jar_name}"
ssh root@$remote_host "rm -rf ${run_path}/${jar_name}"

# 删除 lib 目录
echo "delete jar ${run_path}/lib/*"
ssh root@$remote_host "rm -rf ${run_path}/${lib_path}/*"

# 3.拷贝最新的 jar 过去
echo "copy ${file_path}/${jar_name} to ${remote_host}:${run_path}"
scp $file_path/$jar_name root@$remote_host:${run_path}

# 拷贝最新的 lib 过去
echo "copy ${file_path}/${lib_path} to ${remote_host}:${lib_path}"
scp -r $file_path/$lib_path/* root@$remote_host:$run_path/$lib_path

# 4.启动 jar
ssh root@$remote_host "cd /opt/tools;./serviceRestart.sh /opt/tools/ync-sms ync-sms rd"

exit 0

远程机器上的 shell 脚本 serviceRestart.sh:

#!/bin/sh 
## java env
source /etc/profile
export JAVA_HOME=/usr/local/jdk1.7.0_75
export JRE_HOME=$JAVA_HOME/jre

if [ $# != 3 ] ; then 
echo "USAGE: $0 SERVICE_DIR SERVICE_NAME_PRE DISCONF_ENV" 
echo " e.g.: $0 /opt/dubbo-test dubbo-goods rd" 
exit 1; 
fi 
SERVICE_DIR=$1
SERVICE_NAME_PRE=$2


#设置 disconf 信息
DISCONF_ENV=$3
DISCONF_HOST=192.168.100.15:8015
DISCONF_APP=ync365
DISCONF_VERSION=1.0.0

echo "SERVICE_DIR" $SERVICE_DIR
echo "SERVICE_NAME_PRE" $SERVICE_NAME_PRE
echo "DISCONF_ENV" $DISCONF_ENV
echo "DISCONF_HOST" $DISCONF_HOST
echo "DISCONF_APP" $DISCONF_APP
echo "DISCONF_VERSION" $DISCONF_VERSION
cd $SERVICE_DIR

# P_ID=`ps -ef | grep -w "$SERVICE_NAME_PRE" | grep -v "grep" | awk '{print $2}'`
    P_ID=`jps -ml|grep "$SERVICE_NAME_PRE"|awk '{print $1}'` 
        if [ "$P_ID" == "" ]; then
            echo "=== $SERVICE_NAME process not exists or stop success"
        else
            echo "=== begin kill $SERVICE_NAME process, pid is:$P_ID"
            kill -9 $P_ID
       fi

    sleep 3

    JAR_NAME=`ls -rt *.jar|grep -v 'sources'|tail -1`

    echo "START JAR_NAME "$JAR_NAME
    nohup $JRE_HOME/bin/java -Xms256m -Xmx512m -jar   -Ddisconf.env=$DISCONF_ENV -Ddisconf.conf_server_host=$DISCONF_HOST -Ddisconf.app=$DISCONF_APP -Ddisconf.version=$DISCONF_VERSION -Ddisconf.user_define_download_dir=.classes -Ddisconf.enable.remote.conf=true          $JAR_NAME >$SERVICE_NAME_PRE.log 2>&1 &

    ls *.jar|grep -v $JAR_NAME|xargs rm -rf
exit 0

3.本地部署 web 项目

1.项目类型:maven 项目,war 包启动 pom.xml 文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.springframework.samples.service.service</groupId>
  <artifactId>webTest</artifactId>
  <version>0.1</version>
  <packaging>war</packaging>

    <properties>

        <!-- Generic properties -->
        <java.version>1.6</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Web -->
        <jsp.version>2.2</jsp.version>
        <jstl.version>1.2</jstl.version>
        <servlet.version>2.5</servlet.version>


        <!-- Spring -->
        <spring-framework.version>3.2.3.RELEASE</spring-framework.version>

        <!-- Hibernate / JPA -->
        <hibernate.version>4.2.1.Final</hibernate.version>

        <!-- Logging -->
        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.5</slf4j.version>

        <!-- Test -->
        <junit.version>4.11</junit.version>

    </properties>

    <dependencies>

        <!-- Spring MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- Other Web dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Spring and Transactions -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- Logging with SLF4J & LogBack -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
            <scope>runtime</scope>
        </dependency>

        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>


        <!-- Test Artifacts -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-framework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies> 

    <build>
        <finalName>webTest</finalName>
        <plugins>
            <!-- 修改过滤器的占位符 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                    <delimiters>
                        <delimiter>$[*]</delimiter>
                    </delimiters>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>${java-version}</source>
                    <target>${java-version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <compilerArguments>
                        <extdirs>src\main\webapp\WEB-INF\lib</extdirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <!-- jar打包相关插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
        <defaultGoal>install</defaultGoal>
    </build>

</project>

2.jenkins 项目配置截图:

3.Excute shell:

#!/bin/bash 
#copy file and restart tomcat 

export JAVA_HOME=/opt/tools/jdk1.7.0_67
export CATALINA_HOME=/opt/tools/apache-tomcat-7.0.70
export BUILD_ID=dontKillMe

tomcat_path=/opt/tools/apache-tomcat-7.0.70
war_name=webTest.war
file_path=~/.jenkins/jobs/test/workspace/target

$tomcat_path/bin/shutdown.sh 

sleep 3s 

echo "rm -rf ${tomcat_path}/webapps/ROOT/*"

rm -rf ${tomcat_path}/webapps/ROOT/*

cd $file_path

cp ${war_name} ${tomcat_path}/webapps/ROOT/

cd $tomcat_path/webapps/ROOT/

unzip ${war_name}

rm -rf ${war_name}

sleep 5s 


cd $tomcat_path/bin/
./startup.sh

echo "server restarted"

4.远程部署 war 包(shell 方式)

1.项目类型:maven 项目,war 包启动 pom.xml 文件【同上】
2.部署截图:


3.Excute shell:

#!/bin/bash 
#1.初始化变量

#项目名称
project_name=ync-fertilizer-web
#tomcat 路径
tomcat_path=/opt/tools/tomcat-8081
#tomcat 名称
tomcat_name=tomcat-8081
#war 名称
war_name=ync-fertilizer-web
#打包 war 路径
file_path=~/.jenkins/jobs/$project_name/workspace/target
remote_host=192.168.100.16


#2.删除远程进程
echo "kill process java -jar ${tomcat_name}"

#获取远程的进程 id 号
# ssh root@$remote_host "ps -ef | grep ${tomcat_name} | grep -v grep | awk '{print $2}' | xargs kill -9"
# 注意这种远程执行查询的操作返回的是整个的进程的结果,而不是进程号,所以为了要达到杀掉进程的目的,必须要再对返回的结果进行过滤

#root 7314 1 5 09:51 pts/0 00:00:18 /opt/tools/jdk1.7.0_67/bin/java -Djava.util.logging.config.file=/opt/tools/apache-tomcat-7.0.70/conf/logging.properties 
#-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.endorsed.dirs=/opt/tools/apache-tomcat-7.0.70/endorsed 
#-classpath /opt/tools/apache-tomcat-7.0.70/bin/bootstrap.jar:/opt/tools/apache-tomcat-7.0.70/bin/tomcat-juli.jar -Dcatalina.base=/opt/tools/apache-tomcat-7.0.70 
#-Dcatalina.home=/opt/tools/apache-tomcat-7.0.70 -Djava.io.tmpdir=/opt/tools/apache-tomcat-7.0.70/temp org.apache.catalina.startup.Bootstrap start
pid=$(ssh root@$remote_host "ps -ef | grep ${tomcat_name} | grep -v grep | awk '{print $2}'"| awk '{print $2}')
#远程杀掉进程
ssh root@$remote_host "kill -9 ${pid}"

#3.删除远程文件
echo "rm -rf ${tomcat_path}/webapps/${war_name}*"
ssh root@$remote_host "rm -rf ${tomcat_path}/webapps/${war_name}*"

#4.拷贝文件到远程
scp $file_path/$war_name.war root@$remote_host:$tomcat_path/webapps

#5.启动进程
ssh root@$remote_host "export JAVA_HOME=/opt/tools/jdk1.7.0_67;${tomcat_path}/bin/startup.sh"

#6.输出结果

echo "server restarted"

5.远程部署 war 包,jenkins 插件形式:注意 container 的 url 必须是 httt://ip:port 的格式,去掉 http 是不行的

1.项目类型:maven 项目,war 包启动 pom.xml 文件【同上】
2.部署截图:注意这个里面需要配置被部署到的 tomcat 的信息,需要在 conf/tomcat-users.xml 文件中添加如下内容:

<tomcat-users>
<role rolename="manager"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="admin"/>
<user username="admin" password="admin" roles="admin,manager,manager-script,manager-gui"/>

</tomcat-users>

3.Excute shell:无

6.注意点:

1.从上面的配置可以看出来,对于 jenkins 管理 git 项目进行发布只需要 3 个插件: 1. scm-api(基础插件)、git、git-client。
2.对于远程机器的操作需要首先做到服务器之间的 ssh 免密码登录认证:ssh-kegen -t rsa
3.对于远程机器的操作的时候,在执行 java 命令之前需要首先把 java 的环境变量输出一遍
4.所有的配置命令都可以直接通过 Excute shell 来实现,而不需要在远程机器上新建 shell 文件,之后再调用。
5.尽管通过 deploy 插件可以直接部署 web 项目但是不推荐这么做,因为 tomcat 有时候无法通过 bin/shutdown.sh 来停止。
6.如果项目之间有依赖关系那么需要配置他们的依赖如下图(在此项目编译之前先编译其他项目),只要你输入项目名称自然会提示出目前的项目名称的:

你可能感兴趣的:(jenkins 部署例子 git)