name: Build and Deploy Spring Boot

on:
  push:
    branches: [ master ]  # 可以根据需要修改分支名
  workflow_dispatch:    # 允许手动触发

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest

    steps:
      # 检出代码
      - uses: actions/checkout@v4

      # 设置JDK环境
      - name: Set up JDK 8
        uses: actions/setup-java@v3    # 使用最新的 v3 版本
        with:
          java-version: '8'
          distribution: 'temurin'      # 或者使用 'adopt', 'zulu' 等其他发行版
          cache: 'maven'               # 自动缓存 Maven 依赖
          maven-version: '3.8.6'       # 指定 Maven 版本

      # 缓存Maven依赖
      - name: Cache Maven packages
        uses: actions/cache@v2
        with:
          path: ~/.m2
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2

      # 使用Maven打包
      - name: Build with Maven
        run: mvn clean package -DskipTests

      # 将JAR包传输到服务器并部署
      - name: Deploy to Server
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USERNAME }}
          password: ${{ secrets.SERVER_PASSWORD }}
          port: ${{ secrets.SERVER_PORT }}
          source: "target/*.jar"
          target: "/home/tools/testActions"