From ffd7e10f4069304a9b73f440dc2e4adf0cff9895 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Wed, 19 Aug 2026 23:25:45 +0200 Subject: [PATCH] feat(ci): add member-only /build command for on-demand PR builds --- .github/workflows/pr-build-dispatch.yml | 71 +++++++ .github/workflows/pr-build.yml | 243 ++++++++++++++++++++++++ 2 files changed, 314 insertions(+) create mode 100644 .github/workflows/pr-build-dispatch.yml create mode 100644 .github/workflows/pr-build.yml diff --git a/.github/workflows/pr-build-dispatch.yml b/.github/workflows/pr-build-dispatch.yml new file mode 100644 index 000000000..6701a6bfb --- /dev/null +++ b/.github/workflows/pr-build-dispatch.yml @@ -0,0 +1,71 @@ +name: PR build dispatch + +# Member-only /build gate: dispatches pr-build.yml. +# /build -> android + ios +# /build android -> android only +# /build ios -> ios only +# Anyone without repo write access is ignored; the gate fails closed. +on: + issue_comment: + types: [created] + +permissions: + metadata: read + +jobs: + dispatch: + if: github.event.issue.pull_request + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: Parse /build command + id: cmd + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + script: | + const body = (context.payload.comment.body || '').trim() + const match = /^\/build(?:\s+(android|ios|both))?\s*$/i.exec(body) + if (!match) { + core.setOutput('command', 'false') + return + } + core.setOutput('command', 'true') + core.setOutput('platform', (match[1] || 'both').toLowerCase()) + + - name: Check commenter is a member + id: perm + if: steps.cmd.outputs.command == 'true' + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + script: | + const { owner, repo } = context.repo + const login = context.payload.comment.user.login + try { + const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ + owner, + repo, + username: login, + }) + const allowed = ['admin', 'maintain', 'write'].includes(data.permission) + core.info(`${login} has ${data.permission} -> ${allowed ? 'authorized' : 'not authorized'}`) + core.setOutput('allowed', String(allowed)) + } catch { + core.info(`${login} is not a collaborator -> denied`) + core.setOutput('allowed', 'false') + } + + - name: Dispatch PR build + if: ${{ steps.cmd.outputs.command == 'true' && steps.perm.outputs.allowed == 'true' }} + env: + GH_TOKEN: ${{ secrets.PR_BUILD_DISPATCH_TOKEN }} + PR_NUMBER: ${{ github.event.issue.number }} + PLATFORM: ${{ steps.cmd.outputs.platform }} + run: | + if [ -z "$GH_TOKEN" ]; then + echo "::error::PR_BUILD_DISPATCH_TOKEN is not set. Add it as a repository secret to enable /build dispatches." + exit 1 + fi + gh workflow run pr-build.yml --ref dev \ + --field pr_number="$PR_NUMBER" \ + --field platform="$PLATFORM" + echo "Dispatched pr-build.yml for PR #$PR_NUMBER ($PLATFORM)." \ No newline at end of file diff --git a/.github/workflows/pr-build.yml b/.github/workflows/pr-build.yml new file mode 100644 index 000000000..74986a954 --- /dev/null +++ b/.github/workflows/pr-build.yml @@ -0,0 +1,243 @@ +name: PR build + +# On-demand PR build (APK / unsigned IPA); dispatched by /build or run manually. +on: + workflow_dispatch: + inputs: + pr_number: + description: Pull request number to build + required: true + type: number + platform: + description: Platform to build + required: true + default: both + type: choice + options: + - android + - ios + - both + +permissions: + contents: read + issues: write + pull-requests: write + +concurrency: + group: pr-build-${{ inputs.pr_number }} + cancel-in-progress: false + +env: + CARGO_INCREMENTAL: 0 + CARGO_TERM_COLOR: always + SABLE_BUILD_FLAVOR: dev + VITE_APP_VERSION: 0.0.0-pr.${{ inputs.pr_number }} + +jobs: + resolve: + name: Resolve PR #${{ inputs.pr_number }} + runs-on: ubuntu-latest + timeout-minutes: 5 + outputs: + head_sha: ${{ steps.pr.outputs.head_sha }} + title: ${{ steps.pr.outputs.title }} + steps: + - name: Fetch pull request + id: pr + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + script: | + const { owner, repo } = context.repo + const { data: pull } = await github.rest.pulls.get({ + owner, + repo, + pull_number: ${{ inputs.pr_number }}, + }) + core.setOutput('head_sha', pull.head.sha) + core.setOutput('title', pull.title) + + build-android: + name: Build Android (PR #${{ inputs.pr_number }}) + needs: resolve + if: ${{ inputs.platform != 'ios' }} + runs-on: ubuntu-latest + timeout-minutes: 45 + env: + MISE_ENV: tauri + RUST_TARGETS: aarch64-linux-android armv7-linux-androideabi + ANDROID_KEY_BASE64: ${{ secrets.ANDROID_KEY_BASE64 }} + ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }} + ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }} + steps: + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: refs/pull/${{ inputs.pr_number }}/merge + persist-credentials: false + + - name: Setup app + uses: ./.github/actions/setup + with: + tauri: 'true' + + - name: Setup Android SDK + NDK + shell: bash + run: mise run tauri:setup:android + + - name: Add Rust targets + shell: bash + run: rustup target add $RUST_TARGETS + + - name: Cache Rust build + uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + with: + workspaces: src-tauri + + - name: Setup Gradle + uses: gradle/actions/setup-gradle@9c971963bec38e04b3d30dcc455b5382be2fdbfb # v6.3.0 + + - name: Resolve NDK + shell: bash + run: echo "NDK_HOME=$ANDROID_HOME/ndk/$ANDROID_NDK_VERSION" >> "$GITHUB_ENV" + + - name: Setup Android signing + if: ${{ env.ANDROID_KEY_BASE64 != '' }} + shell: bash + run: | + cd src-tauri/gen/android + echo "keyAlias=$ANDROID_KEY_ALIAS" > keystore.properties + echo "password=$ANDROID_KEY_PASSWORD" >> keystore.properties + base64 -d <<< "$ANDROID_KEY_BASE64" > "$RUNNER_TEMP/keystore.jks" + echo "storeFile=$RUNNER_TEMP/keystore.jks" >> keystore.properties + + - name: Build Android APK + shell: bash + run: | + if [ -n "${ANDROID_KEY_BASE64:-}" ]; then + pnpm tauri android build --apk --target aarch64 armv7 + else + # No keystore secret available: build a debug APK (signed with the + # debug keystore) so members can still sideload the build. + pnpm tauri android build --debug --apk --target aarch64 armv7 + fi + OUT='src-tauri/gen/android/app/build/outputs' + APK=$(find "$OUT/apk" -name '*.apk' -type f | head -1) + [ -n "$APK" ] || { echo "APK not found"; exit 1; } + mv "$APK" "$OUT/Sable-pr${{ inputs.pr_number }}-android.apk" + + - name: Upload APK + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: sable-pr${{ inputs.pr_number }}-android + path: src-tauri/gen/android/app/build/outputs/Sable-pr${{ inputs.pr_number }}-android.apk + if-no-files-found: error + retention-days: 7 + + build-ios: + name: Build iOS (PR #${{ inputs.pr_number }}) + needs: resolve + if: ${{ inputs.platform != 'android' }} + runs-on: macos-latest + timeout-minutes: 60 + env: + RUST_TARGETS: aarch64-apple-ios + steps: + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: refs/pull/${{ inputs.pr_number }}/merge + persist-credentials: false + + - name: Setup app + uses: ./.github/actions/setup + with: + tauri: 'true' + + - name: Add Rust targets + shell: bash + run: rustup target add $RUST_TARGETS + + - name: Cache Rust build + uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 + with: + workspaces: src-tauri + + - name: Set up iOS + shell: bash + run: mise -y run tauri:setup:ios + + - name: Download iOS frameworks + shell: bash + run: pnpm ios:frameworks + + - name: Symlink icons to the ios folder + shell: bash + run: mise run icons:symlink --write --force + + - name: Build unsigned IPA + shell: bash + run: | + pnpm tauri ios build --no-sign --ci + IPA=$(find src-tauri/gen/apple/build -name '*.ipa' -type f | head -1) + [ -n "$IPA" ] || { echo "IPA not found"; exit 1; } + mv "$IPA" "$(dirname "$IPA")/Sable-pr${{ inputs.pr_number }}-ios.ipa" + + - name: Upload IPA + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 + with: + name: sable-pr${{ inputs.pr_number }}-ios + path: src-tauri/gen/apple/build/**/Sable-pr${{ inputs.pr_number }}-ios.ipa + if-no-files-found: error + retention-days: 7 + + comment: + name: Post build result comment + needs: [resolve, build-android, build-ios] + if: ${{ always() && !cancelled() && needs.resolve.result == 'success' }} + runs-on: ubuntu-latest + timeout-minutes: 5 + env: + HEAD_SHA: ${{ needs.resolve.outputs.head_sha }} + PR_TITLE: ${{ needs.resolve.outputs.title }} + steps: + - name: Update PR comment + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + script: | + const { owner, repo } = context.repo + const issue_number = ${{ inputs.pr_number }} + const marker = '' + const shortSha = process.env.HEAD_SHA.slice(0, 7) + const runUrl = `${context.serverUrl}/${owner}/${repo}/actions/runs/${context.runId}` + const status = (result) => + result === 'success' ? ':white_check_mark: success' + : result === 'failure' ? ':x: failure' + : result === 'cancelled' ? ':stop_sign: cancelled' + : ':fast_forward: skipped' + + const body = [ + marker, + '## PR build', + '', + '| Platform | Status |', + '| --- | --- |', + `| Android | ${status('${{ needs.build-android.result }}')} |`, + `| iOS | ${status('${{ needs.build-ios.result }}')} |`, + '', + `- [Download artifacts](${runUrl}#artifacts) — expire after 7 days.`, + `- Commit \`${shortSha}\`: ${process.env.PR_TITLE}`, + ].join('\n') + + const { data: comments } = await github.rest.issues.listComments({ owner, repo, issue_number }) + const previous = comments.find((comment) => comment.body?.includes(marker)) + + if (previous) { + await github.rest.issues.updateComment({ + owner, + repo, + comment_id: previous.id, + body, + }) + } else { + await github.rest.issues.createComment({ owner, repo, issue_number, body }) + } \ No newline at end of file