diff --git a/.gitea/workflows/dev.yml b/.gitea/workflows/dev.yml new file mode 100644 index 0000000..33c8f72 --- /dev/null +++ b/.gitea/workflows/dev.yml @@ -0,0 +1,66 @@ +name: dev + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + build: + name: Build debug APK + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - name: Checkout + run: | + git clone https://git.trevena.info/nick/trafero.git repo + cd repo + + - name: Set up JDK 17 + run: | + WORKDIR=/var/lib/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=/var/lib/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 + fi + # Install per-package (idempotent): a runner that only cached + # android-34 from other projects must still gain android-35. + # sdkmanager no-ops packages that are already present. + yes | "$SDK/cmdline-tools/latest/bin/sdkmanager" --licenses > /dev/null 2>&1 || true + "$SDK/cmdline-tools/latest/bin/sdkmanager" --install \ + "platform-tools" \ + "platforms;android-35" \ + "build-tools;35.0.0" + [ -d "$SDK/platforms/android-35" ] || { echo "ERROR: platforms;android-35 did not install"; exit 1; } + [ -d "$SDK/build-tools/35.0.0" ] || { echo "ERROR: build-tools;35.0.0 did not install"; exit 1; } + 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 + timeout-minutes: 25 + run: | + cd repo + chmod +x gradlew + export JAVA_HOME=/var/lib/gitea-runner/work/jdk + export ANDROID_HOME=/var/lib/gitea-runner/work/android-sdk + export PATH=$JAVA_HOME/bin:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools:$PATH + ./gradlew assembleDebug --no-daemon diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml new file mode 100644 index 0000000..239207d --- /dev/null +++ b/.gitea/workflows/release.yml @@ -0,0 +1,243 @@ +name: release + +on: + push: + tags: + - 'v*.*.*' + # Retry path: a tag push pins the workflow file to that tag's commit, so a + # re-run of a red release run replays the OLD workflow even after main is + # fixed (that is exactly how run 78 kept failing post-fix). Dispatching + # against main runs main's current workflow against any already-published + # tag, so a release can be re-published without moving a tag. + workflow_dispatch: + inputs: + tag: + description: 'Existing tag to rebuild and re-publish, e.g. v0.3.8.1' + required: true + type: string + +jobs: + build-release: + name: Build & attach release APK + runs-on: ubuntu-latest + timeout-minutes: 30 + env: + # The tag this run publishes: the tag itself on a push, or the typed + # input on a manual retry (where github.ref_name is just the branch). + RELEASE_TAG: ${{ github.event.inputs.tag || github.ref_name }} + steps: + - name: Checkout + run: | + git clone --depth=1 https://git.trevena.info/nick/trafero.git repo + cd repo + # Targeted shallow fetch of the exact tag: the clone above is shallow + # off the default branch, so --tags alone may not carry the tag's + # commit when the run is dispatched from main instead of pushed. + git fetch --depth=1 origin tag "$RELEASE_TAG" || true + git fetch --tags origin + git checkout "$RELEASE_TAG" + + - name: Set up JDK 17 + run: | + WORKDIR=/var/lib/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=/var/lib/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 + fi + # Install per-package (idempotent): a runner that only cached + # android-34 from other projects must still gain android-35. + # sdkmanager no-ops packages that are already present. + yes | "$SDK/cmdline-tools/latest/bin/sdkmanager" --licenses > /dev/null 2>&1 || true + "$SDK/cmdline-tools/latest/bin/sdkmanager" --install \ + "platform-tools" \ + "platforms;android-35" \ + "build-tools;35.0.0" + [ -d "$SDK/platforms/android-35" ] || { echo "ERROR: platforms;android-35 did not install"; exit 1; } + [ -d "$SDK/build-tools/35.0.0" ] || { echo "ERROR: build-tools;35.0.0 did not install"; exit 1; } + 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 "$RELEASE_TAG" + # 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. + # A runner-host rebuild would mint a fresh key — the only + # scenario that breaks one upgrade. + mkdir -p keystore + if [ ! -f /var/lib/gitea-runner/work/release.jks ]; then + echo "Minting new persistent CI keystore" + keytool -genkey -noprompt -v \ + -keystore /var/lib/gitea-runner/work/release.jks \ + -keyalg RSA -keysize 2048 -validity 10000 \ + -alias trafero \ + -storepass trafero_dev_keystore \ + -keypass trafero_dev_keystore \ + -dname "CN=Trafero Dev, OU=Local, O=Trafero, L=Local, ST=Local, C=NZ" + else + echo "Reusing persistent CI keystore from runner cache" + fi + cp /var/lib/gitea-runner/work/release.jks keystore/release.jks + export JAVA_HOME=/var/lib/gitea-runner/work/jdk + export ANDROID_HOME=/var/lib/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="$RELEASE_TAG" + VERSION="${TAG#v}" + API="http://192.168.8.17:3000/api/v1/repos/nick/trafero" + APK_DEBUG="Trafero-$VERSION-debug.apk" + APK_RELEASE="Trafero-$VERSION-release.apk" + + echo "Tag: $TAG Version: $VERSION" + + # User-facing release notes: harvest this version's section from + # PREVIOUS_HISTORY.md (see the file header for the convention). + # Used as the release description and uploaded beside the APKs. + NOTES_FILE="/tmp/Trafero-$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="Trafero-$VERSION-changes.md" + echo "--- Deleting stale asset for $NOTES_ASSET ---" + python3 /tmp/delete_asset.py "$API" "$RELEASE_ID" "$NOTES_ASSET" "$GH_TOKEN" + # Upload $NOTES_FILE directly: it already IS /tmp/, so + # copying it onto itself made cp fail with exit 1 and killed the + # whole step (set -e) after the APKs had uploaded fine. + upload_asset "$NOTES_ASSET" "$NOTES_FILE" || 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')"