Skip to content

Instantly share code, notes, and snippets.

@bastionkid
Created June 16, 2023 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastionkid/d906046e8ffec5503a62f8710862968b to your computer and use it in GitHub Desktop.
Save bastionkid/d906046e8ffec5503a62f8710862968b to your computer and use it in GitHub Desktop.
Complete Size Metrics workflow
name: Size Metrics
# cancel in-progress workflow if new commits are pushed to same head branch
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
cancel-in-progress: true
on:
pull_request:
branches: [ "master", "main", "release/*", "feature/*" ]
env:
size_diff_report_file: apk_size_diff_report.html
jobs:
build:
name: Build apk
runs-on: ubuntu-latest
outputs:
base_short_sha: ${{ steps.base-short-sha.outputs.base_short_sha }}
head_short_sha: ${{ steps.head-short-sha.outputs.head_short_sha }}
steps:
- name: Checkout base (PR target) branch
uses: actions/checkout@v3
with:
ref: ${{ github.base_ref }}
# this step is optional if you don't use JDK 17 as ubuntu-latest runner already
# has java setup which defaults to JDK 11
- name: Setup Java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '17'
# this step is optional and should be added in case you get "Failed to install the following
# Android SDK packages as some licences have not been accepted" error
- name: Setup Android SDK
uses: android-actions/setup-android@v2
# setup cache so that every build trigger doesn't require downloading all the
# dependencies used in project again and again saving tonnes of time
- name: Setup Gradle & Android SDK Cache
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
/usr/local/lib/android/sdk/build-tools
/usr/local/lib/android/sdk/system-images
key: ${{ runner.os }}-${{ hashFiles('**/*.gradle*') }}-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}-${{ hashFiles('**/buildSrc/**/*.kt') }}
- name: Generate shorter github.sha for base branch
id: base-short-sha
run: |
chmod +x ./.github/scripts/commit_short_sha.sh
short_sha=$(./.github/scripts/commit_short_sha.sh)
echo "base_short_sha=${short_sha}" >> "$GITHUB_ENV"
echo "base_short_sha=${short_sha}" >> "$GITHUB_OUTPUT"
- name: Build release APK for base branch and rename it to match base-short-sha result
run: |
./gradlew assembleRelease -PtargetDensity=xxhdpi -PtargetAbi=arm64-v8a -PresConfig=en -Pandroid.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=BaselineProfile
mv app/build/outputs/apk/release/app-xxhdpiArm64-v8a-release-unsigned.apk app/build/outputs/apk/release/${{ env.base_short_sha }}.apk
- name: Upload APK
uses: actions/upload-artifact@v3
with:
name: ${{ env.base_short_sha }}.apk
path: app/build/outputs/apk/release/${{ env.base_short_sha }}.apk
- name: Checkout head branch
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
fetch-depth: 0
- name: Generate shorter github.sha for head branch
id: head-short-sha
run: |
chmod +x ./.github/scripts/commit_short_sha.sh
short_sha=$(./.github/scripts/commit_short_sha.sh)
echo "head_short_sha=${short_sha}" >> "$GITHUB_ENV"
echo "head_short_sha=${short_sha}" >> "$GITHUB_OUTPUT"
- name: Merge base (PR target) branch
run: |
git config --global user.email "akash.mercer@gmail.com"
git config --global user.name "Akash Khunt"
git merge origin/${{ github.base_ref }}
- name: Build release APK for head branch and rename it to match head-short-sha result
run: |
./gradlew assembleRelease -PtargetDensity=xxhdpi -PtargetAbi=arm64-v8a -PresConfig=en -Pandroid.testInstrumentationRunnerArguments.androidx.benchmark.enabledRules=BaselineProfile
mv app/build/outputs/apk/release/app-xxhdpiArm64-v8a-release-unsigned.apk app/build/outputs/apk/release/${{ env.head_short_sha }}.apk
- name: Upload APK
uses: actions/upload-artifact@v3
with:
name: ${{ env.head_short_sha }}.apk
path: app/build/outputs/apk/release/${{ env.head_short_sha }}.apk
size_diff_report:
needs: build
name: Size Report
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
with:
name: ${{ needs.build.outputs.head_short_sha }}.apk
- uses: actions/download-artifact@v3
with:
name: ${{ needs.build.outputs.base_short_sha }}.apk
# here we use our custom python script to generate file matching size_diff_report_file
- name: Generate size report
run: |
chmod +x ./.github/scripts/compare_apks.py
python3 ./.github/scripts/compare_apks.py ${{ needs.build.outputs.base_short_sha }} ${{ needs.build.outputs.head_short_sha }}
- name: Upload Size Report html file
uses: actions/upload-artifact@v3
with:
name: ${{ env.size_diff_report_file }}
path: ${{ env.size_diff_report_file }}
pr_comment:
needs: size_diff_report
name: PR Comment (Size Report)
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v3
with:
name: ${{ env.size_diff_report_file }}
- name: Add PR Comment
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
try {
const filePath = 'apk_size_diff_report.html'
const sizeReportContent = fs.readFileSync(filePath, 'utf8')
const result = await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: sizeReportContent
})
console.log(result)
} catch (error) {
console.error('Error reading file:', error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment