name: release on: push: tags: - 'v*.*.*' jobs: build-release: name: Build & attach release APK runs-on: ubuntu-latest timeout-minutes: 30 steps: - name: Checkout run: | git clone --depth=1 https://git.trevena.info/nick/incido.git repo cd repo git fetch --tags origin git checkout ${{ github.ref_name }} - name: Set up JDK 17 run: | WORKDIR=/tmp/gitea-runner-work if [ ! -d "$WORKDIR/jdk" ]; then wget -q 'https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.13%2B11/OpenJDK17U-jdk_x64_linux_hotspot_17.0.13_11.tar.gz' -O /tmp/jdk.tar.gz mkdir -p "$WORKDIR/jdk" tar -xzf /tmp/jdk.tar.gz -C "$WORKDIR/jdk" --strip-components=1 rm /tmp/jdk.tar.gz fi echo "JAVA_HOME=$WORKDIR/jdk" >> $GITHUB_ENV echo "PATH=$WORKDIR/jdk/bin:$PATH" >> $GITHUB_ENV - name: Set up Android SDK run: | WORKDIR=/tmp/gitea-runner-work SDK=$WORKDIR/android-sdk if [ ! -d "$SDK/cmdline-tools/latest" ]; then mkdir -p "$SDK" wget -q 'https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip' -O /tmp/cmdline-tools.zip unzip -q /tmp/cmdline-tools.zip -d /tmp/cmdline-tools-extract mkdir -p "$SDK/cmdline-tools" mv /tmp/cmdline-tools-extract/cmdline-tools "$SDK/cmdline-tools/latest" rm -rf /tmp/cmdline-tools.zip /tmp/cmdline-tools-extract export JAVA_HOME=$WORKDIR/jdk export PATH=$JAVA_HOME/bin:$PATH yes | "$SDK/cmdline-tools/latest/bin/sdkmanager" --licenses > /dev/null 2>&1 || true "$SDK/cmdline-tools/latest/bin/sdkmanager" --install \ "platform-tools" \ "platforms;android-34" \ "build-tools;34.0.0" > /dev/null 2>&1 fi echo "ANDROID_HOME=$SDK" >> $GITHUB_ENV echo "ANDROID_SDK_ROOT=$SDK" >> $GITHUB_ENV echo "PATH=$SDK/cmdline-tools/latest/bin:$SDK/platform-tools:$PATH" >> $GITHUB_ENV - name: Build debug + release APKs timeout-minutes: 25 run: | cd repo git checkout ${{ github.ref_name }} # Persistent signing keystore: minted ONCE in the runner's # cache dir (which survives across runs, same place the JDK # and SDK live) and copied into the checkout. Every release # is therefore signed with the SAME key, so users can # upgrade in place instead of hitting a signature conflict # (and, with allowBackup=false, losing their notes). # A runner-host rebuild would mint a fresh key โ€” the only # scenario that breaks one upgrade. mkdir -p keystore if [ ! -f /tmp/gitea-runner-work/release.jks ]; then echo "Minting new persistent CI keystore" keytool -genkey -noprompt -v \ -keystore /tmp/gitea-runner-work/release.jks \ -keyalg RSA -keysize 2048 -validity 10000 \ -alias incido \ -storepass incido_dev_keystore \ -keypass incido_dev_keystore \ -dname "CN=Incido Dev, OU=Local, O=Incido, L=Local, ST=Local, C=NZ" else echo "Reusing persistent CI keystore from runner cache" fi cp /tmp/gitea-runner-work/release.jks keystore/release.jks export JAVA_HOME=/tmp/gitea-runner-work/jdk export ANDROID_HOME=/tmp/gitea-runner-work/android-sdk export PATH=$JAVA_HOME/bin:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools:$PATH chmod +x ./gradlew ./gradlew --stop 2>/dev/null || true ./gradlew assembleDebug assembleRelease --no-daemon --stacktrace 2>&1 | tee /tmp/build.log BUILD_STATUS=${PIPESTATUS[0]} echo "Gradle exit status: $BUILD_STATUS" if [ $BUILD_STATUS -ne 0 ]; then echo "Build failed" tail -100 /tmp/build.log exit 1 fi ls -la app/build/outputs/apk/debug/ app/build/outputs/apk/release/ - name: Create release + attach both APKs env: GH_TOKEN: ${{ secrets.TOKEN }} run: | set -e cd repo TAG="${{ github.ref_name }}" VERSION="${TAG#v}" API="https://git.trevena.info/api/v1/repos/nick/incido" APK_DEBUG="Incido-$VERSION-debug.apk" APK_RELEASE="Incido-$VERSION-release.apk" echo "Tag: $TAG Version: $VERSION" # User-facing release notes: harvest this version's section from # PREVIOUS_HISTORY.md (maintained per Development Plan ยง11.1). # Used as the release description and uploaded beside the APKs. NOTES_FILE="/tmp/Incido-$VERSION-changes.md" if python3 scripts/ci/release-notes.py "$VERSION" --out "$NOTES_FILE"; then HAS_NOTES=1 echo "--- Release notes preview (v$VERSION) ---" head -12 "$NOTES_FILE" echo "---" else HAS_NOTES=0 echo "No PREVIOUS_HISTORY.md section for v$VERSION - falling back to the default description." fi echo "--- Check for existing release ---" RELEASE_ID=$(curl -s "$API/releases/tags/$TAG" \ -H "Authorization: token $GH_TOKEN" \ | python3 -c "import sys,json; print(json.load(sys.stdin).get('id','') or '')") if [ -z "$RELEASE_ID" ]; then echo "--- Create release ---" REL_TAG="$TAG" REL_NOTES_FILE="$NOTES_FILE" python3 -c "import json,os; nf=os.environ.get('REL_NOTES_FILE',''); body=(open(nf,encoding='utf-8').read() if nf and os.path.exists(nf) else 'Release '+os.environ['REL_TAG']); print(json.dumps({'tag_name':os.environ['REL_TAG'],'name':os.environ['REL_TAG'],'body':body,'draft':False,'prerelease':False}))" \ > /tmp/release_payload.json RELEASE_ID=$(curl -s -X POST "$API/releases" \ -H "Authorization: token $GH_TOKEN" \ -H "Content-Type: application/json" \ -d @/tmp/release_payload.json \ | python3 -c "import sys,json; print(json.load(sys.stdin).get('id','') or '')") else echo "--- Reusing existing release $RELEASE_ID ---" # Refresh the description even on a rerun, so backfills and # retried asset uploads always match PREVIOUS_HISTORY.md. if [ "$HAS_NOTES" = "1" ]; then echo "--- Patching release description from PREVIOUS_HISTORY.md ---" REL_NOTES_FILE="$NOTES_FILE" python3 -c "import json,os; print(json.dumps({'body': open(os.environ['REL_NOTES_FILE'],encoding='utf-8').read()}))" \ > /tmp/body_patch.json curl -s -X PATCH "$API/releases/$RELEASE_ID" \ -H "Authorization: token $GH_TOKEN" \ -H "Content-Type: application/json" \ -d @/tmp/body_patch.json > /dev/null fi fi echo "Release ID: $RELEASE_ID" if [ -z "$RELEASE_ID" ]; then echo "ERROR: no release ID"; exit 1; fi echo "aW1wb3J0IHN5cywganNvbiwgdXJsbGliLnJlcXVlc3QKYXBpID0gc3lzLmFyZ3ZbMV0KcmVsZWFzZV9pZCA9IHN5cy5hcmd2WzJdCmFwayA9IHN5cy5hcmd2WzNdCnRva2VuID0gc3lzLmFyZ3ZbNF0KcmVxID0gdXJsbGliLnJlcXVlc3QuUmVxdWVzdCgKICAgIGYie2FwaX0vcmVsZWFzZXMve3JlbGVhc2VfaWR9L2Fzc2V0cyIsCiAgICBoZWFkZXJzPXsiQXV0aG9yaXphdGlvbiI6IGYidG9rZW4ge3Rva2VufSJ9CikKcmVzcCA9IHVybGxpYi5yZXF1ZXN0LnVybG9wZW4ocmVxKQpmb3IgYSBpbiBqc29uLmxvYWRzKHJlc3AucmVhZCgpKToKICAgIGlmIGFbIm5hbWUiXSA9PSBhcGs6CiAgICAgICAgZGVsZXRlX3JlcSA9IHVybGxpYi5yZXF1ZXN0LlJlcXVlc3QoCiAgICAgICAgICAgIGYie2FwaX0vcmVsZWFzZXMve3JlbGVhc2VfaWR9L2Fzc2V0cy97YVsnaWQnXX0iLAogICAgICAgICAgICBtZXRob2Q9IkRFTEVURSIsCiAgICAgICAgICAgIGhlYWRlcnM9eyJBdXRob3JpemF0aW9uIjogZiJ0b2tlbiB7dG9rZW59In0KICAgICAgICApCiAgICAgICAgdXJsbGliLnJlcXVlc3QudXJsb3BlbihkZWxldGVfcmVxKQogICAgICAgIHByaW50KGYiRGVsZXRlZCB7YVsnaWQnXX0iLCBmbHVzaD1UcnVlKQo=" | base64 -d > /tmp/delete_asset.py # Upload one asset with retries, then verify server-side. # Cloudflare sits in front of the origin and returns # "error code: 524" (origin timeout, ~100s) on large/slow # uploads โ€” the file frequently lands on the origin anyway, # so success is decided by listing the release's assets # rather than trusting the upload response body (which may # be a non-JSON Cloudflare error page). upload_asset() { local apk_name="$1" apk_path="$2" local attempt for attempt in 1 2 3 4 5; do echo "--- Uploading $apk_name (attempt $attempt/5) ---" ls -la "$apk_path" UPLOAD=$(curl -s -f -X POST "$API/releases/$RELEASE_ID/assets?name=$apk_name" \ -H "Authorization: token $GH_TOKEN" \ -F "attachment=@$apk_path" \ --max-time 600 --retry 2 --retry-all-errors --retry-delay 15 || true) echo "Upload response: ${UPLOAD:0:200}" if curl -s "$API/releases/$RELEASE_ID" \ -H "Authorization: token $GH_TOKEN" \ | python3 -c "import sys,json; d=json.load(sys.stdin); sys.exit(0 if any(a['name']=='$apk_name' for a in d['assets']) else 1)"; then echo "Verified: $apk_name is attached to the release." return 0 fi echo "Asset not on the server yet โ€” retrying in 15s..." sleep 15 done echo "ERROR: $apk_name failed to attach after 5 attempts." return 1 } for APK in "$APK_DEBUG" "$APK_RELEASE"; do if echo "$APK" | grep -q "debug"; then APK_PATH="app/build/outputs/apk/debug/app-debug.apk" else APK_PATH="app/build/outputs/apk/release/app-release.apk" fi if [ ! -f "$APK_PATH" ]; then echo "ERROR: APK not found: $APK_PATH" exit 1 fi echo "--- Deleting stale assets for $APK ---" python3 /tmp/delete_asset.py "$API" "$RELEASE_ID" "$APK" "$GH_TOKEN" upload_asset "$APK" "$APK_PATH" || exit 1 done # Ship the changelog beside the binaries: people skimming the # downloads get the notes for exactly this version as a file, # generated from PREVIOUS_HISTORY.md at the top of this run. if [ "$HAS_NOTES" = "1" ]; then NOTES_ASSET="Incido-$VERSION-changes.md" echo "--- Deleting stale asset for $NOTES_ASSET ---" python3 /tmp/delete_asset.py "$API" "$RELEASE_ID" "$NOTES_ASSET" "$GH_TOKEN" cp "$NOTES_FILE" "/tmp/$NOTES_ASSET" upload_asset "$NOTES_ASSET" "/tmp/$NOTES_ASSET" || exit 1 fi echo "--- Final release state ---" curl -s "$API/releases/$RELEASE_ID" -H "Authorization: token $GH_TOKEN" | python3 -c "import sys,json; d=json.load(sys.stdin); print('Release ' + d['tag_name'] + ' has ' + str(len(d['assets'])) + ' assets')"