|
| 1 | +# One-click release of the ShimmerCapture app (the shimmerServiceExample module): a signed APK on a |
| 2 | +# GitHub pre-release, and optionally a draft release in Google Play. It automates the Confluence page |
| 3 | +# "ShimmerCapture Android Release (shimmerServiceExample)", which did the same by hand in Android |
| 4 | +# Studio. |
| 5 | +# |
| 6 | +# How to release (the full guide is ShimmerAndroidInstrumentDriver/shimmerServiceExample/RELEASING.md) |
| 7 | +# 1. In a PR, as before, raise versionCode and versionName in shimmerServiceExample/build.gradle, |
| 8 | +# and leave MainActivity.appReleaseType at PUBLIC. Merge it. |
| 9 | +# 2. Actions -> "Release ShimmerCapture" -> Run workflow (on master). The version is the |
| 10 | +# versionName in build.gradle, tagged ShimmerCapture_v<versionName>. |
| 11 | +# 3. With google_play ticked, the APK goes to Play Console's Production track as a draft. In Play |
| 12 | +# Console, open that release, check it, and "Send changes for review", as on the Confluence page. |
| 13 | +# |
| 14 | +# Unlike the Android API's release.yml, the version is committed rather than typed: Play needs a |
| 15 | +# versionCode higher than every one before it, and build.gradle is the only record of the last one. |
| 16 | +# |
| 17 | +# One run does, in order, and creates nothing public until the APK has built and is signed: |
| 18 | +# build reads the version; fails if it is already tagged, if versionCode did not go up since the |
| 19 | +# last ShimmerCapture release, or if the app is not set to PUBLIC; builds the signed |
| 20 | +# release APK. Read-only (no keystore without it): it also runs, unsigned, on PRs. |
| 21 | +# release tags ShimmerCapture_v<version> on the commit it built and creates the GitHub |
| 22 | +# pre-release with the APK and notes since the previous ShimmerCapture release. |
| 23 | +# Pre-release, so the Android API release stays "Latest", as before. |
| 24 | +# play google_play only: uploads the same APK to Play's Production track as a draft. |
| 25 | +# |
| 26 | +# If a later job fails, "Re-run failed jobs" re-runs just that job. |
| 27 | +# |
| 28 | +# Secrets (repository Settings -> Secrets and variables -> Actions) |
| 29 | +# SHIMMERCAPTURE_KEYSTORE_BASE64 the upload keystore (.jks) used in Android Studio, base64: |
| 30 | +# [Convert]::ToBase64String([IO.File]::ReadAllBytes("x.jks")) |
| 31 | +# SHIMMERCAPTURE_KEYSTORE_PASSWORD its store password |
| 32 | +# SHIMMERCAPTURE_KEY_ALIAS the key alias |
| 33 | +# SHIMMERCAPTURE_KEY_PASSWORD the key password |
| 34 | +# GOOGLE_PLAY_SERVICE_ACCOUNT_JSON google_play only: JSON key of a Google Cloud service account |
| 35 | +# invited in Play Console (Users and permissions) with "Release |
| 36 | +# to production" on ShimmerCapture |
| 37 | +# A real release refuses to run without the keystore. A dry run builds unsigned if it is missing, |
| 38 | +# and a PR run always builds unsigned. |
| 39 | +# |
| 40 | +# The driver is read from Shimmer-Java-Android-API's GitHub Packages, as in release.yml: this repo's |
| 41 | +# GITHUB_TOKEN, or PACKAGES_READ_TOKEN if that is ever refused. |
| 42 | +# |
| 43 | +# dry_run builds the signed APK (unsigned without the keystore) and keeps it as a workflow artifact, |
| 44 | +# from any branch, and publishes nothing. The workflow also dry-runs on any PR that changes this file. |
| 45 | + |
| 46 | +name: Release ShimmerCapture |
| 47 | + |
| 48 | +on: |
| 49 | + workflow_dispatch: |
| 50 | + inputs: |
| 51 | + google_play: |
| 52 | + description: 'Also upload to Google Play Production as a draft, to send for review in Play Console' |
| 53 | + type: boolean |
| 54 | + default: false |
| 55 | + whats_new: |
| 56 | + description: 'Google Play release notes (en-US, 500 characters max). Blank = "Bug fixes and improvements."' |
| 57 | + type: string |
| 58 | + required: false |
| 59 | + dry_run: |
| 60 | + description: 'Build only; publish nothing' |
| 61 | + type: boolean |
| 62 | + default: false |
| 63 | + pull_request: |
| 64 | + paths: |
| 65 | + - '.github/workflows/release-shimmercapture.yml' |
| 66 | + |
| 67 | +concurrency: |
| 68 | + group: release-shimmercapture |
| 69 | + cancel-in-progress: false |
| 70 | + |
| 71 | +defaults: |
| 72 | + run: |
| 73 | + working-directory: ShimmerAndroidInstrumentDriver |
| 74 | + |
| 75 | +jobs: |
| 76 | + build: |
| 77 | + runs-on: ubuntu-latest |
| 78 | + timeout-minutes: 30 # a hung Gradle daemon otherwise holds the runner for 6 hours |
| 79 | + permissions: |
| 80 | + contents: read |
| 81 | + packages: read |
| 82 | + env: |
| 83 | + DRY_RUN: ${{ github.event_name != 'workflow_dispatch' || inputs.dry_run }} |
| 84 | + # Read by the repositories in the root build.gradle to resolve the driver. |
| 85 | + USERNAME: ${{ github.actor }} |
| 86 | + TOKEN: ${{ secrets.PACKAGES_READ_TOKEN || github.token }} |
| 87 | + outputs: |
| 88 | + version: ${{ steps.version.outputs.version }} |
| 89 | + version_code: ${{ steps.version.outputs.version_code }} |
| 90 | + tag: ${{ steps.version.outputs.tag }} |
| 91 | + previous_tag: ${{ steps.version.outputs.previous_tag }} |
| 92 | + driver: ${{ steps.version.outputs.driver }} |
| 93 | + apk: ${{ steps.collect.outputs.apk }} |
| 94 | + steps: |
| 95 | + - name: Release from master only |
| 96 | + if: env.DRY_RUN != 'true' && github.ref != 'refs/heads/master' |
| 97 | + working-directory: . |
| 98 | + run: | |
| 99 | + echo "::error::A release must run on master, not ${GITHUB_REF_NAME}. Tick dry_run to try another branch." |
| 100 | + exit 1 |
| 101 | +
|
| 102 | + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 |
| 103 | + with: |
| 104 | + fetch-depth: 0 # tags, to refuse a version that is already released |
| 105 | + # Keep the token out of .git/config, where the Gradle build could read it. |
| 106 | + persist-credentials: false |
| 107 | + |
| 108 | + - name: Check the version and the release type |
| 109 | + id: version |
| 110 | + env: |
| 111 | + GH_TOKEN: ${{ github.token }} |
| 112 | + run: | |
| 113 | + gradle_file=shimmerServiceExample/build.gradle |
| 114 | + VERSION=$(sed -nE 's/^\s*versionName\s+"([^"]+)".*/\1/p' "$gradle_file") |
| 115 | + VERSION_CODE=$(sed -nE 's/^\s*versionCode\s+([0-9]+).*/\1/p' "$gradle_file") |
| 116 | + if [ -z "$VERSION" ] || [ -z "$VERSION_CODE" ]; then |
| 117 | + echo "::error::Cannot read versionName and versionCode from $gradle_file." |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | + TAG="ShimmerCapture_v$VERSION" |
| 121 | +
|
| 122 | + PREVIOUS_TAG=$(gh release list --repo "$GITHUB_REPOSITORY" --limit 200 --json tagName,createdAt \ |
| 123 | + --jq '[.[] | select(.tagName | startswith("ShimmerCapture_v"))] | sort_by(.createdAt) | last | .tagName // ""') |
| 124 | +
|
| 125 | + fail() { |
| 126 | + if [ "$DRY_RUN" = true ]; then |
| 127 | + echo "::warning::$1 A real release would stop here. Dry run continues." |
| 128 | + else |
| 129 | + echo "::error::$1" |
| 130 | + exit 1 |
| 131 | + fi |
| 132 | + } |
| 133 | +
|
| 134 | + if git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then |
| 135 | + fail "$VERSION is already released as $TAG. Raise versionName and versionCode in $gradle_file first." |
| 136 | + fi |
| 137 | + # Play refuses a versionCode it has seen, so check it went up since the last release. |
| 138 | + if [ -n "$PREVIOUS_TAG" ]; then |
| 139 | + previous_code=$(git show "refs/tags/$PREVIOUS_TAG:ShimmerAndroidInstrumentDriver/$gradle_file" 2>/dev/null \ |
| 140 | + | sed -nE 's/^\s*versionCode\s+([0-9]+).*/\1/p') |
| 141 | + if [ -n "$previous_code" ] && [ "$VERSION_CODE" -le "$previous_code" ]; then |
| 142 | + fail "versionCode $VERSION_CODE is not higher than $previous_code in $PREVIOUS_TAG. Raise it in $gradle_file." |
| 143 | + fi |
| 144 | + fi |
| 145 | + # The Confluence procedure's first step: a Play release must be the PUBLIC build. |
| 146 | + release_type=$(sed -nE 's/.*appReleaseType\s*=\s*APP_RELEASE_TYPE\.([A-Z_]+).*/\1/p' \ |
| 147 | + shimmerServiceExample/src/main/java/com/shimmerresearch/shimmercapture/MainActivity.java) |
| 148 | + if [ "$release_type" != PUBLIC ]; then |
| 149 | + fail "MainActivity.appReleaseType is ${release_type:-not found}, not PUBLIC." |
| 150 | + fi |
| 151 | +
|
| 152 | + DRIVER=$(sed -nE "s/.*findProperty\('driverVersion'\) \?: '([^']+)'.*/\1/p" build.gradle) |
| 153 | + { |
| 154 | + echo "version=$VERSION" |
| 155 | + echo "version_code=$VERSION_CODE" |
| 156 | + echo "tag=$TAG" |
| 157 | + echo "previous_tag=$PREVIOUS_TAG" |
| 158 | + echo "driver=$DRIVER" |
| 159 | + } >> "$GITHUB_OUTPUT" |
| 160 | + echo "Releasing ShimmerCapture $VERSION (versionCode $VERSION_CODE) as $TAG against driver ${DRIVER:-unknown}; notes since ${PREVIOUS_TAG:-the first commit}" |
| 161 | +
|
| 162 | + # Decoded before building so a missing keystore fails in seconds. The keystore is written |
| 163 | + # outside the checkout, and the step that uses it deletes it. Never on a PR: the build runs the |
| 164 | + # PR's own Gradle scripts, which could read it. |
| 165 | + - name: Set up the signing keystore |
| 166 | + id: keystore |
| 167 | + env: |
| 168 | + KEYSTORE_BASE64: ${{ github.event_name == 'workflow_dispatch' && secrets.SHIMMERCAPTURE_KEYSTORE_BASE64 || '' }} |
| 169 | + run: | |
| 170 | + if [ -z "$KEYSTORE_BASE64" ]; then |
| 171 | + if [ "$DRY_RUN" = true ]; then |
| 172 | + echo "::warning::No SHIMMERCAPTURE_KEYSTORE_BASE64 secret, so this dry run builds an unsigned APK." |
| 173 | + echo "signed=false" >> "$GITHUB_OUTPUT" |
| 174 | + exit 0 |
| 175 | + fi |
| 176 | + echo "::error::No SHIMMERCAPTURE_KEYSTORE_BASE64 secret. See the secrets list at the top of this workflow." |
| 177 | + exit 1 |
| 178 | + fi |
| 179 | + keystore="$RUNNER_TEMP/shimmercapture-upload.jks" |
| 180 | + printf '%s' "$KEYSTORE_BASE64" | base64 -d > "$keystore" |
| 181 | + echo "signed=true" >> "$GITHUB_OUTPUT" |
| 182 | + echo "path=$keystore" >> "$GITHUB_OUTPUT" |
| 183 | +
|
| 184 | + # AGP 8 needs JDK 17. |
| 185 | + - name: Set up JDK 17 |
| 186 | + uses: actions/setup-java@cf277c60eb25467037889841efdb72551f06f6c3 # v4.9.1 |
| 187 | + with: |
| 188 | + java-version: '17' |
| 189 | + distribution: 'temurin' |
| 190 | + |
| 191 | + - name: Setup Gradle |
| 192 | + uses: gradle/actions/setup-gradle@ec92e829475ac0c2315ea8f9eced72db85bb337a # v3.0.0 |
| 193 | + |
| 194 | + # The android.injected.signing.* properties are what Android Studio's "Generate Signed App |
| 195 | + # Bundle / APK" passes, so this signs exactly as the manual procedure did, with no signing |
| 196 | + # config in build.gradle. Actions masks the secrets in the log. gradle.properties is gitignored |
| 197 | + # here, so android.useAndroidX and the daemon heap are passed on the command line, as in ci.yml. |
| 198 | + - name: Build the release APK |
| 199 | + env: |
| 200 | + SIGNED: ${{ steps.keystore.outputs.signed }} |
| 201 | + KEYSTORE: ${{ steps.keystore.outputs.path }} |
| 202 | + KEYSTORE_PASSWORD: ${{ secrets.SHIMMERCAPTURE_KEYSTORE_PASSWORD }} |
| 203 | + KEY_ALIAS: ${{ secrets.SHIMMERCAPTURE_KEY_ALIAS }} |
| 204 | + KEY_PASSWORD: ${{ secrets.SHIMMERCAPTURE_KEY_PASSWORD }} |
| 205 | + run: | |
| 206 | + signing=() |
| 207 | + if [ "$SIGNED" = true ]; then |
| 208 | + trap 'rm -f "$KEYSTORE"' EXIT |
| 209 | + signing=( |
| 210 | + -Pandroid.injected.signing.store.file="$KEYSTORE" |
| 211 | + -Pandroid.injected.signing.store.password="$KEYSTORE_PASSWORD" |
| 212 | + -Pandroid.injected.signing.key.alias="$KEY_ALIAS" |
| 213 | + -Pandroid.injected.signing.key.password="$KEY_PASSWORD" |
| 214 | + ) |
| 215 | + fi |
| 216 | + ./gradlew :shimmerServiceExample:assembleRelease -Pandroid.useAndroidX=true "${signing[@]}" \ |
| 217 | + -Dorg.gradle.jvmargs="-Xmx4g -XX:MaxMetaspaceSize=1g" |
| 218 | +
|
| 219 | + - name: Collect and check the APK |
| 220 | + id: collect |
| 221 | + env: |
| 222 | + VERSION: ${{ steps.version.outputs.version }} |
| 223 | + VERSION_CODE: ${{ steps.version.outputs.version_code }} |
| 224 | + SIGNED: ${{ steps.keystore.outputs.signed }} |
| 225 | + run: | |
| 226 | + out=shimmerServiceExample/build/outputs/apk/release |
| 227 | + ls -l "$out" |
| 228 | + if [ "$SIGNED" = true ]; then |
| 229 | + built="$out/shimmerServiceExample-release.apk" |
| 230 | + name="ShimmerCapture_v$VERSION.apk" |
| 231 | + else |
| 232 | + built="$out/shimmerServiceExample-release-unsigned.apk" |
| 233 | + name="ShimmerCapture_v$VERSION-unsigned.apk" |
| 234 | + fi |
| 235 | + mkdir -p ../release-assets |
| 236 | + cp "$built" "../release-assets/$name" |
| 237 | +
|
| 238 | + build_tools=$(ls -d "$ANDROID_HOME"/build-tools/* | sort -V | tail -1) |
| 239 | + badging=$("$build_tools/aapt2" dump badging "../release-assets/$name" | head -1) |
| 240 | + echo "$badging" |
| 241 | + if ! grep -q "versionCode='$VERSION_CODE'" <<<"$badging" || ! grep -q "versionName='$VERSION'" <<<"$badging"; then |
| 242 | + echo "::error::The APK does not carry versionName $VERSION and versionCode $VERSION_CODE." |
| 243 | + exit 1 |
| 244 | + fi |
| 245 | + if [ "$SIGNED" = true ]; then |
| 246 | + "$build_tools/apksigner" verify --print-certs "../release-assets/$name" |
| 247 | + fi |
| 248 | + echo "apk=$name" >> "$GITHUB_OUTPUT" |
| 249 | +
|
| 250 | + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2 |
| 251 | + with: |
| 252 | + name: shimmercapture-apk |
| 253 | + path: release-assets/ |
| 254 | + if-no-files-found: error |
| 255 | + |
| 256 | + release: |
| 257 | + needs: build |
| 258 | + if: github.event_name == 'workflow_dispatch' && !inputs.dry_run |
| 259 | + runs-on: ubuntu-latest |
| 260 | + timeout-minutes: 10 |
| 261 | + permissions: |
| 262 | + contents: write # create the tag and the release |
| 263 | + env: |
| 264 | + GH_TOKEN: ${{ github.token }} |
| 265 | + VERSION: ${{ needs.build.outputs.version }} |
| 266 | + VERSION_CODE: ${{ needs.build.outputs.version_code }} |
| 267 | + TAG: ${{ needs.build.outputs.tag }} |
| 268 | + PREVIOUS_TAG: ${{ needs.build.outputs.previous_tag }} |
| 269 | + DRIVER: ${{ needs.build.outputs.driver }} |
| 270 | + steps: |
| 271 | + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 |
| 272 | + with: |
| 273 | + name: shimmercapture-apk |
| 274 | + path: release-assets |
| 275 | + |
| 276 | + # Generated notes cover every PR since the last ShimmerCapture tag, not only the app's: the app |
| 277 | + # ships the library and the driver too. |
| 278 | + - name: Create the tag and GitHub pre-release |
| 279 | + working-directory: . |
| 280 | + run: | |
| 281 | + args=(-f tag_name="$TAG" -f target_commitish="$GITHUB_SHA") |
| 282 | + if [ -n "$PREVIOUS_TAG" ]; then args+=(-f previous_tag_name="$PREVIOUS_TAG"); fi |
| 283 | + { |
| 284 | + echo "ShimmerCapture $VERSION (versionCode $VERSION_CODE), built from \`shimmerServiceExample\` against Shimmer Java API driver ${DRIVER:-(unknown)}." |
| 285 | + echo |
| 286 | + gh api "repos/$GITHUB_REPOSITORY/releases/generate-notes" "${args[@]}" --jq .body |
| 287 | + } > notes.md |
| 288 | + gh release create "$TAG" release-assets/* \ |
| 289 | + --repo "$GITHUB_REPOSITORY" \ |
| 290 | + --target "$GITHUB_SHA" \ |
| 291 | + --title "$TAG" \ |
| 292 | + --notes-file notes.md \ |
| 293 | + --prerelease |
| 294 | + { |
| 295 | + echo "### Released ShimmerCapture $VERSION" |
| 296 | + echo "- Release: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/tag/$TAG" |
| 297 | + echo "- versionCode $VERSION_CODE, driver ${DRIVER:-unknown}" |
| 298 | + } >> "$GITHUB_STEP_SUMMARY" |
| 299 | +
|
| 300 | + play: |
| 301 | + needs: [build, release] |
| 302 | + if: github.event_name == 'workflow_dispatch' && !inputs.dry_run && inputs.google_play |
| 303 | + runs-on: ubuntu-latest |
| 304 | + timeout-minutes: 15 |
| 305 | + permissions: {} |
| 306 | + steps: |
| 307 | + - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0 |
| 308 | + with: |
| 309 | + name: shimmercapture-apk |
| 310 | + path: release-assets |
| 311 | + |
| 312 | + - name: Write the release notes |
| 313 | + working-directory: . |
| 314 | + env: |
| 315 | + WHATS_NEW: ${{ inputs.whats_new }} |
| 316 | + HAS_ACCOUNT: ${{ secrets.GOOGLE_PLAY_SERVICE_ACCOUNT_JSON != '' }} |
| 317 | + run: | |
| 318 | + if [ "$HAS_ACCOUNT" != true ]; then |
| 319 | + echo "::error::No GOOGLE_PLAY_SERVICE_ACCOUNT_JSON secret. The GitHub release is done; upload $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases to Play Console by hand, or add the secret and re-run this job." |
| 320 | + exit 1 |
| 321 | + fi |
| 322 | + mkdir -p whatsnew |
| 323 | + printf '%s\n' "${WHATS_NEW:-Bug fixes and improvements.}" > whatsnew/whatsnew-en-US |
| 324 | + if [ "$(wc -m < whatsnew/whatsnew-en-US)" -gt 500 ]; then |
| 325 | + echo "::error::Google Play allows 500 characters of release notes." |
| 326 | + exit 1 |
| 327 | + fi |
| 328 | +
|
| 329 | + # A draft, so a person still checks the release in Play Console and sends it for review, as in |
| 330 | + # the manual procedure. |
| 331 | + - name: Upload to Google Play (Production, draft) |
| 332 | + uses: r0adkll/upload-google-play@e738b9dd8f2476ea806d921b64aacd24f34515a5 # v1.1.5 |
| 333 | + with: |
| 334 | + serviceAccountJsonPlainText: ${{ secrets.GOOGLE_PLAY_SERVICE_ACCOUNT_JSON }} |
| 335 | + packageName: com.shimmersensing.shimmerconnect |
| 336 | + releaseFiles: release-assets/${{ needs.build.outputs.apk }} |
| 337 | + releaseName: ${{ needs.build.outputs.version }} |
| 338 | + track: production |
| 339 | + status: draft |
| 340 | + whatsNewDirectory: whatsnew |
| 341 | + |
| 342 | + - name: Summary |
| 343 | + working-directory: . |
| 344 | + run: | |
| 345 | + echo "### Uploaded to Google Play as a draft on Production. In Play Console: Test and release -> Production -> review the release -> Send changes for review." >> "$GITHUB_STEP_SUMMARY" |
0 commit comments