Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

ci(mobile): build a release APK as a workflow artifact

Tin1e657291abdd
diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml
new file mode 100644
index 000000000..c5f710129
--- /dev/null
+++ b/.github/workflows/android.yml
@@ -0,0 +1,103 @@
+name: Android APK
+
+# Builds a release APK of apps/mobile and uploads it as a workflow artifact. With the signing
+# secrets set, the APK is signed with the Kaneo release key, so later builds install as updates.
+# Without them (forks, or before the key exists) it falls back to the debug key.
+
+on:
+ pull_request:
+ paths:
+ - "apps/mobile/**"
+ - ".github/workflows/android.yml"
+ workflow_dispatch:
+
+concurrency:
+ group: android-${{ github.ref }}
+ cancel-in-progress: true
+
+permissions:
+ contents: read
+
+jobs:
+ apk:
+ runs-on: blacksmith-4vcpu-ubuntu-2404
+ timeout-minutes: 60
+ steps:
+ - name: Checkout repository
+ uses: useblacksmith/checkout@25227e61ff9dafe400e22fa487b673eac4e4409a # v1
+ with:
+ persist-credentials: false
+
+ - name: Setup pnpm
+ uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0
+ with:
+ version: 10.32.1
+
+ - name: Setup Node.js
+ uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7
+ with:
+ node-version: 24.19.0
+ cache: pnpm
+
+ - name: Install dependencies
+ run: pnpm install --frozen-lockfile
+
+ - name: Generate the Android project
+ run: pnpm --filter @kaneo/mobile exec expo prebuild --platform android --clean --no-install
+
+ # After prebuild, so the Gradle cache can key on the generated build files.
+ - name: Setup Java
+ uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad # v6.1.0
+ with:
+ distribution: temurin
+ java-version: 17
+ cache: gradle
+
+ - name: Setup Android SDK
+ uses: android-actions/setup-android@be39fa834029ff78f1a44aa3bb0819b8fc2bd8fd # v4.0.4
+
+ - name: Prepare the release key
+ env:
+ KEYSTORE_BASE64: ${{ secrets.ANDROID_KEYSTORE_BASE64 }}
+ KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
+ KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
+ KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }}
+ run: |
+ if [ -z "$KEYSTORE_BASE64" ]; then
+ echo "::warning::ANDROID_KEYSTORE_BASE64 is not set; signing with the debug key. This APK can't be updated by a release-signed one."
+ echo "SIGNED_WITH=debug" >> "$GITHUB_ENV"
+ exit 0
+ fi
+ echo "$KEYSTORE_BASE64" | base64 --decode > "$RUNNER_TEMP/release.keystore"
+ {
+ echo "KANEO_ANDROID_KEYSTORE=$RUNNER_TEMP/release.keystore"
+ echo "KANEO_ANDROID_KEYSTORE_PASSWORD=$KEYSTORE_PASSWORD"
+ echo "KANEO_ANDROID_KEY_ALIAS=$KEY_ALIAS"
+ echo "KANEO_ANDROID_KEY_PASSWORD=$KEY_PASSWORD"
+ echo "SIGNED_WITH=release"
+ } >> "$GITHUB_ENV"
+
+ - name: Build the APK
+ working-directory: apps/mobile/android
+ run: ./gradlew assembleRelease --no-daemon
+
+ - name: Name the APK
+ id: apk
+ working-directory: apps/mobile
+ run: |
+ version=$(node -p 'require("./app.json").expo.version')
+ code=$(node -p 'require("./app.json").expo.android.versionCode')
+ suffix=""
+ [ "$SIGNED_WITH" = "debug" ] && suffix="-debug-signed"
+ name="kaneo-${version}-${code}${suffix}"
+ mv android/app/build/outputs/apk/release/app-release.apk "$RUNNER_TEMP/$name.apk"
+ echo "name=$name" >> "$GITHUB_OUTPUT"
+ echo "Built \`$name.apk\` (signed with the $SIGNED_WITH key)." >> "$GITHUB_STEP_SUMMARY"
+
+ - name: Upload the APK
+ uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
+ with:
+ name: ${{ steps.apk.outputs.name }}
+ path: ${{ runner.temp }}/${{ steps.apk.outputs.name }}.apk
+ if-no-files-found: error
+ retention-days: 30
diff --git a/apps/mobile/README.md b/apps/mobile/README.md
index 6403a26bf..f7d063a6e 100644
--- a/apps/mobile/README.md
+++ b/apps/mobile/README.md
@@ -58,7 +58,17 @@ pnpm --filter @kaneo/mobile test
## Release builds
-Use [EAS Build](https://docs.expo.dev/build/introduction/) (`pnpm dlx eas-cli build --platform all`) or build locally with `pnpm --filter @kaneo/mobile exec expo run:android --variant release` and `pnpm --filter @kaneo/mobile exec expo run:ios --configuration Release`. Local Android release builds are signed with the debug key; set up your own signing before publishing.
+The **Android APK** workflow (`.github/workflows/android.yml`) builds a release APK and uploads it as a workflow artifact. It runs on pull requests that touch `apps/mobile` and can be started by hand. Bump `version` and `android.versionCode` in `app.json` for each APK you hand out; Android only installs an update with a higher `versionCode`.
+
+Release builds are signed with the key named by `KANEO_ANDROID_KEYSTORE`, `KANEO_ANDROID_KEYSTORE_PASSWORD`, `KANEO_ANDROID_KEY_ALIAS` and `KANEO_ANDROID_KEY_PASSWORD` (see `plugins/with-release-signing.js`), and with the debug key when those are unset. An APK only installs over one signed with the same key, so create the release key once and keep it safe:
+
+```bash
+keytool -genkeypair -v -keystore kaneo-release.keystore -alias kaneo \
+ -keyalg RSA -keysize 4096 -validity 10000
+base64 -i kaneo-release.keystore | pbcopy # paste into the ANDROID_KEYSTORE_BASE64 secret
+```
+
+CI reads the repository secrets `ANDROID_KEYSTORE_BASE64`, `ANDROID_KEYSTORE_PASSWORD`, `ANDROID_KEY_ALIAS` and `ANDROID_KEY_PASSWORD`. Locally, export the `KANEO_ANDROID_*` variables and run `pnpm --filter @kaneo/mobile exec expo run:android --variant release`. For iOS, build with `pnpm --filter @kaneo/mobile exec expo run:ios --configuration Release`.
## Project layout
diff --git a/apps/mobile/app.json b/apps/mobile/app.json
index a29f26aae..0ec4a5d7a 100644
--- a/apps/mobile/app.json
+++ b/apps/mobile/app.json
@@ -66,6 +66,7 @@
"expo-background-task",
"expo-font",
"./plugins/with-gradle-memory",
+ "./plugins/with-release-signing",
[
"expo-build-properties",
{
diff --git a/apps/mobile/plugins/with-release-signing.js b/apps/mobile/plugins/with-release-signing.js
new file mode 100644
index 000000000..8d820bf56
--- /dev/null
+++ b/apps/mobile/plugins/with-release-signing.js
@@ -0,0 +1,44 @@
+const { withAppBuildGradle } = require("expo/config-plugins");
+
+const RELEASE_CONFIG = `
+ release {
+ def keystore = System.getenv("KANEO_ANDROID_KEYSTORE")
+ if (keystore) {
+ storeFile file(keystore)
+ storePassword System.getenv("KANEO_ANDROID_KEYSTORE_PASSWORD")
+ keyAlias System.getenv("KANEO_ANDROID_KEY_ALIAS")
+ keyPassword System.getenv("KANEO_ANDROID_KEY_PASSWORD")
+ }
+ }`;
+
+/**
+ * Signs release builds with the keystore named by `KANEO_ANDROID_KEYSTORE` (plus its passwords and
+ * alias), and falls back to the debug key when it is unset so local release builds keep working.
+ * APKs must keep one key for updates to install over each other, so CI sets these from secrets.
+ */
+function addReleaseSigning(gradle) {
+ if (gradle.includes("KANEO_ANDROID_KEYSTORE")) return gradle;
+ const withConfig = gradle.replace(
+ /(signingConfigs \{\n\s*debug \{[^}]*\})/,
+ `$1${RELEASE_CONFIG}`,
+ );
+ const result = withConfig.replace(
+ /(release \{\n(?:\s*\/\/[^\n]*\n)*\s*)signingConfig signingConfigs\.debug/,
+ `$1signingConfig System.getenv("KANEO_ANDROID_KEYSTORE") ? signingConfigs.release : signingConfigs.debug`,
+ );
+ // Fail the prebuild rather than quietly ship debug-signed builds if the template changes.
+ if (withConfig === gradle || result === withConfig)
+ throw new Error(
+ "with-release-signing: app/build.gradle no longer matches; update the plugin",
+ );
+ return result;
+}
+
+module.exports = function withReleaseSigning(config) {
+ return withAppBuildGradle(config, (cfg) => {
+ cfg.modResults.contents = addReleaseSigning(cfg.modResults.contents);
+ return cfg;
+ });
+};
+
+module.exports.addReleaseSigning = addReleaseSigning;