Skip to content

使用GitHub Action自动构建部署项目

Elm Forest edited this page Sep 15, 2023 · 2 revisions

当Push新代码到仓库时,GitHub Action会帮您自动构建jar并部署到您的服务器上

创建私有文件仓库

您需要创建和配置该springboot项目生产环境下的application-pro.yml文件,

git clone https://github.com/Elm-Forest/Dino-Office.git
cd ./Dino-Office/OA-web/src/main/resources
# 此处修改或创建springboot配置文件 #

这里有一个配置文件的参考demo
然后创建一个空白的私有仓库,建议取名为application,然后将该application-pro.yml配置文件push到该仓库

创建Workflow

现在,返回您的Dino-Office项目仓库,创建以下文件夹

cd ./.github
mkdir workflow
cd ./workflow

创建deploy.yml文件,将以下内容粘贴进去:

name: Deploy

on:
  push:
    branches: [ "master" ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'

      - name: Checkout Master
        uses: actions/checkout@v3.3.0
        with:
          ref: master
          path: Dino-Office

      - name: Checkout Private Repository
        uses: actions/checkout@v3.3.0
        with:
          # 这里修改为你自己的存储私有文件的私有仓库
          repository: 你的github用户名/私有仓库名称
          ref: master
          token: ${{ secrets.GIT_TOKEN }}
          path: application

      - name: Copy application.yml
        run: |
          cp application/application-pro.yml Dino-Office/OA-web/src/main/resources/

      - name: Package with Maven
        run: |
          cd Auto-Office
          mvn package -f pom.xml

      - name: Kill application
        uses: appleboy/ssh-action@v0.1.7
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USERNAME }}
          password: ${{ secrets.SERVER_PASSWORD }}
          script: |
            sh /usr/local/dino-kill.sh

      - name: Deploy to server
        uses: appleboy/scp-action@v0.1.3
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USERNAME }}
          password: ${{ secrets.SERVER_PASSWORD }}
          source: Dino-Office/OA-web/target/OA-web-0.0.1.jar
          target: /usr/local/server

      - name: Start application
        uses: appleboy/ssh-action@v0.1.7
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USERNAME }}
          password: ${{ secrets.SERVER_PASSWORD }}
          script: |
            sh /usr/local/dino-start.sh

配置Actions secrets

在您的Dino-Office项目仓库里,依次点击Setting -> Secrets and variables -> Actions
点击New repository secret,分别创建如下四个secrets:

  • GIT_TOKEN,您的GitHub Access Token,访问Access Token进行创建。仅仅勾选Public Repositories即可,除非您的项目仓库是私有的
  • SERVER_HOST: 服务器的公网地址
  • SERVER_USERNAME: 服务器的登录用户名
  • SERVER_PASSWORD: 服务器的登录密码

配置服务器

接下来,您需要对服务器进行配置

首先确保服务器上已经安装了JDK版本为17及以上的环境,并配置好了环境变量,您可以使用以下命令进行查看:

java --version

您还需要在服务器上安装screen,您可能已经安装,请使用以下命令进行确认:

screen -ls

若出现screen: command not found,则说明没有安装screen

如果您使用的是CentOS系统,可用以下命令安装screen

yum install screen 

如果您使用的是Debian/Ubuntu系统,可用以下命令安装screen:

apt-get install screen

接下来,键入cd /usr/local,创建文件dino-start.sh

screen_name="Dino-Office"

# Check if screen is running
if screen -ls | grep -q "$screen_name"; then
  # Kill the screen if running
  screen -X -S "$screen_name" quit
  echo "Screen '$screen_name' was killed."
  screen -S Dino-Office -d -m java -jar /usr/local/OA-web-0.0.1.jar
  echo "Screen '$screen_name' start"

else
  echo "Screen '$screen_name' was not found."
  screen -S Dino-Office -d -m java -jar /usr/local/OA-web-0.0.1.jar
  echo "Screen '$screen_name' start"
fi

然后创建文件dino-kill.sh

screen_name="Dino-Office"

# Check if screen is running
if screen -ls | grep -q "$screen_name"; then
  # Kill the screen if running
  screen -X -S "$screen_name" quit
  echo "Screen '$screen_name' was killed."
else
  echo "Screen '$screen_name' was not found."
fi

Enjoy a cup of coffee

end