Loading

Gradle项目实现自动构建、部署项目

build.gradle

plugins {
    id 'java'
    id "com.github.spotbugs" version "3.0.0"
    id "org.hidetake.ssh" version "2.10.1"
}

remotes {
    webServer {
        host = '192.168.10.10'
        port = 22
        user = 'kim'
        password = '123'
    }
}

task showPlatformVersion {
    doLast {
        println ssh.version
        println bootJar {}.getArchiveFile().get().toString()
        ssh.run {
            session(remotes.webServer) {
                execute 'uname -a'
                execute 'cat /etc/*-release', ignoreError: true
            }
        }
    }
}
test {
    enabled = false
}

spotbugsMain {
    enabled = false
}

task deploy(dependsOn: bootJar) {
    doLast {
        ssh.run {
            session(remotes.webServer) {
                put from: bootJar {}.getArchiveFile().get().toString(), into: '/usr/local/app/jars/deployFile/'
                execute 'sh /usr/local/app/jars/sh/app.sh'
            }
        }
    }
}

app.sh

#启动包路径
PROJECT_PATH=/usr/local/app/jars
#启动包
PROJECT_NAME=app.jar
#备份路径
ARCHIVE_PROJECT_PATH=/usr/local/app/jars/archive
#上传包
ORIGIN_PROJECT_PATH=/usr/local/app/jars/deployFile
#日志路径
LOG_PATH=/dev/null


cp -f $PROJECT_PATH/$PROJECT_NAME  $ARCHIVE_PROJECT_PATH &
cp -f $ORIGIN_PROJECT_PATH/$PROJECT_NAME  $PROJECT_PATH  & 

sleep 8s

PID=$(ps -ef | grep $PROJECT_NAME | grep -v grep | awk '{ print $2 }')
if [ -z "$PID" ]
then
    echo Application is already stopped
else
    echo kill $PID
    kill -9 $PID
fi 

sleep 1

#清空日志
echo "">$LOG_PATH

cd $PROJECT_PATH
nohup /usr/local/app/jdk/bin/java -jar -Xms512M -Xmx2048M -Dspring.profiles.active=41 -Drocketmq.client.logRoot=/usr/local/app/jars/logs/rocket_app -Drocketmq.client.logLevel=warn $PROJECT_NAME >/dev/null 2>&1 &
echo Start Success!
posted @ 2021-01-13 11:06  元宝爸爸  阅读(576)  评论(0编辑  收藏  举报