From 876ca037d8e255cc4284380c7ca1b51d1c4ddea7 Mon Sep 17 00:00:00 2001 From: Sujal Date: Thu, 20 Feb 2025 21:53:08 +0530 Subject: [PATCH] Update android.yml --- .github/workflows/android.yml | 50 ++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index 98acb120a..675f5a38f 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -1,7 +1,7 @@ name: Android CI -on: [push, pull_request, workflow_dispatch] - +on: [push, pull_request, pull_request_target, workflow_dispatch] + permissions: contents: write pull-requests: write @@ -19,9 +19,11 @@ concurrency: cancel-in-progress: true jobs: - build: + build-and-upload: name: Run tests and generate APK runs-on: ubuntu-latest + # Run this job for push, pull_request, and workflow_dispatch (not pull_request_target) + if: github.event_name == 'push' || github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch' steps: - name: Checkout code uses: actions/checkout@v3 @@ -114,19 +116,29 @@ jobs: with: name: prodDebugAPK path: app/build/outputs/apk/prod/debug/app-*.apk - + + comment-on-pr: + name: Comment on PR with APK links + runs-on: ubuntu-latest + if: github.event_name == 'pull_request_target' + needs: build-and-upload + steps: + - name: Checkout base branch + uses: actions/checkout@v3 + with: + ref: ${{ github.base_ref }} + - name: Comment on PR with APK download links - if: github.event_name == 'pull_request' + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} uses: actions/github-script@v6 with: - github-token: ${{secrets.GITHUB_TOKEN}} script: | try { - const token = process.env.GITHUB_TOKEN; - if (!token) { - throw new Error('GITHUB_TOKEN is not set. Please check workflow permissions.'); - } - + const token = process.env.GH_TOKEN; + if (!token) { + throw new Error('GITHUB_TOKEN is not set.'); + } const { data: { artifacts } } = await github.rest.actions.listWorkflowRunArtifacts({ owner: context.repo.owner, @@ -152,9 +164,9 @@ jobs: const prodDownloadUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/suites/${context.runId}/artifacts/${prodArtifact.id}`; const commentBody = ` - 📱 **APK for pull request is ready to see the changes** 📱 - - [Download Beta APK](${betaDownloadUrl}) - - [Download Prod APK](${prodDownloadUrl}) + 📱 **APK for pull request is ready to see the changes** 📱 + - [Download Beta APK](${betaDownloadUrl}) + - [Download Prod APK](${prodDownloadUrl}) `; await github.rest.issues.createComment({ @@ -163,15 +175,9 @@ jobs: repo: context.repo.repo, body: commentBody }); - + console.log('Successfully posted comment with APK download links'); } catch (error) { console.error('Error in PR comment creation:', error); - if (error.message.includes('GITHUB_TOKEN')) { - core.setFailed('Missing or invalid GITHUB_TOKEN. Please check repository secrets configuration.'); - } else if (error.status === 403) { - core.setFailed('Permission denied. Please check workflow permissions in repository settings.'); - } else { - core.setFailed(`Workflow failed: ${error.message}`); - } + core.setFailed(`Workflow failed: ${error.message}`); }