diff --git a/.github/actions/reusable-setup/action.yaml b/.github/actions/reusable-setup/action.yaml index a4e004c9..f909fce4 100644 --- a/.github/actions/reusable-setup/action.yaml +++ b/.github/actions/reusable-setup/action.yaml @@ -3,7 +3,6 @@ description: Install the pinned Node, pnpm, Ruby, and optional Bundler dependenc inputs: bundle: - type: boolean required: true runs: diff --git a/.github/workflows/code-quality.yml b/.github/workflows/code-quality.yml index f740c5a2..eb72246f 100644 --- a/.github/workflows/code-quality.yml +++ b/.github/workflows/code-quality.yml @@ -50,3 +50,6 @@ jobs: - name: Firestore Rules run: pnpm test:firestore-rules + + - name: Verify release artifact encryption + run: node --test scripts/release-artifact.test.mjs diff --git a/.github/workflows/release-throwl.yaml b/.github/workflows/release-throwl.yaml index 4389c081..3ceffc71 100644 --- a/.github/workflows/release-throwl.yaml +++ b/.github/workflows/release-throwl.yaml @@ -2,8 +2,21 @@ name: Distribute app to Android & iOS stores 🚀 on: push: + branches: [master] tags: - '*' + paths: + - 'apps/throwl/**' + - 'libs/**' + - 'scripts/**' + - '.github/actions/**' + - '.github/workflows/release-throwl.yaml' + - '.github/workflows/reusable-deploy.yaml' + - '.nvmrc' + - 'package.json' + - 'pnpm-*.yaml' + - 'nx.json' + - 'tsconfig*.json' workflow_dispatch: inputs: deploy: @@ -12,10 +25,18 @@ on: type: choice required: false options: [ios, android, both] + publish: + description: Upload the signed builds to the stores + type: boolean + default: true permissions: contents: read +concurrency: + group: ${{ github.event_name == 'push' && github.ref_type == 'branch' && 'master-native-build' || github.run_id }} + cancel-in-progress: ${{ github.event_name == 'push' && github.ref_type == 'branch' }} + jobs: validate: runs-on: ubuntu-latest @@ -58,7 +79,9 @@ jobs: os: ubuntu-24.04 working_directory: apps/throwl/android store: Google Play Store + publish: ${{ (github.event_name == 'push' && github.ref_type == 'tag') || (github.event_name == 'workflow_dispatch' && inputs.publish) }} secrets: + RELEASE_ARTIFACT_KEY: ${{ secrets.RELEASE_ARTIFACT_KEY }} ANDROID_KEYSTORE_FILE: ${{ secrets.ANDROID_KEYSTORE_FILE }} ANDROID_KEYSTORE_KEY_ALIAS: ${{ secrets.ANDROID_KEYSTORE_ALIAS }} ANDROID_KEYSTORE_KEY_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }} @@ -77,7 +100,9 @@ jobs: os: macos-latest working_directory: apps/throwl/ios store: TestFlight + publish: ${{ (github.event_name == 'push' && github.ref_type == 'tag') || (github.event_name == 'workflow_dispatch' && inputs.publish) }} secrets: + RELEASE_ARTIFACT_KEY: ${{ secrets.RELEASE_ARTIFACT_KEY }} IOS_APP_STORE_CONNECT_TEAM_ID: ${{ secrets.APP_STORE_CONNECT_TEAM_ID }} IOS_DEVELOPER_APP_ID: ${{ secrets.DEVELOPER_APP_ID }} IOS_DEVELOPER_APP_IDENTIFIER: ${{ secrets.DEVELOPER_APP_IDENTIFIER }} diff --git a/.github/workflows/reusable-deploy.yaml b/.github/workflows/reusable-deploy.yaml index c31f73fa..15c7c373 100644 --- a/.github/workflows/reusable-deploy.yaml +++ b/.github/workflows/reusable-deploy.yaml @@ -17,7 +17,14 @@ on: required: true description: One of `apps/throwl/ios` or `apps/throwl/android` + publish: + type: boolean + required: true + secrets: + RELEASE_ARTIFACT_KEY: + required: true + # iOS IOS_APP_STORE_CONNECT_TEAM_ID: IOS_DEVELOPER_APP_ID: @@ -43,14 +50,13 @@ permissions: contents: read jobs: - build_and_deploy: - name: Build and Deploy to ${{ inputs.store }} + build: + name: Build for ${{ inputs.store }} + outputs: + artifact_id: ${{ steps.bundle.outputs.artifact-id }} runs-on: ${{ inputs.os }} permissions: contents: read - concurrency: - group: ${{ github.workflow }}-${{ inputs.os }}-${{ inputs.working_directory }} - cancel-in-progress: true steps: - name: Checkout repository @@ -61,6 +67,12 @@ jobs: with: bundle: true + - name: Check release artifact key + if: inputs.publish || github.event_name == 'workflow_dispatch' + run: node scripts/release-artifact.mjs check + env: + RELEASE_ARTIFACT_KEY: ${{ secrets.RELEASE_ARTIFACT_KEY }} + #region Android - name: Use JAVA 22 if: ${{ endsWith(inputs.working_directory, '/android') }} @@ -69,6 +81,13 @@ jobs: distribution: 'zulu' java-version: '22.0.2' + - name: Cache Gradle dependencies + if: ${{ endsWith(inputs.working_directory, '/android') }} + uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6 + with: + cache-provider: basic + cache-read-only: false + - name: Configure Keystore if: ${{ endsWith(inputs.working_directory, '/android') }} working-directory: ${{ inputs.working_directory }} @@ -97,20 +116,6 @@ jobs: KEYSTORE_KEY_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_KEY_PASSWORD }} KEYSTORE_STORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_STORE_PASSWORD }} - - name: Create Google Play Config file - if: ${{ endsWith(inputs.working_directory, '/android') }} - working-directory: ${{ inputs.working_directory }} - run: | - python3 - <<'PY' - import base64, os, pathlib - - wd = pathlib.Path(".") - play_b64 = os.environ["PLAY_CONFIG_JSON"] - (wd / "playstore_config.json").write_bytes(base64.b64decode(play_b64)) - PY - env: - PLAY_CONFIG_JSON: ${{ secrets.ANDROID_PLAY_CONFIG_JSON }} - - name: Create google-services.json (Firebase) if: ${{ endsWith(inputs.working_directory, '/android') }} working-directory: ${{ inputs.working_directory }} @@ -153,15 +158,22 @@ jobs: uses: maxim-lobanov/setup-xcode@ed7a3b1fda3918c0306d1b724322adc0b8cc0a90 # v1.7.0 with: xcode-version: latest-stable + - name: Cache CocoaPods downloads + if: ${{ endsWith(inputs.working_directory, '/ios') }} + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ~/Library/Caches/CocoaPods + key: cocoapods-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('apps/throwl/Gemfile.lock', 'apps/throwl/ios/Podfile.lock') }} + restore-keys: cocoapods-${{ runner.os }}-${{ runner.arch }}- #endregion - - name: Distribute app to ${{ inputs.store }} 🚀 + - name: Build signed store bundle working-directory: apps/throwl run: | if [[ "${{ inputs.working_directory }}" == *"/ios" ]]; then - bundle exec fastlane ios beta + bundle exec fastlane ios beta upload:false else - bundle exec fastlane android beta + bundle exec fastlane android beta upload:false fi env: # iOS (only set when iOS) @@ -175,3 +187,108 @@ jobs: APPLE_KEY_ID: ${{ (endsWith(inputs.working_directory, '/ios') && secrets.IOS_APPLE_KEY_ID) || '' }} APPLE_ISSUER_ID: ${{ (endsWith(inputs.working_directory, '/ios') && secrets.IOS_APPLE_ISSUER_ID) || '' }} APPLE_KEY_CONTENT: ${{ (endsWith(inputs.working_directory, '/ios') && secrets.IOS_APPLE_KEY_CONTENT) || '' }} + + - name: Encrypt signed bundle + if: inputs.publish || github.event_name == 'workflow_dispatch' + run: | + if [[ "$APP_PLATFORM" == ios ]]; then + bundle=apps/throwl/fastlane/output/Throwl.ipa + else + bundle=apps/throwl/android/app/build/outputs/bundle/release/app-release.aab + fi + node scripts/release-artifact.mjs encrypt "$bundle" "$RUNNER_TEMP/release-bundle.enc" + env: + APP_PLATFORM: ${{ endsWith(inputs.working_directory, '/ios') && 'ios' || 'android' }} + RELEASE_ARTIFACT_KEY: ${{ secrets.RELEASE_ARTIFACT_KEY }} + RELEASE_ARTIFACT_CONTEXT: ${{ github.run_id }}:${{ github.sha }}:${{ inputs.working_directory }} + + - name: Retain encrypted bundle for upload retries + if: inputs.publish || github.event_name == 'workflow_dispatch' + id: bundle + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 + with: + name: release-${{ endsWith(inputs.working_directory, '/ios') && 'ios' || 'android' }}-${{ github.run_attempt }} + path: ${{ runner.temp }}/release-bundle.enc + if-no-files-found: error + compression-level: 0 + retention-days: 7 + + upload: + name: Upload to ${{ inputs.store }} + needs: build + if: inputs.publish + runs-on: ${{ inputs.os }} + concurrency: + group: store-upload-${{ inputs.working_directory }} + cancel-in-progress: false + steps: + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + + - name: Set up Node + uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0 + with: + node-version-file: .nvmrc + package-manager-cache: false + + - name: Set up Ruby + uses: ruby/setup-ruby@145243922b4465b78e04dd66294782c7de45901f # v1.345.0 + with: + working-directory: apps/throwl + bundler-cache: true + + - name: Download exact bundle from build job + env: + BUILD_ARTIFACT_ID: ${{ needs.build.outputs.artifact_id }} + run: test -n "$BUILD_ARTIFACT_ID" + + - name: Download encrypted bundle + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8 + with: + artifact-ids: ${{ needs.build.outputs.artifact_id }} + path: ${{ runner.temp }}/release-artifact + + - name: Decrypt signed bundle + run: | + node scripts/release-artifact.mjs decrypt "$RUNNER_TEMP/release-artifact/release-bundle.enc" "$RUNNER_TEMP/$BUNDLE_NAME" + env: + BUNDLE_NAME: ${{ endsWith(inputs.working_directory, '/ios') && 'Throwl.ipa' || 'app-release.aab' }} + RELEASE_ARTIFACT_KEY: ${{ secrets.RELEASE_ARTIFACT_KEY }} + RELEASE_ARTIFACT_CONTEXT: ${{ github.run_id }}:${{ github.sha }}:${{ inputs.working_directory }} + + - name: Use latest Xcode + if: ${{ endsWith(inputs.working_directory, '/ios') }} + uses: maxim-lobanov/setup-xcode@ed7a3b1fda3918c0306d1b724322adc0b8cc0a90 # v1.7.0 + with: + xcode-version: latest-stable + + - name: Upload to TestFlight + if: ${{ endsWith(inputs.working_directory, '/ios') }} + working-directory: apps/throwl + run: bundle exec fastlane ios upload "ipa:$RUNNER_TEMP/Throwl.ipa" + env: + DEVELOPER_APP_IDENTIFIER: ${{ secrets.IOS_DEVELOPER_APP_IDENTIFIER }} + APP_STORE_CONNECT_TEAM_ID: ${{ secrets.IOS_APP_STORE_CONNECT_TEAM_ID }} + FASTLANE_APPLE_ID: ${{ secrets.IOS_FASTLANE_APPLE_ID }} + APPLE_KEY_ID: ${{ secrets.IOS_APPLE_KEY_ID }} + APPLE_ISSUER_ID: ${{ secrets.IOS_APPLE_ISSUER_ID }} + APPLE_KEY_CONTENT: ${{ secrets.IOS_APPLE_KEY_CONTENT }} + + - name: Create Google Play Config file + if: ${{ endsWith(inputs.working_directory, '/android') }} + working-directory: ${{ inputs.working_directory }} + run: | + python3 - <<'PY' + import base64, os, pathlib + + wd = pathlib.Path(".") + play_b64 = os.environ["PLAY_CONFIG_JSON"] + (wd / "playstore_config.json").write_bytes(base64.b64decode(play_b64)) + PY + env: + PLAY_CONFIG_JSON: ${{ secrets.ANDROID_PLAY_CONFIG_JSON }} + + - name: Upload to Play Internal + if: ${{ endsWith(inputs.working_directory, '/android') }} + working-directory: apps/throwl + run: bundle exec fastlane android upload "aab:$RUNNER_TEMP/app-release.aab" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f7e0d950..e8e7aab1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -87,3 +87,29 @@ so the owner should protect that tag namespace and `master` with GitHub rulesets App Check is optional post-publication abuse hardening. Firestore Security Rules, not App Check, remain the authorization boundary. + +### Release build retries + +Before merging the release workflow change, a maintainer must provision the +`RELEASE_ARTIFACT_KEY` GitHub Actions repository secret once. From an +authenticated checkout, generate it without printing it: + +```sh +openssl rand -hex 32 | gh secret set RELEASE_ARTIFACT_KEY +``` + +The build job stores only the encrypted signed bundle, for seven days. If store +upload fails, fix the store issue and choose **Re-run failed jobs** on the same +workflow run. The successful build job's artifact ID selects the original +bundle. If the artifact expired or the key was rotated, rerun the build too. +Keep the key stable while any upload retries are pending. + +Pushes to `master` that change the app, dependencies, or release tooling validate +and build both platforms. These runs use release signing and warm native caches +without retaining bundles or uploading to the stores. A newer qualifying push +cancels an older post-merge run. Tag releases run independently and can restore +eligible caches from `master`. + +To check encrypted bundle creation on GitHub without uploading to the stores, +run the release workflow manually with `publish: false`. That run requires +release signing and the artifact secret, and retains the encrypted bundles. diff --git a/apps/throwl/fastlane/Fastfile b/apps/throwl/fastlane/Fastfile index 119d0e2e..2c471b8c 100644 --- a/apps/throwl/fastlane/Fastfile +++ b/apps/throwl/fastlane/Fastfile @@ -5,6 +5,16 @@ APPLE_ISSUER_ID = ENV.fetch("APPLE_ISSUER_ID", nil) APPLE_KEY_ID = ENV.fetch("APPLE_KEY_ID", nil) APPLE_KEY_CONTENT = ENV.fetch("APPLE_KEY_CONTENT", nil) +def testflight_api_key + app_store_connect_api_key( + key_id: APPLE_KEY_ID, + issuer_id: APPLE_ISSUER_ID, + key_content: APPLE_KEY_CONTENT, + duration: 1200, + in_house: false + ) +end + platform :ios do desc "Bump marketing version and build number" lane :bump_version do |options| @@ -16,26 +26,18 @@ platform :ios do end desc "Build (and optionally upload) a TestFlight release bundle" - lane :beta do + lane :beta do |options| setup_ci cocoapods( - clean_install: true, + deployment: true, podfile: "ios/Podfile" ) - api_key = app_store_connect_api_key( - key_id: APPLE_KEY_ID, - issuer_id: APPLE_ISSUER_ID, - key_content: APPLE_KEY_CONTENT, - duration: 1200, - in_house: false - ) - match( type: "appstore", app_identifier: DEVELOPER_APP_IDENTIFIER, - api_key: api_key + api_key: testflight_api_key ) build_app( @@ -43,11 +45,20 @@ platform :ios do workspace: "ios/Throwl.xcworkspace", clean: true, export_method: "app-store", - output_directory: "fastlane/output" + output_directory: "fastlane/output", + output_name: "Throwl.ipa" ) + if options.fetch(:upload, true).to_s.downcase == "true" + upload(ipa: lane_context[SharedValues::IPA_OUTPUT_PATH]) + end + end + + desc "Upload an existing signed IPA to TestFlight" + lane :upload do |options| upload_to_testflight( - api_key: api_key, + ipa: options.fetch(:ipa), + api_key: testflight_api_key, app_identifier: DEVELOPER_APP_IDENTIFIER, skip_waiting_for_build_processing: true, skip_submission: true, @@ -69,7 +80,7 @@ platform :android do desc "Build (and optionally upload) a Beta/Release bundle" lane :beta do |options| - upload = options.fetch(:upload, ENV.fetch("UPLOAD", "true")).to_s.downcase == "true" + should_upload = options.fetch(:upload, ENV.fetch("UPLOAD", "true")).to_s.downcase == "true" gradle( project_dir: "android", @@ -78,14 +89,19 @@ platform :android do aab_path = "android/app/build/outputs/bundle/release/app-release.aab" - if upload - upload_to_play_store( - aab: aab_path, - track: "internal" - ) + if should_upload + upload(aab: aab_path) else UI.important("UPLOAD=false -> skipping upload_to_play_store (dry run)") UI.message("Built AAB should be at: #{aab_path}") end end + + desc "Upload an existing signed AAB to Play Internal" + lane :upload do |options| + upload_to_play_store( + aab: options.fetch(:aab), + track: "internal" + ) + end end diff --git a/apps/throwl/fastlane/README.md b/apps/throwl/fastlane/README.md index e73a5f65..0889aea7 100644 --- a/apps/throwl/fastlane/README.md +++ b/apps/throwl/fastlane/README.md @@ -30,6 +30,14 @@ Bump marketing version and build number Build (and optionally upload) a TestFlight release bundle +### ios upload + +```sh +[bundle exec] fastlane ios upload +``` + +Upload an existing signed IPA to TestFlight + --- ## Android @@ -50,6 +58,14 @@ Bump versionName and versionCode Build (and optionally upload) a Beta/Release bundle +### android upload + +```sh +[bundle exec] fastlane android upload +``` + +Upload an existing signed AAB to Play Internal + --- This README.md is auto-generated and will be re-generated every time [_fastlane_](https://fastlane.tools) is run. diff --git a/scripts/release-artifact.mjs b/scripts/release-artifact.mjs new file mode 100644 index 00000000..16893782 --- /dev/null +++ b/scripts/release-artifact.mjs @@ -0,0 +1,57 @@ +import { createCipheriv, createDecipheriv, randomBytes } from 'node:crypto'; +import { readFile, writeFile } from 'node:fs/promises'; + +const [operation, source, destination] = process.argv.slice(2); +const secret = process.env.RELEASE_ARTIFACT_KEY ?? ''; + +if (!/^[0-9a-f]{64}$/i.test(secret)) { + throw new Error( + 'RELEASE_ARTIFACT_KEY must contain 32 random bytes encoded as hex.', + ); +} + +if (operation !== 'check') { + const context = process.env.RELEASE_ARTIFACT_CONTEXT; + if (!context || !source || !destination) { + throw new Error( + 'The release artifact context and both file paths are required.', + ); + } + + const key = Buffer.from(secret, 'hex'); + const associatedData = Buffer.from(context); + const input = await readFile(source); + const nonceLength = 12; + const tagLength = 16; + const options = { flag: 'wx', mode: 0o600 }; + + if (operation === 'encrypt') { + const nonce = randomBytes(nonceLength); + const cipher = createCipheriv('aes-256-gcm', key, nonce); + cipher.setAAD(associatedData); + const ciphertext = Buffer.concat([cipher.update(input), cipher.final()]); + await writeFile( + destination, + Buffer.concat([nonce, cipher.getAuthTag(), ciphertext]), + options, + ); + } else if (operation === 'decrypt') { + if (input.length <= nonceLength + tagLength) { + throw new Error('The encrypted release artifact is truncated.'); + } + + const nonce = input.subarray(0, nonceLength); + const tag = input.subarray(nonceLength, nonceLength + tagLength); + const ciphertext = input.subarray(nonceLength + tagLength); + const decipher = createDecipheriv('aes-256-gcm', key, nonce); + decipher.setAAD(associatedData); + decipher.setAuthTag(tag); + const plaintext = Buffer.concat([ + decipher.update(ciphertext), + decipher.final(), + ]); + await writeFile(destination, plaintext, options); + } else { + throw new Error('Expected check, encrypt or decrypt.'); + } +} diff --git a/scripts/release-artifact.test.mjs b/scripts/release-artifact.test.mjs new file mode 100644 index 00000000..94965bc5 --- /dev/null +++ b/scripts/release-artifact.test.mjs @@ -0,0 +1,52 @@ +import assert from 'node:assert/strict'; +import { spawnSync } from 'node:child_process'; +import { randomBytes } from 'node:crypto'; +import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { test } from 'node:test'; +import { fileURLToPath } from 'node:url'; + +const script = fileURLToPath( + new URL('./release-artifact.mjs', import.meta.url), +); + +test('the upload job recovers the exact signed bundle and rejects altered handoffs', async (t) => { + const directory = await mkdtemp(join(tmpdir(), 'throwl-release-test-')); + t.after(() => rm(directory, { recursive: true, force: true })); + + const original = join(directory, 'original.ipa'); + const encrypted = join(directory, 'release-bundle.enc'); + const recovered = join(directory, 'recovered.ipa'); + const rejected = join(directory, 'rejected.ipa'); + const bytes = randomBytes(8192); + await writeFile(original, bytes); + + const key = randomBytes(32).toString('hex'); + const context = 'release-run:release-commit:apps/throwl/ios'; + const run = (args, secret = key, release = context) => + spawnSync(process.execPath, [script, ...args], { + env: { RELEASE_ARTIFACT_KEY: secret, RELEASE_ARTIFACT_CONTEXT: release }, + encoding: 'utf8', + }); + + assert.notEqual(run(['check'], '').status, 0); + + const encryption = run(['encrypt', original, encrypted]); + assert.equal(encryption.status, 0, encryption.stderr); + assert.notDeepEqual(await readFile(encrypted), bytes); + + const decryption = run(['decrypt', encrypted, recovered]); + assert.equal(decryption.status, 0, decryption.stderr); + assert.deepEqual(await readFile(recovered), bytes); + + const decrypt = ['decrypt', encrypted, rejected]; + assert.notEqual(run(decrypt, randomBytes(32).toString('hex')).status, 0); + assert.notEqual(run(decrypt, key, 'different-release').status, 0); + + const tampered = await readFile(encrypted); + tampered[tampered.length - 1] ^= 1; + await writeFile(encrypted, tampered); + assert.notEqual(run(decrypt).status, 0); + await assert.rejects(readFile(rejected), { code: 'ENOENT' }); +});