使用Github Action来自动化构建Maven项目

简介

本文主要介绍如何把Github仓库中的Maven项目通过Github Action进行自动化构建后生成Jar包,接着拷贝到服务器进行部署。

服务器脚本

  • 脚本application.sh用于启停应用:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    #!/bin/bash

    # application jar's name
    appDir=~/java_deploy
    application=$(find $appDir -type f -name "*.jar")

    ## determine existence of processus of application
    isExist(){
    ## check pid
    pid=`ps -ef | grep ${application} | grep -v grep | awk '{print $2}'`
    ## if pid not exist return 0 else 1
    if [ -z "${pid}" ]; then
    return 0
    else
    return 1
    fi
    }

    log=$appDir/application.log

    ## start application in background and generate appDir/application.log
    function start()
    {
    ## deermine existence of processus
    isExist
    ## if not running
    if [ $? -eq "0" ]; then
    echo "Your ${application} is not running"
    nohup java -jar $application --spring.profiles.active=test > $log &
    echo "${application} startup success"
    else
    echo "${application} is running, pid=${pid} "
    fi
    }

    ## stop the processus application
    function stop()
    {
    isExist
    ## if not exist - ok
    if [ $? -eq "0" ]; then
    echo "${application} is not running"
    else
    echo "${application} is running, pid=${pid}, prepare kill it "
    # if exist - kill the processus
    kill -9 ${pid}
    echo "${application} has been successfully killed"
    fi
    }

    ## check status
    function status()
    {
    appPid=`ps -ef |grep java|grep $application |awk '{print $2}'`
    if [ -z $appPid ]; then
    echo -e "Not running "
    else
    echo -e "Running [$appPid] "
    fi
    }

    ## restart
    function restart(){
    stop
    start
    }

    ## arg
    case "$1" in
    "start")
    start
    ;;
    "stop")
    stop
    ;;
    "restart")
    restart
    ;;
    "status")
    status
    ;;
    *)
    echo "please enter the correct commands: "
    echo "such as : sh application.sh [ start | stop | restart |status ]"
    ;;
    esac

配置仓库Setting –> Secrets

Key Value
HOST 你的服务器地址
USERNAME SSH连接用户名
PASSWORD SSH连接密码
PORT SSH端口

Github Action

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
name: Java CI with Maven & Deploy

on:
push:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup java
uses: actions/setup-java@v2
with:
java-version: '8'
distribution: 'adopt'
- name: Build with Maven
run: mvn clean package -Dmaven.test.skip=true
- name: Deploy to Server
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
port: ${{ secrets.PORT }}
source: "your project path/*.jar"
target: "target path"
strip_components: 2
- name: Run SSH Command
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
port: ${{ secrets.PORT }}
script: "your path/application.sh restart"
作者

VGEAREN

发布于

2021-07-10

更新于

2022-01-27

许可协议

评论