From da8fe5f4ceedf8941984101aaa0a00492ae41866 Mon Sep 17 00:00:00 2001 From: Fran Aguilera Date: Mon, 21 Jul 2025 11:10:52 +0200 Subject: [PATCH 1/2] Simplifying apk size reporting --- .github/workflows/android.yaml | 85 +++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 32 deletions(-) diff --git a/.github/workflows/android.yaml b/.github/workflows/android.yaml index f44eddaf5..39b5b722c 100644 --- a/.github/workflows/android.yaml +++ b/.github/workflows/android.yaml @@ -79,6 +79,8 @@ jobs: run: ./ci/run_tests.sh build_and_compare_apk: + permissions: + contents: write runs-on: ubuntu-latest if: needs.pre_check.outputs.should_run == 'true' needs: pre_check @@ -98,33 +100,50 @@ jobs: name: android_app.apk path: ./bazel-bin/examples/android/android_app.apk - - name: Prepare baseline folder (main only) - if: github.ref == 'refs/heads/main' && success() + - name: Save APK size baseline and push to branch (main only) + #if: github.ref == 'refs/heads/main' && success() + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - rm -rf bazel_apk_baseline # Remove if it exists - mkdir -p bazel_apk_baseline - cp ./bazel-bin/examples/android/android_app.apk bazel_apk_baseline/ + APK_PATH=$(find bazel-bin/examples/android -name "android_app.apk" | head -n 1) + SIZE_FILE=ci/hello_world_bazel_x86_64_main_size.txt + stat -c%s "$APK_PATH" > "$SIZE_FILE" - - name: Cache baseline APK (main only) - if: github.ref == 'refs/heads/main' && success() - uses: actions/cache/save@v3 - with: - path: bazel_apk_baseline - key: bazel-apk-main-baseline + git config user.name "github-actions" + git config user.email "github-actions@users.noreply.github.com" + git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git + git fetch origin hello-world-bazel-apk-size + git worktree add temp-worktree origin/hello-world-bazel-apk-size - - name: Restore baseline APK from main (PR only) - if: github.event_name == 'pull_request' - uses: actions/cache/restore@v3 - with: - path: bazel_apk_baseline - key: bazel-apk-main-baseline + cp "$SIZE_FILE" temp-worktree/ci/ + + cd temp-worktree + git add ci/hello_world_bazel_x86_64_main_size.txt + + if ! git diff --cached --quiet; then + git commit -m "ci: update Bazel APK size baseline [skip ci]" + git push origin HEAD:hello-world-bazel-apk-size + else + echo "No changes to baseline size." + fi + + cd .. + git worktree remove temp-worktree --force + + - name: Fetch APK size baseline from branch (PR only) + #if: github.event_name == 'pull_request' + run: | + git fetch origin hello-world-bazel-apk-size + git worktree add temp-baseline origin/hello-world-bazel-apk-size + cp temp-baseline/ci/hello_world_bazel_x86_64_main_size.txt ci/ + git worktree remove temp-baseline --force - name: Compute and compare APK size if: github.event_name == 'pull_request' id: apk_size run: | APK_PATH=$(find bazel-bin/examples/android -name "android_app.apk" | head -n 1) - BAZEL_APK_BASELINE_PATH=$(find bazel_apk_baseline -name "android_app.apk" | head -n 1) + BASELINE_PATH=ci/hello_world_bazel_x86_64_main_size.txt if [ -z "$APK_PATH" ]; then echo "❌ Current APK file not found!" @@ -135,17 +154,15 @@ jobs: CURRENT_KB=$((CURRENT_SIZE / 1024)) echo "current_kb=$CURRENT_KB" >> $GITHUB_OUTPUT - if [ -z "$BAZEL_APK_BASELINE_PATH" ]; then + if [ ! -f "$BASELINE_PATH" ]; then echo "baseline_kb=" >> $GITHUB_OUTPUT echo "diff_kb=" >> $GITHUB_OUTPUT else - BASELINE_SIZE=$(stat -c%s "$BAZEL_APK_BASELINE_PATH") + BASELINE_SIZE=$(cat "$BASELINE_PATH") BASELINE_KB=$((BASELINE_SIZE / 1024)) DIFF_KB=$((CURRENT_KB - BASELINE_KB)) echo "baseline_kb=$BASELINE_KB" >> $GITHUB_OUTPUT echo "diff_kb=$DIFF_KB" >> $GITHUB_OUTPUT - MODIFIED_DATE=$(stat -c %y "$BAZEL_APK_BASELINE_PATH") - echo "Baseline APK last modified date: $MODIFIED_DATE" fi - name: Report APK size on PR @@ -161,11 +178,11 @@ jobs: const baseline = process.env.BASELINE_KB; const diff = process.env.DIFF_KB; - let table = ''; + let body = ''; if (baseline && baseline !== '') { - table = ` - ### 📦 APK Size Report + body = ` + ### 📦 Bazel APK(x86_64) Size Report | Metric | Size (KB) | |------------|-----------| @@ -176,21 +193,25 @@ jobs: `; if (parseInt(diff, 10) > 0) { - table += `> ❌ APK size increased by ${diff} KB.`; + body += `> ❌ Bazel APK(x86_64) size increased by ${diff} KB.`; } else if (parseInt(diff, 10) < 0) { - table += `> ✅ APK size decreased by ${Math.abs(diff)} KB.`; + body += `> ✅ Bazel APK(x86_64) size decreased by ${Math.abs(diff)} KB.`; } else { - table += `> ✅ APK size unchanged.`; + body += `> ✅ Bazel APK(x86_64) size unchanged.`; } } else { - table = `📌 Current APK size: ${current} KB (No baseline available for comparison).`; + body = `📌 Current Bazel APK(x86_64) size: ${current} KB (No baseline available for comparison).`; } - github.rest.issues.createComment({ + github.rest.pulls.createReviewComment({ owner: context.repo.owner, repo: context.repo.repo, - issue_number: context.payload.pull_request.number, - body: table + pull_number: context.payload.pull_request.number, + body: body, + commit_id: context.payload.pull_request.head.sha, + path: 'ci/hello_world_bazel_x86_64_main_size.txt', + line: 1, + side: 'RIGHT' }); gradle_tests: From c2a4fc4a193daec97e34467c1033be40a28fada9 Mon Sep 17 00:00:00 2001 From: Fran Aguilera Date: Mon, 21 Jul 2025 16:53:34 +0200 Subject: [PATCH 2/2] Updating baseline branch --- .github/workflows/android.yaml | 36 ++++++++++++++-------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/.github/workflows/android.yaml b/.github/workflows/android.yaml index 39b5b722c..195113293 100644 --- a/.github/workflows/android.yaml +++ b/.github/workflows/android.yaml @@ -11,7 +11,7 @@ concurrency: permissions: actions: write - contents: read + contents: write pull-requests: write statuses: write @@ -79,8 +79,6 @@ jobs: run: ./ci/run_tests.sh build_and_compare_apk: - permissions: - contents: write runs-on: ubuntu-latest if: needs.pre_check.outputs.should_run == 'true' needs: pre_check @@ -101,28 +99,28 @@ jobs: path: ./bazel-bin/examples/android/android_app.apk - name: Save APK size baseline and push to branch (main only) - #if: github.ref == 'refs/heads/main' && success() + if: github.ref == 'refs/heads/main' && success() env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | APK_PATH=$(find bazel-bin/examples/android -name "android_app.apk" | head -n 1) - SIZE_FILE=ci/hello_world_bazel_x86_64_main_size.txt + SIZE_FILE=ci/baseline_binary_size.txt stat -c%s "$APK_PATH" > "$SIZE_FILE" git config user.name "github-actions" git config user.email "github-actions@users.noreply.github.com" git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git - git fetch origin hello-world-bazel-apk-size - git worktree add temp-worktree origin/hello-world-bazel-apk-size + git fetch origin binary-size-baseline + git worktree add temp-worktree origin/binary-size-baseline cp "$SIZE_FILE" temp-worktree/ci/ cd temp-worktree - git add ci/hello_world_bazel_x86_64_main_size.txt + git add ci/baseline_binary_size.txt if ! git diff --cached --quiet; then git commit -m "ci: update Bazel APK size baseline [skip ci]" - git push origin HEAD:hello-world-bazel-apk-size + git push origin HEAD:binary-size-baseline else echo "No changes to baseline size." fi @@ -131,11 +129,11 @@ jobs: git worktree remove temp-worktree --force - name: Fetch APK size baseline from branch (PR only) - #if: github.event_name == 'pull_request' + if: github.event_name == 'pull_request' run: | - git fetch origin hello-world-bazel-apk-size - git worktree add temp-baseline origin/hello-world-bazel-apk-size - cp temp-baseline/ci/hello_world_bazel_x86_64_main_size.txt ci/ + git fetch origin binary-size-baseline + git worktree add temp-baseline origin/binary-size-baseline + cp temp-baseline/ci/baseline_binary_size.txt ci/ git worktree remove temp-baseline --force - name: Compute and compare APK size @@ -143,7 +141,7 @@ jobs: id: apk_size run: | APK_PATH=$(find bazel-bin/examples/android -name "android_app.apk" | head -n 1) - BASELINE_PATH=ci/hello_world_bazel_x86_64_main_size.txt + BASELINE_PATH=ci/baseline_binary_size.txt if [ -z "$APK_PATH" ]; then echo "❌ Current APK file not found!" @@ -203,15 +201,11 @@ jobs: body = `📌 Current Bazel APK(x86_64) size: ${current} KB (No baseline available for comparison).`; } - github.rest.pulls.createReviewComment({ + github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, - pull_number: context.payload.pull_request.number, - body: body, - commit_id: context.payload.pull_request.head.sha, - path: 'ci/hello_world_bazel_x86_64_main_size.txt', - line: 1, - side: 'RIGHT' + issue_number: context.payload.pull_request.number, + body: body }); gradle_tests: