name: Build and Release

on:
  push:
    branches:
      - "rebuild-develop"   # develop分支
      - "rebuild-release"   # release 分支
    tags:
      - 'build-*'  # 打 tag 时跑 build + release
  workflow_dispatch: # 允许手动触发

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          distribution: temurin
          java-version: 17

      - name: Grant execute permission for Gradlew
        run: chmod +x gradlew

      - name: Build with Gradle
        run: ./gradlew clean build

      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: my-build
          path: '**/build/libs/*.jar'

  release:
    needs: build
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/build-')  # 只有打 tag 才执行 release
    steps:
      - name: Download build artifact
        uses: actions/download-artifact@v4
        with:
          name: my-build
          path: ./build-artifacts

      - name: Create GitHub Release
        id: create_release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ github.ref_name }}
          name: Release ${{ github.ref_name }}
          body: |
            自动发布所有子项目构建产物。
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload artifact to Release
        uses: softprops/action-gh-release@v1
        with:
          files: ./build-artifacts/**/*.jar
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
