TExcelTAD
Home › Mobile Build Pipelines: Gradle/AGP, Xcode, SwiftPM & Fastlane › GitHub Actions for Android Releases

GitHub Actions for Android Releases

Everything from the previous lessons comes together in one CI workflow: a fast check on every pull request, and a release job that builds a signed, minified App Bundle from a tag, verifies it and hands it to the store. GitHub Actions is used here because it's the most common; the same steps translate directly to GitLab CI, Bitrise, CircleCI or Jenkins.

Note: workflows can't run in this course's container, so the YAML below is non-run. Each step is one you ran for real in an earlier lesson; the Mini Project runs the release job's build and verification steps.

The pull-request job

yaml
# .github/workflows/android-ci.yml
name: android-ci
on:
  pull_request:
  push:
    branches: [main]

permissions:
  contents: read

jobs:
  check:
    runs-on: ubuntu-24.04
    timeout-minutes: 30
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 21
      - uses: gradle/actions/setup-gradle@v4     # caches ~/.gradle, validates the wrapper
        with:
          cache-read-only: ${{ github.ref != 'refs/heads/main' }}
      - name: Unit tests and a debug build of one variant
        run: ./gradlew testFreeDebugUnitTest assembleFreeDebug --configuration-cache
      - uses: actions/upload-artifact@v4
        if: always()
        with:
          name: test-results
          path: app/build/test-results/

Choices worth copying:

  • One variant on PRs. testFreeDebugUnitTest assembleFreeDebug instead of build, which would build and test every variant. Run the full matrix nightly.
  • cache-read-only on branches. Only main writes the Gradle cache, so pull requests can't fill it with garbage (or poison it).
  • Pinned runner image (ubuntu-24.04, not ubuntu-latest) and pinned action major versions. For supply-chain hardening, pin actions by commit SHA.
  • GitHub's Ubuntu runners already contain an Android SDK (ANDROID_HOME is set); if your compileSdk or build-tools aren't preinstalled, AGP installs them when licences are accepted, or use android-actions/setup-android.

The release job

Triggered by a version tag, with the signing secrets only available in a protected environment that requires approval:

yaml
# .github/workflows/android-release.yml
name: android-release
on:
  push:
    tags: ['v*']

permissions:
  contents: write          # to attach artifacts to the GitHub Release

jobs:
  release:
    runs-on: ubuntu-24.04
    environment: production            # secrets scoped here; required reviewers
    timeout-minutes: 45
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0               # tags for the version name
      - uses: actions/setup-java@v4
        with: { distribution: temurin, java-version: 21 }
      - uses: gradle/actions/setup-gradle@v4
        with: { cache-read-only: true }

      - name: Restore the upload keystore
        run: echo "$UPLOAD_KEYSTORE_B64" | base64 -d > "$RUNNER_TEMP/upload.jks"
        env:
          UPLOAD_KEYSTORE_B64: ${{ secrets.UPLOAD_KEYSTORE_B64 }}

      - name: Build signed bundle and APK
        run: ./gradlew bundleFreeRelease assembleFreeRelease -PbuildNumber=${{ github.run_number }}
        env:
          ORG_GRADLE_PROJECT_releaseStoreFile: ${{ runner.temp }}/upload.jks
          ORG_GRADLE_PROJECT_releaseStorePassword: ${{ secrets.UPLOAD_STORE_PASSWORD }}
          ORG_GRADLE_PROJECT_releaseKeyAlias: upload
          ORG_GRADLE_PROJECT_releaseKeyPassword: ${{ secrets.UPLOAD_KEY_PASSWORD }}

      - name: Verify outputs
        run: ./ci/verify-release.sh      # apksigner, zipalign, badging, bundletool checks

      - name: Upload to Play (internal track)
        run: bundle exec fastlane android internal
        env:
          PLAY_JSON_KEY: ${{ secrets.PLAY_SERVICE_ACCOUNT_JSON }}

      - uses: actions/upload-artifact@v4
        with:
          name: release-${{ github.ref_name }}
          path: |
            app/build/outputs/bundle/freeRelease/*.aab
            app/build/outputs/apk/free/release/*.apk
            app/build/outputs/mapping/freeRelease/mapping.txt

      - name: Remove the keystore
        if: always()
        run: rm -f "$RUNNER_TEMP/upload.jks"

The pieces map to earlier lessons: fetch-depth: 0 and buildNumber to version codes, the ORG_GRADLE_PROJECT_* variables to signing, the verify script to apksigner & zipalign and bundletool, and the archived mapping.txt to retrace. The upload uses Fastlane's supply.

How the Gradle cache key works

setup-gradle (like most CI caches) stores ~/.gradle under a key derived from the files that decide what Gradle downloads. Change one of them and the key changes; otherwise the job restores the previous cache. A runnable model of that key:

python · try it yourself
import hashlib

def cache_key(os_name, files):
    h = hashlib.sha256()
    for path in sorted(files):                 # order-independent
        h.update(path.encode() + b"\0" + files[path].encode() + b"\0")
    return f"gradle-{os_name}-{h.hexdigest()[:12]}"

repo = {
    "gradle/wrapper/gradle-wrapper.properties": "distributionUrl=…gradle-9.8.2-bin.zip",
    "gradle/libs.versions.toml": 'agp = "9.4.1"',
    "app/build.gradle.kts": "versionCode = 1",
}
k1 = cache_key("linux", repo)
print("main:          ", k1)

repo["app/build.gradle.kts"] = "versionCode = 2"          # a build script edit
k2 = cache_key("linux", repo)
print("script edited: ", k2, "(miss)" if k2 != k1 else "(hit)")

repo["app/build.gradle.kts"] = "versionCode = 1"
repo["gradle/libs.versions.toml"] = 'agp = "9.4.1"'       # unchanged content
print("reverted:      ", cache_key("linux", repo), "(hit)" if cache_key("linux", repo) == k1 else "(miss)")
print("on macOS:      ", cache_key("macos", repo), "(separate cache per OS)")
main:           gradle-linux-bff5a8e90df7
script edited:  gradle-linux-ee1ea4eab70f (miss)
reverted:       gradle-linux-bff5a8e90df7 (hit)
on macOS:       gradle-macos-bff5a8e90df7 (separate cache per OS)

A miss isn't a disaster: real cache actions fall back to the most recent cache with the same prefix (restore-keys) and update it afterwards. What you want to avoid is a key that changes on every commit (e.g. hashing all source files), which means you never hit.

Test yourself

Why does the release job use fetch-depth: 0 in actions/checkout?

  1. To speed up the checkout
  2. So the Git tags are present for the version name
  3. To get the keystore
  4. GitHub requires it for tags

Why is cache-read-only true for pull requests?

  1. Read-only caches are faster
  2. Only trusted main-branch builds should write the cache, so PRs can't fill or poison it
  3. Pull requests can't use caches
  4. To save disk space on the runner

Where should the signing secrets of the release job live?

  1. In the repository, base64-encoded
  2. In a protected GitHub environment's secrets, passed to Gradle as ORG_GRADLE_PROJECT_* variables
  3. In gradle.properties
  4. In the workflow YAML