CI/CD pipelines

Learn how to build an Android CI/CD pipeline with GitHub Actions and Gradle: caching, lint and unit tests, upload key secrets, and Play track publishing. About 25 minutes.

Why it matters

  • A broken build on main blocks every developer. A CI pipeline that runs ./gradlew lintDebug testDebugUnitTest on every pull request catches it before merge.
  • With Play App Signing, Google keeps the app signing key. CI only holds the upload key, and if that leaks, the account owner can ask Google to reset it without changing the key users' devices trust.
  • Interviewers ask you to design an Android release pipeline: where secrets live, how versionCode is stamped, and how a build moves from the internal track to a production staged rollout.
  • Automating the upload with the Google Play Developer API (through Gradle Play Publisher or fastlane supply) removes manual uploads, which is where wrong builds and reused version codes slip in.

The simple explanation

  • A CI pipeline is an assembly line: each station (build, lint, test) must pass before the product moves on.
  • The Gradle cache is the parts bin: instead of making every part again, you reuse the last good one if nothing changed.
  • Play App Signing is a vault: the real key stays with Google; CI only has the courier's badge (the upload key) that proves a delivery came from you.
  • A staged rollout is a test flight: a small share of users get the build first.

What is a CI/CD pipeline for Android? It is an automated workflow, usually GitHub Actions, GitLab CI or similar, that builds the app, runs lint and unit tests on every change (CI), and on main or a tag builds a signed Android App Bundle and uploads it to a Google Play track (CD). The usual sequence is checkout → set up JDK and Gradle → lint and test → build signed .aab → upload to the internal track → promote.

Build and cache. In GitHub Actions you use actions/checkout, actions/setup-java and gradle/actions/setup-gradle. The Gradle action caches the Gradle User Home (downloaded dependencies, wrapper distributions, build cache entries) between runs, so you do not need to hand-write actions/cache keys. By default it saves the cache only from the default branch; other branches read it. You can also turn on org.gradle.caching=true and org.gradle.configuration-cache=true in gradle.properties.

Secrets. The upload keystore (base64-encoded), its passwords and the Play service-account JSON are stored as encrypted CI secrets. The workflow passes them to steps as environment variables, decodes the keystore into the runner's temp directory, and build.gradle.kts reads the values from the environment. Nothing is committed, and nothing is echoed to the log.

Play App Signing and AAB. New apps on Google Play must be published as an Android App Bundle (.aab), which also means using Play App Signing. You sign the bundle with your upload key; Google signs the APKs it generates for each device with the app signing key it holds.

Tracks and rollout. A common flow uploads to the internal track first, then promotes to closed, open or production. In production you can release to a percentage of users (a staged rollout), increase it, or halt it. Gradle Play Publisher and fastlane supply both call the Play Developer API to do this from CI.

StageCommand / toolFails the job on
Lint./gradlew lintDebuglint issues at error severity
Test./gradlew testDebugUnitTestany failing JVM unit test
Build./gradlew bundleReleasecompile error, signing config error
Publish./gradlew publishReleaseBundle (Gradle Play Publisher)Play API error, version code already used
PromotePlay Console or ./gradlew promoteArtifactmanual approval, review

versionCode is an integer that must be higher for every new release; versionName is the free-text label users see (e.g. 2.4.0).

Notes

  • Use the official actions: actions/checkout, actions/setup-java (with a distribution such as temurin) and gradle/actions/setup-gradle. The Gradle action handles caching for you.
  • Run lint and unit tests on every pull request; build and publish the signed .aab only from main or a release tag.
  • On an ephemeral runner the Gradle daemon lives only for that job, so it gives no cross-run speed-up. Several ./gradlew calls in the same job still reuse it.
  • Play App Signing: the upload key signs the .aab in CI; Google's app signing key signs the APKs users install. If the upload key is lost or leaked, the account owner requests an upload key reset in Play Console.
  • New apps on Google Play must use the Android App Bundle format. Use ./gradlew bundleRelease, not assembleRelease (which builds APKs).
  • Every upload needs a versionCode that has never been used for the app before. Stamping it in CI (for example from github.run_number plus an offset) avoids two branches picking the same number.
  • Play tracks: internal, closed, open, production. A production staged rollout can be increased, halted and resumed in Play Console or through the Play Developer API.
  • Gradle Play Publisher (plugin id com.github.triplet.play) reads service-account JSON from the ANDROID_PUBLISHER_CREDENTIALS environment variable, so the key file never has to exist in the repo.
  • GitHub Actions masks registered secret values in logs, but a secret transformed (for example base64-decoded and printed) is not masked. Never echo a secret.
  • Trunk-based development (small changes merged to main, releases tagged from main) keeps one version counter and avoids long-lived release branches.

Code example

This GitHub Actions workflow runs lint and unit tests on every pull request, and on main builds a signed bundle and uploads it to the internal track with Gradle Play Publisher.

# .github/workflows/android.yml
name: Android CI
 
on:
  pull_request:
  push:
    branches: [main]
 
jobs:
  check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-java@v5
        with:
          distribution: temurin
          java-version: 17
      - uses: gradle/actions/setup-gradle@v6
      - name: Lint and unit tests
        run: ./gradlew lintDebug testDebugUnitTest
 
  release:
    needs: check
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    environment: play-release
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-java@v5
        with:
          distribution: temurin
          java-version: 17
      - uses: gradle/actions/setup-gradle@v6
      - name: Decode upload keystore
        env:
          UPLOAD_KEYSTORE_BASE64: ${{ secrets.UPLOAD_KEYSTORE_BASE64 }}
        run: printf '%s' "$UPLOAD_KEYSTORE_BASE64" | base64 --decode > "$RUNNER_TEMP/upload.jks"
      - name: Build and upload to the internal track
        env:
          UPLOAD_KEYSTORE_PATH: ${{ runner.temp }}/upload.jks
          UPLOAD_KEYSTORE_PASSWORD: ${{ secrets.UPLOAD_KEYSTORE_PASSWORD }}
          UPLOAD_KEY_ALIAS: ${{ secrets.UPLOAD_KEY_ALIAS }}
          UPLOAD_KEY_PASSWORD: ${{ secrets.UPLOAD_KEY_PASSWORD }}
          ANDROID_PUBLISHER_CREDENTIALS: ${{ secrets.PLAY_SERVICE_ACCOUNT_JSON }}
          VERSION_CODE: ${{ github.run_number }}
        run: ./gradlew publishReleaseBundle --track internal

The app module reads the signing values and the version code from the environment:

// app/build.gradle.kts
plugins {
    id("com.android.application")
    id("com.github.triplet.play")
}
 
android {
    defaultConfig {
        // Offset keeps CI numbers above codes already used before CI existed.
        versionCode = 1000 + (System.getenv("VERSION_CODE")?.toInt() ?: 0)
        versionName = "2.4.0"
    }
    signingConfigs {
        create("upload") {
            val path = System.getenv("UPLOAD_KEYSTORE_PATH")
            if (path != null) {
                storeFile = file(path)
                storePassword = System.getenv("UPLOAD_KEYSTORE_PASSWORD")
                keyAlias = System.getenv("UPLOAD_KEY_ALIAS")
                keyPassword = System.getenv("UPLOAD_KEY_PASSWORD")
            }
        }
    }
    buildTypes {
        release {
            if (System.getenv("UPLOAD_KEYSTORE_PATH") != null) {
                signingConfig = signingConfigs.getByName("upload")
            }
        }
    }
}
 
play {
    // Credentials come from ANDROID_PUBLISHER_CREDENTIALS.
    track.set("internal")
    defaultToAppBundles.set(true)
}
  • setup-gradle restores and saves the Gradle cache, so the second run downloads far less.
  • The keystore is decoded into $RUNNER_TEMP, outside the repo, and the passwords stay in environment variables. Nothing secret is printed.
  • publishReleaseBundle builds the signed .aab and uploads it to the internal track. Promotion to production with a staged rollout is a separate step, for example ./gradlew promoteArtifact --from-track internal --promote-track production --release-status inProgress --user-fraction .05.
  • github.run_number increases with every run of the workflow, so each upload gets a new versionCode. Re-running the same run reuses the number and Play rejects it, which is the safe outcome.
  • The environment: play-release line lets you add required reviewers in GitHub, so a human approves before the release job can read its secrets.

Run the idea in plain Kotlin

data class PipelineStep(val name: String, val success: Boolean)

fun runPipeline(steps: List<PipelineStep>): List<String> {
  val log = mutableListOf("Pipeline started")
  for (step in steps) {
      if (step.success) {
          log += "  PASS: " + step.name
      } else {
          log += "  FAIL: " + step.name
          log += "Pipeline stopped at: " + step.name
          return log
      }
  }
  log += "Pipeline completed"
  return log
}

fun main() {
  val names = listOf("lintDebug", "testDebugUnitTest", "bundleRelease", "publishReleaseBundle")

  println("--- Run 1: a unit test fails ---")
  val failing = names.map { PipelineStep(it, it != "testDebugUnitTest") }
  runPipeline(failing).forEach(::println)

  println("--- Run 2: all steps pass ---")
  val passing = names.map { PipelineStep(it, true) }
  runPipeline(passing).forEach(::println)
}

Expected output:

--- Run 1: a unit test fails ---
Pipeline started
  PASS: lintDebug
  FAIL: testDebugUnitTest
Pipeline stopped at: testDebugUnitTest
--- Run 2: all steps pass ---
Pipeline started
  PASS: lintDebug
  PASS: testDebugUnitTest
  PASS: bundleRelease
  PASS: publishReleaseBundle
Pipeline completed

In production

Reused versionCode blocks a release
  • The release job uploaded an .aab and Play rejected it because that version code had already been used: a hotfix built from another branch had shipped with the same number.
  • versionCode was hardcoded in build.gradle.kts and bumped by hand on each branch, so two branches picked the same value.
  • The release waited while someone looked up the highest code in Play Console and rebuilt.
The fix
  • versionCode is now stamped by CI from a single counter (github.run_number plus a fixed offset), and only main produces release builds.
  • Gradle Play Publisher's resolutionStrategy can be set to ResolutionStrategy.AUTO to pick a code above the latest one on Play, as a safety net.
  • Hotfixes merge to main first, so there is one line of version codes.
Upload keystore committed to the repository
  • The upload .jks and a keystore.properties file with its passwords were committed to a private repository and copied to every clone and fork.
  • Anyone with repository access could sign bundles that Play would accept as coming from the team.
The fix
  • The account owner requested an upload key reset in Play Console with a new upload certificate. The app signing key, and so the key on users' devices, did not change.
  • The new keystore lives only in CI secrets as base64; passwords are separate secrets read from environment variables.
  • *.jks, *.keystore and keystore.properties were added to .gitignore, and a secret scanner (such as gitleaks) runs on pull requests.

Common mistakes

  • Building APKs for Play. assembleRelease produces APKs; new apps must upload an Android App Bundle. Use bundleRelease (or Gradle Play Publisher's publishReleaseBundle).
  • Committing the upload key or service-account JSON. Anyone with repo access can sign or publish. Keep them in the CI secret store and pass them as environment variables.
  • Printing secrets. An echo of a decoded secret, or set -x around a command that uses one, can bypass log masking. Write secrets to files or env vars, never to stdout.
  • Hand-bumping versionCode on several branches. Two branches pick the same number and the second upload is rejected. Stamp it in CI from one counter.
  • Skipping lint or unit tests to save time. Problems surface in production instead. Gate merges on lintDebug and testDebugUnitTest.
  • Releasing to 100% of production at once. Every user gets a bad build. Start a staged rollout, watch Android vitals and crash reports, then increase or halt.

Interview questions

Answer out loud first. Then reveal the answer and compare it at each depth.

“How do you make a Gradle build fast in CI?”

“What is Play App Signing and why does it matter for CI?”

“Describe a release pipeline from merge to production.”

“How do you handle secrets in a CI pipeline for Android?”

Quiz

Which setting lets Gradle restore task outputs instead of re-running tasks whose inputs did not change?

Which Gradle task builds the format required for new apps on Google Play?

Where should a CI pipeline get the upload keystore password?

A production staged rollout shows a crash spike. What does halting it do?

0 of 4 answered

Mini task

Wire a minimal CI pipeline for your Android app (15 min)

  1. Create a workflow file. Add .github/workflows/android.yml that runs on pull_request and on pushes to main, using ubuntu-latest.

  2. Set up the toolchain. Add actions/checkout, actions/setup-java (distribution temurin, the JDK version your Android Gradle plugin needs) and gradle/actions/setup-gradle.

  3. Add the quality gate. Run ./gradlew lintDebug testDebugUnitTest. Break a test on purpose and confirm the job fails, then fix it.

  4. Build a bundle. Add a step that runs ./gradlew bundleRelease and confirm app/build/outputs/bundle/release/app-release.aab is produced (unsigned is fine for this step).

  5. Add signing from secrets. Store a base64 upload keystore and its passwords as repository secrets, decode the keystore into $RUNNER_TEMP and read the values in signingConfigs from environment variables, as in the example. Check the log shows no secret values.

  6. Verify caching. Push twice. In the second run's setup-gradle step output, confirm the cache was restored and the job is faster.

Done condition: pull requests run lint and unit tests, a push to main produces a signed .aab, no secret appears in the repository or the logs, and the second run restores the Gradle cache.

Next: Signing, bundles and Play release tracks