美文网首页
Jenkins构建发布任务

Jenkins构建发布任务

作者: Habit_1027 | 来源:发表于2021-06-09 11:48 被阅读0次

    一、新建一个项目

    image.png

    项目名称随意

    image.png image.png image.png image.png image.png

    调用maven命令

    image.png image.png

    二、部署java服务器,做jenkins打包上线

    [root@java-server ~]# mkdir -p /data/application
    上传jdk   (放在/usr/local 下面也可以 后面记得改)
    [root@java-server ~]# tar xzf jdk-8u191-linux-x64.tar.gz -C /usr/local/
    [root@java-server ~]# cd /usr/local/
    [root@java-server local]# mv jdk1.8.0_191/ java
    下载tomcat
    [root@java-server ~]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.42/bin/apache-tomcat-8.5.42.tar.gz
    [root@java-server ~]# tar xzf apache-tomcat-8.5.42.tar.gz -C /data/application/
    [root@java-server ~]# cd /data/application/
    [root@java-server application]# mv apache-tomcat-8.5.42/ tomcat
    设置环境变量
    [root@java-server ~]# vim /etc/profile
    export JAVA_HOME=/usr/local/java
    export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
    export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
    export TOMCAT_HOME=/data/application/tomcat
    [root@java-server ~]# source /etc/profile
    测试:
    [root@java-server ~]# java -version 
    java version "1.8.0_191"
    Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
    Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
    删除tomcat默认发布目录下面的内容:
    [root@java-server ~]# rm -rf /data/application/tomcat/webapps/*
    [root@java-server ~]# cd /data/application/tomcat/webapps/
    [root@java-server webapps]# ls
    创建目录和脚本:
    [root@java-server ~]# mkdir /opt/script  #创建脚本目录
    [root@java-server ~]# vim app-jenkins.sh   #创建脚本
    脚本内容在后面
    [root@java-server ~]# chmod +x app-jenkins.sh  #添加执行权限
    [root@java-server ~]# mv app-jenkins.sh /opt/script/
    

    三、回到jenkins页面开始构建任务

    image.png image.png image.png image.png image.png

    四、登陆java-server服务器查看

    image.png

    脚本内容

    [root@java-server script]# cat app-jenkins.sh 
    #!/usr/bin/bash
    #本脚本适用于jenkins持续集成,实现备份war包到代码更新上线!使用时请注意全局变量。
    #================
    #Defining variables
    export JAVA_HOME=/usr/local/
    export TOMCAT_HOME=/usr/local/tomcat
    webapp_path="/usr/local/tomcat/webapps"
    tomcat_run="/usr/local/tomcat/bin"
    updata_path="/data/update/`date +%F-%T`"
    backup_path="/data/backup/`date +%F-%T`"
    tomcat_pid=`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`
    files_dir="easy-springmvc-maven"
    files="easy-springmvc-maven.war"
    job_path="/root/upload"
    
    #Preparation environment
    echo "Creating related directory"
    mkdir -p $updata_path
    mkdir -p $backup_path
    
    echo "Move the uploaded war package to the update directory"
    mv $job_path/$files $updata_path
    
    echo "========================================================="
    cd /opt
    echo "Backing up java project"
    if [ -f $webapp_path/$files ];then
        tar czf $backup_path/`date +%F-%H`.tar.gz $webapp_path
        if [ $? -ne 0 ];then
            echo "打包失败,自动退出"
            exit 1
        else
            echo "Checking if tomcat is started"
            if [ -n "$tomcat_pid" ];then
                kill -9 $tomcat_pid
                if [ $? -ne 0 ];then
                    echo "tomcat关闭失败,将会自动退出"
                    exit 2
                fi
            fi
            cd $webapp_path
            rm -rf $files && rm -rf $files_dir
            cp $updata_path/$files $webapp_path
            cd /opt
            $tomcat_run/startup.sh
            sleep 5
            echo "显示tomcat的pid"
            echo "`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`"
            echo "tomcat startup"
            echo "请手动查看tomcat日志。脚本将会自动退出"
        fi
    else
        echo "Checking if tomcat is started"
            if [ -n "$tomcat_pid" ];then
                kill -9 $tomcat_pid
                    if [ $? -ne 0 ];then
                        echo "tomcat关闭失败,将会自动退出"
                            exit 2
                    fi
            fi
        cp $updata_path/$files $webapp_path
        $tomcat_run/startup.sh
            sleep 5
            echo "显示tomcat的pid"
            echo "`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`"
            echo "tomcat startup"
            echo "请手动查看tomcat日志。脚本将会自动退出"
    fi
    

    思路:Jenkins 构建过程
    1.从github拉取代码 --->/root/.jenkins/workspace
    2.build--->jenkins /usr/local/java/maven构建下载好的java代码
    3.通过ssh 将打包好的 war包 拷贝到后端服务器(例如java/root/upload/war
    4.通过后端服务器的app-jenkins脚本 将原来的代码进行备份 ,最后重新启动tomcat

    相关文章

      网友评论

          本文标题:Jenkins构建发布任务

          本文链接:https://www.haomeiwen.com/subject/cyqeeltx.html