|
| 1 | +name: Android Full Nightly |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + target_ref: |
| 7 | + description: Branch, tag, or SHA to build. |
| 8 | + required: true |
| 9 | + default: develop |
| 10 | + force: |
| 11 | + description: Build even when no functional files changed since the previous nightly. |
| 12 | + required: true |
| 13 | + type: boolean |
| 14 | + default: false |
| 15 | + publish: |
| 16 | + description: Publish or update the public nightly GitHub prerelease. |
| 17 | + required: true |
| 18 | + type: boolean |
| 19 | + default: true |
| 20 | + schedule: |
| 21 | + - cron: "37 2 * * *" |
| 22 | + |
| 23 | +permissions: |
| 24 | + contents: write |
| 25 | + |
| 26 | +concurrency: |
| 27 | + group: android-full-nightly |
| 28 | + cancel-in-progress: false |
| 29 | + |
| 30 | +env: |
| 31 | + APK_NAME: sdai-full-nightly.apk |
| 32 | + NIGHTLY_TAG: nightly |
| 33 | + NIGHTLY_RELEASE_NAME: Android Full Nightly |
| 34 | + |
| 35 | +jobs: |
| 36 | + nightly: |
| 37 | + name: Build Android full nightly |
| 38 | + runs-on: ubuntu-24.04 |
| 39 | + timeout-minutes: 90 |
| 40 | + env: |
| 41 | + TARGET_REF: ${{ github.event_name == 'workflow_dispatch' && inputs.target_ref || 'develop' }} |
| 42 | + FORCE_NIGHTLY: ${{ github.event_name == 'workflow_dispatch' && inputs.force || false }} |
| 43 | + PUBLISH_NIGHTLY: ${{ github.event_name != 'workflow_dispatch' || inputs.publish }} |
| 44 | + |
| 45 | + steps: |
| 46 | + - name: Checkout target ref |
| 47 | + uses: actions/checkout@v4 |
| 48 | + with: |
| 49 | + ref: ${{ github.event_name == 'workflow_dispatch' && inputs.target_ref || 'develop' }} |
| 50 | + fetch-depth: 0 |
| 51 | + |
| 52 | + - name: Check whether a nightly is needed |
| 53 | + id: plan |
| 54 | + shell: bash |
| 55 | + run: | |
| 56 | + set -euo pipefail |
| 57 | +
|
| 58 | + git fetch --force origin "refs/tags/${NIGHTLY_TAG}:refs/tags/${NIGHTLY_TAG}" || true |
| 59 | +
|
| 60 | + target_sha="$(git rev-parse HEAD)" |
| 61 | + echo "target_sha=${target_sha}" >> "${GITHUB_OUTPUT}" |
| 62 | +
|
| 63 | + if ! git rev-parse --verify "${NIGHTLY_TAG}^{commit}" >/dev/null 2>&1; then |
| 64 | + echo "should_build=true" >> "${GITHUB_OUTPUT}" |
| 65 | + echo "reason=No previous nightly tag exists." >> "${GITHUB_OUTPUT}" |
| 66 | + { |
| 67 | + echo "### Android full nightly" |
| 68 | + echo |
| 69 | + echo "No previous nightly tag exists. Building ${target_sha}." |
| 70 | + } >> "${GITHUB_STEP_SUMMARY}" |
| 71 | + exit 0 |
| 72 | + fi |
| 73 | +
|
| 74 | + if [[ "${FORCE_NIGHTLY}" == "true" ]]; then |
| 75 | + echo "should_build=true" >> "${GITHUB_OUTPUT}" |
| 76 | + echo "reason=Manual force build was requested." >> "${GITHUB_OUTPUT}" |
| 77 | + { |
| 78 | + echo "### Android full nightly" |
| 79 | + echo |
| 80 | + echo "Manual force build requested for ${target_sha}." |
| 81 | + } >> "${GITHUB_STEP_SUMMARY}" |
| 82 | + exit 0 |
| 83 | + fi |
| 84 | +
|
| 85 | + git diff --name-only "${NIGHTLY_TAG}..HEAD" > "${RUNNER_TEMP}/nightly-changed-files.txt" |
| 86 | +
|
| 87 | + functional_pattern='^(app/|core/|data/|demo/|domain/|feature/|network/|presentation/|storage/|build-logic/|gradle/|build\.gradle\.kts$|settings\.gradle\.kts$|gradle\.properties$|gradlew$|gradlew\.bat$)' |
| 88 | + if grep -Eq "${functional_pattern}" "${RUNNER_TEMP}/nightly-changed-files.txt"; then |
| 89 | + echo "should_build=true" >> "${GITHUB_OUTPUT}" |
| 90 | + echo "reason=Functional files changed since the previous nightly." >> "${GITHUB_OUTPUT}" |
| 91 | + else |
| 92 | + echo "should_build=false" >> "${GITHUB_OUTPUT}" |
| 93 | + echo "reason=Only documentation, website, or repository metadata changed since the previous nightly." >> "${GITHUB_OUTPUT}" |
| 94 | + fi |
| 95 | +
|
| 96 | + { |
| 97 | + echo "### Android full nightly" |
| 98 | + echo |
| 99 | + echo "Target ref: \`${TARGET_REF}\`" |
| 100 | + echo "Target commit: \`${target_sha}\`" |
| 101 | + echo "Decision: \`$(grep '^should_build=' "${GITHUB_OUTPUT}" | tail -n 1 | cut -d= -f2)\`" |
| 102 | + echo |
| 103 | + echo "Changed files since \`${NIGHTLY_TAG}\`:" |
| 104 | + echo |
| 105 | + sed 's/^/- `/' "${RUNNER_TEMP}/nightly-changed-files.txt" | sed 's/$/`/' || true |
| 106 | + } >> "${GITHUB_STEP_SUMMARY}" |
| 107 | +
|
| 108 | + - name: Set up JDK 17 |
| 109 | + if: steps.plan.outputs.should_build == 'true' |
| 110 | + uses: actions/setup-java@v4 |
| 111 | + with: |
| 112 | + distribution: temurin |
| 113 | + java-version: "17" |
| 114 | + |
| 115 | + - name: Set up Gradle |
| 116 | + if: steps.plan.outputs.should_build == 'true' |
| 117 | + uses: gradle/actions/setup-gradle@v4 |
| 118 | + |
| 119 | + - name: Build unsigned full release APK |
| 120 | + if: steps.plan.outputs.should_build == 'true' |
| 121 | + shell: bash |
| 122 | + run: | |
| 123 | + set -euo pipefail |
| 124 | +
|
| 125 | + ./gradlew :app:assembleFullRelease \ |
| 126 | + --no-daemon \ |
| 127 | + -Dkotlin.native.ignoreDisabledTargets=true \ |
| 128 | + -Pkotlin.native.ignoreDisabledTargets=true |
| 129 | +
|
| 130 | + - name: Sign APK with apksigner key and certificate |
| 131 | + if: steps.plan.outputs.should_build == 'true' |
| 132 | + id: sign |
| 133 | + shell: bash |
| 134 | + env: |
| 135 | + ANDROID_NIGHTLY_CERT_PEM_BASE64: ${{ secrets.ANDROID_NIGHTLY_CERT_PEM_BASE64 }} |
| 136 | + ANDROID_NIGHTLY_KEY_PASSWORD: ${{ secrets.ANDROID_NIGHTLY_KEY_PASSWORD }} |
| 137 | + ANDROID_NIGHTLY_KEY_PK8_BASE64: ${{ secrets.ANDROID_NIGHTLY_KEY_PK8_BASE64 }} |
| 138 | + run: | |
| 139 | + set -euo pipefail |
| 140 | +
|
| 141 | + : "${ANDROID_NIGHTLY_CERT_PEM_BASE64:?Missing ANDROID_NIGHTLY_CERT_PEM_BASE64 secret.}" |
| 142 | + : "${ANDROID_NIGHTLY_KEY_PK8_BASE64:?Missing ANDROID_NIGHTLY_KEY_PK8_BASE64 secret.}" |
| 143 | +
|
| 144 | + unsigned_apk="$(find app/android/build/outputs/apk/full/release -type f -name '*-unsigned.apk' -print -quit)" |
| 145 | + if [[ -z "${unsigned_apk}" ]]; then |
| 146 | + echo "Could not find unsigned full release APK." >&2 |
| 147 | + find app/android/build/outputs/apk/full/release -type f -print >&2 || true |
| 148 | + exit 1 |
| 149 | + fi |
| 150 | +
|
| 151 | + build_tools="$(find "${ANDROID_HOME}/build-tools" -mindepth 1 -maxdepth 1 -type d -print | sort -V | tail -n 1)" |
| 152 | + apksigner="${build_tools}/apksigner" |
| 153 | + zipalign="${build_tools}/zipalign" |
| 154 | +
|
| 155 | + signing_dir="${RUNNER_TEMP}/nightly-signing" |
| 156 | + output_dir="${RUNNER_TEMP}/nightly-output" |
| 157 | + install -m 700 -d "${signing_dir}" "${output_dir}" |
| 158 | +
|
| 159 | + key_file="${signing_dir}/nightly.pk8" |
| 160 | + cert_file="${signing_dir}/nightly.x509.pem" |
| 161 | + aligned_apk="${output_dir}/sdai-full-nightly-aligned.apk" |
| 162 | + signed_apk="${output_dir}/${APK_NAME}" |
| 163 | +
|
| 164 | + printf "%s" "${ANDROID_NIGHTLY_KEY_PK8_BASE64}" | base64 --decode > "${key_file}" |
| 165 | + printf "%s" "${ANDROID_NIGHTLY_CERT_PEM_BASE64}" | base64 --decode > "${cert_file}" |
| 166 | + chmod 600 "${key_file}" "${cert_file}" |
| 167 | +
|
| 168 | + "${zipalign}" -p -f 4 "${unsigned_apk}" "${aligned_apk}" |
| 169 | +
|
| 170 | + sign_args=(sign --key "${key_file}" --cert "${cert_file}" --out "${signed_apk}") |
| 171 | + if [[ -n "${ANDROID_NIGHTLY_KEY_PASSWORD:-}" ]]; then |
| 172 | + sign_args+=(--key-pass "pass:${ANDROID_NIGHTLY_KEY_PASSWORD}") |
| 173 | + fi |
| 174 | + "${apksigner}" "${sign_args[@]}" "${aligned_apk}" |
| 175 | + "${apksigner}" verify --print-certs "${signed_apk}" > "${output_dir}/apksigner.txt" |
| 176 | +
|
| 177 | + sha256sum "${signed_apk}" > "${output_dir}/${APK_NAME}.sha256" |
| 178 | + echo "signed_apk=${signed_apk}" >> "${GITHUB_OUTPUT}" |
| 179 | + echo "sha256_file=${output_dir}/${APK_NAME}.sha256" >> "${GITHUB_OUTPUT}" |
| 180 | + echo "apksigner_report=${output_dir}/apksigner.txt" >> "${GITHUB_OUTPUT}" |
| 181 | +
|
| 182 | + - name: Prepare nightly release notes |
| 183 | + if: steps.plan.outputs.should_build == 'true' |
| 184 | + id: metadata |
| 185 | + shell: bash |
| 186 | + run: | |
| 187 | + set -euo pipefail |
| 188 | +
|
| 189 | + output_dir="${RUNNER_TEMP}/nightly-output" |
| 190 | + release_notes="${output_dir}/release-notes.md" |
| 191 | + target_sha="${{ steps.plan.outputs.target_sha }}" |
| 192 | + version_name="$(awk -F '"' '/^versionName = / { print $2; exit }' gradle/libs.versions.toml)" |
| 193 | + version_code="$(awk -F '"' '/^versionCode = / { print $2; exit }' gradle/libs.versions.toml)" |
| 194 | + apk_sha256="$(cut -d ' ' -f 1 "${{ steps.sign.outputs.sha256_file }}")" |
| 195 | + cert_sha256="$(awk -F': ' '/Signer #1 certificate SHA-256 digest:/ { print $2; exit }' "${{ steps.sign.outputs.apksigner_report }}")" |
| 196 | + download_url="https://github.com/${GITHUB_REPOSITORY}/releases/download/${NIGHTLY_TAG}/${APK_NAME}" |
| 197 | +
|
| 198 | + cat > "${release_notes}" <<EOF |
| 199 | + Automated Android full nightly build. |
| 200 | +
|
| 201 | + - Target ref: \`${TARGET_REF}\` |
| 202 | + - Commit: \`${target_sha}\` |
| 203 | + - Version: \`${version_name} (${version_code})\` |
| 204 | + - APK SHA-256: \`${apk_sha256}\` |
| 205 | + - Signing certificate SHA-256: \`${cert_sha256}\` |
| 206 | +
|
| 207 | + This is a prerelease build from the moving \`${NIGHTLY_TAG}\` tag. It is not a Play Store, F-Droid, or stable project release. |
| 208 | + EOF |
| 209 | +
|
| 210 | + echo "release_notes=${release_notes}" >> "${GITHUB_OUTPUT}" |
| 211 | + { |
| 212 | + echo "### Nightly artifact" |
| 213 | + echo |
| 214 | + echo "Static APK URL: ${download_url}" |
| 215 | + echo "APK SHA-256: \`${apk_sha256}\`" |
| 216 | + echo "Signing certificate SHA-256: \`${cert_sha256}\`" |
| 217 | + } >> "${GITHUB_STEP_SUMMARY}" |
| 218 | +
|
| 219 | + - name: Publish GitHub prerelease |
| 220 | + if: steps.plan.outputs.should_build == 'true' && env.PUBLISH_NIGHTLY == 'true' |
| 221 | + shell: bash |
| 222 | + env: |
| 223 | + GH_TOKEN: ${{ github.token }} |
| 224 | + run: | |
| 225 | + set -euo pipefail |
| 226 | +
|
| 227 | + target_sha="${{ steps.plan.outputs.target_sha }}" |
| 228 | + git config user.name "github-actions[bot]" |
| 229 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 230 | + git tag -f "${NIGHTLY_TAG}" "${target_sha}" |
| 231 | + git push --force origin "refs/tags/${NIGHTLY_TAG}" |
| 232 | +
|
| 233 | + if gh release view "${NIGHTLY_TAG}" >/dev/null 2>&1; then |
| 234 | + while IFS= read -r asset_name; do |
| 235 | + case "${asset_name}" in |
| 236 | + "${APK_NAME}"|"${APK_NAME}.sha256") ;; |
| 237 | + *) gh release delete-asset "${NIGHTLY_TAG}" "${asset_name}" --yes ;; |
| 238 | + esac |
| 239 | + done < <(gh release view "${NIGHTLY_TAG}" --json assets --jq '.assets[].name') |
| 240 | +
|
| 241 | + gh release upload "${NIGHTLY_TAG}" \ |
| 242 | + "${{ steps.sign.outputs.signed_apk }}" \ |
| 243 | + "${{ steps.sign.outputs.sha256_file }}" \ |
| 244 | + --clobber |
| 245 | + gh release edit "${NIGHTLY_TAG}" \ |
| 246 | + --title "${NIGHTLY_RELEASE_NAME}" \ |
| 247 | + --notes-file "${{ steps.metadata.outputs.release_notes }}" \ |
| 248 | + --prerelease |
| 249 | + else |
| 250 | + gh release create "${NIGHTLY_TAG}" \ |
| 251 | + "${{ steps.sign.outputs.signed_apk }}" \ |
| 252 | + "${{ steps.sign.outputs.sha256_file }}" \ |
| 253 | + --title "${NIGHTLY_RELEASE_NAME}" \ |
| 254 | + --notes-file "${{ steps.metadata.outputs.release_notes }}" \ |
| 255 | + --prerelease \ |
| 256 | + --verify-tag |
| 257 | + fi |
0 commit comments