# Release 发行版本部署
## 多端部署
name: Build Release

on:
  push:
    tags:
      - v*

jobs:
  # Windows
  build-windows:
    name: Build for Windows
    runs-on: windows-latest
    timeout-minutes: 30
    steps:
      # 检出 Git 仓库
      - name: Check out git repository
        uses: actions/checkout@v4
      # 安装 Node.js
      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20.x"
      # 复制环境变量文件
      - name: Copy .env.example
        run: |
          if (-not (Test-Path .env)) {
            Copy-Item .env.example .env
          } else {
            Write-Host ".env file already exists. Skipping the copy step."
          }
      # 安装项目依赖
      - name: Install Dependencies
        run: npm install
      # 构建 Electron App
      - name: Build Electron App for Windows
        run: npm run build:win || true
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
      # 上传构建产物
      - name: Upload Windows artifact
        uses: actions/upload-artifact@v4
        with:
          name: SPlarer-Win
          if-no-files-found: ignore
          path: dist/*.*
      # 创建 GitHub Release
      - name: Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/v')
        with:
          draft: false
          files: dist/*.*
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
  # Mac
  build-macos:
    name: Build for macOS
    runs-on: macos-latest
    timeout-minutes: 30
    steps:
      # 检出 Git 仓库
      - name: Check out git repository
        uses: actions/checkout@v4
      # 安装 Node.js
      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20.x"
      # 复制环境变量文件
      - name: Copy .env.example
        run: |
          if [ ! -f .env ]; then
            cp .env.example .env
          else
            echo ".env file already exists. Skipping the copy step."
          fi
      # 安装项目依赖
      - name: Install Dependencies
        run: npm install
      # 构建 Electron App
      - name: Build Electron App for macOS
        run: npm run build:mac || true
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
      # 上传构建产物
      - name: Upload macOS artifact
        uses: actions/upload-artifact@v4
        with:
          name: SPlarer-Macos
          if-no-files-found: ignore
          path: dist/*.*
      # 创建 GitHub Release
      - name: Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/v')
        with:
          draft: false
          files: dist/*.*
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
  # Linux
  build-linux:
    name: Build for Linux
    runs-on: ubuntu-22.04
    timeout-minutes: 30
    steps:
      # 检出 Git 仓库
      - name: Check out git repository
        uses: actions/checkout@v4
      # 安装 Node.js
      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "20.x"
      # 更新 Ubuntu 软件源
      - name: Ubuntu Update with sudo
        run: sudo apt-get update
      # 安装依赖
      - name: Install RPM & Pacman
        run: |
          sudo apt-get install --no-install-recommends -y rpm &&
          sudo apt-get install --no-install-recommends -y libarchive-tools &&
          sudo apt-get install --no-install-recommends -y libopenjp2-tools
      # 复制环境变量文件
      - name: Copy .env.example
        run: |
          if [ ! -f .env ]; then
            cp .env.example .env
          else
            echo ".env file already exists. Skipping the copy step."
          fi
      # 安装项目依赖
      - name: Install Dependencies
        run: npm install
      # 构建 Electron App
      - name: Build Electron App for Linux
        run: npm run build:linux || true
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
      # 上传构建产物
      - name: Upload Linux artifact
        uses: actions/upload-artifact@v4
        with:
          name: SPlarer-Linux
          if-no-files-found: ignore
          path: dist/*.*
      # 创建 GitHub Release
      - name: Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/v')
        with:
          draft: false
          files: dist/*.*
        env:
          GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
