diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml new file mode 100644 index 0000000..503dfef --- /dev/null +++ b/.github/workflows/pr.yml @@ -0,0 +1,149 @@ +name: 'Tauri App Build PR Test' + +on: + pull_request: + types: [opened, labeled] + +jobs: + test-tauri: + if: github.event.action == 'opened' || github.event.label.name == '需要 Action 构建' + + permissions: + contents: read + pull-requests: write + + strategy: + fail-fast: false + matrix: + include: + - platform: 'macos-latest' + args: '--target aarch64-apple-darwin' + name: 'macOS ARM64' + - platform: 'macos-latest' + args: '--target x86_64-apple-darwin' + name: 'macOS x64' + - platform: 'ubuntu-22.04' + args: '' + name: 'Linux' + - platform: 'windows-latest' + args: '' + name: 'Windows' + + runs-on: ${{ matrix.platform }} + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: lts/* + + - run: corepack enable + + - uses: dtolnay/rust-toolchain@stable + with: + targets: ${{ matrix.platform == 'macos-latest' && 'aarch64-apple-darwin,x86_64-apple-darwin' || '' }} + + - if: matrix.platform == 'ubuntu-22.04' + run: | + sudo apt-get update + sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf + + - run: pnpm install + + - uses: tauri-apps/tauri-action@v0 + id: tauri-build + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + args: ${{ matrix.args }} + + - if: matrix.platform == 'macos-latest' + uses: actions/upload-artifact@v4 + with: + name: tauri-app-${{ matrix.args == '--target aarch64-apple-darwin' && 'macos-arm64' || 'macos-x64' }} + path: | + src-tauri/target/*/release/bundle/**/*.dmg + src-tauri/target/*/release/bundle/**/*.app + if-no-files-found: warn + + - if: matrix.platform == 'ubuntu-22.04' + uses: actions/upload-artifact@v4 + with: + name: tauri-app-linux + path: | + src-tauri/target/release/bundle/**/*.AppImage + src-tauri/target/release/bundle/**/*.deb + if-no-files-found: warn + + - if: matrix.platform == 'windows-latest' + uses: actions/upload-artifact@v4 + with: + name: tauri-app-windows + path: | + src-tauri/target/release/bundle/**/*.msi + src-tauri/target/release/bundle/**/*.exe + if-no-files-found: warn + + outputs: + result: ${{ job.status }} + platform: ${{ matrix.name }} + + cleanup: + needs: test-tauri + if: always() && (github.event.action == 'opened' || github.event.label.name == '需要 Action 构建') + runs-on: ubuntu-latest + permissions: + pull-requests: write + steps: + - uses: actions/github-script@v7 + with: + script: | + const results = JSON.parse('${{ toJson(needs.test-tauri.result) }}'); + const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; + const commit = context.sha.substring(0, 7); + const branch = context.ref.replace('refs/heads/', ''); + + let body = ''; + + if (results == 'success') { + body = `✅ **构建成功**\n\n`; + body += `- 分支: ${branch}\n`; + body += `- 提交: ${commit}\n`; + body += `- 触发方式: ${context.payload.action == 'opened' ? 'PR 创建' : '标签触发'}\n\n`; + body += `**构建产物:**\n`; + body += `- macOS ARM64: \`tauri-app-macos-arm64\`\n`; + body += `- macOS x64: \`tauri-app-macos-x64\`\n`; + body += `- Linux: \`tauri-app-linux\`\n`; + body += `- Windows: \`tauri-app-windows\`\n\n`; + body += `[查看详情并下载](${runUrl})`; + } else { + body = `❌ **构建失败**\n\n`; + body += `- 分支: ${branch}\n`; + body += `- 提交: ${commit}\n`; + body += `- 触发方式: ${context.payload.action == 'opened' ? 'PR 创建' : '标签触发'}\n`; + body += `- 失败状态: ${results}\n\n`; + body += `请检查以下可能原因:\n`; + body += `- 代码编译错误\n`; + body += `- 依赖安装失败\n`; + body += `- 构建配置问题\n\n`; + body += `[查看详细日志](${runUrl})`; + } + + await github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: body + }); + + if (github.event.label.name == '需要 Action 构建') { + try { + await github.rest.issues.removeLabel({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + name: '需要 Action 构建' + }); + } catch (e) { + } + }