ci(release): robust release creation — get-or-create + delete stale assets before re-upload
This commit is contained in:
1 parent
734f9aecca
commit
06b4c208a6
1 file changed
+65
-29
@@ -49,12 +49,6 @@ jobs:
|
||||
echo "ANDROID_SDK_ROOT=$SDK" >> $GITHUB_ENV
|
||||
echo "PATH=$SDK/cmdline-tools/latest/bin:$SDK/platform-tools:$PATH" >> $GITHUB_ENV
|
||||
|
||||
# The release signing config in app/build.gradle.kts only attaches a
|
||||
# storeFile when keystore/release.jks exists, and that .jks is
|
||||
# gitignored -- so the CI runner must mint its own. We use the exact
|
||||
# dev keystore documented in keystore/README.md (alias "incido",
|
||||
# both passwords "incido_dev_keystore"). Move to a Gitea Actions
|
||||
# secret + production keystore before publishing to Play / F-Droid.
|
||||
- name: Build debug + release APKs
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.TOKEN }}
|
||||
@@ -63,8 +57,6 @@ jobs:
|
||||
cd repo
|
||||
chmod +x gradlew
|
||||
|
||||
# Mint the dev keystore in the same shell session as the build
|
||||
# so gradle can find it at keystore/release.jks (rootProject.file).
|
||||
mkdir -p keystore
|
||||
keytool -genkey -noprompt -v \
|
||||
-keystore keystore/release.jks \
|
||||
@@ -78,41 +70,85 @@ jobs:
|
||||
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
|
||||
|
||||
# Build both variants; copyReleaseApks finalizes assembleRelease and
|
||||
# drops version-stamped APKs at the repo root:
|
||||
# Incido-<version>-debug.apk
|
||||
# Incido-<version>-release.apk
|
||||
./gradlew assembleDebug assembleRelease --no-daemon
|
||||
|
||||
# Verify both files exist before proceeding
|
||||
TAG="${GITHUB_REF##*/}"
|
||||
|
||||
- name: Create release + attach both APKs
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.TOKEN }}
|
||||
GH_TOKEN: ${{ secrets.TOKEN }}
|
||||
run: |
|
||||
set -e
|
||||
cd repo
|
||||
TAG="${GITHUB_REF##*/}"
|
||||
VERSION="${TAG#v}"
|
||||
API="https://git.trevena.info/api/v1/repos/nick/incido"
|
||||
|
||||
echo "Creating release for tag: $TAG"
|
||||
RELEASE_ID=$(curl -s -X POST "https://git.trevena.info/api/v1/repos/nick/incido/releases" \
|
||||
-H "Authorization: token $GITHUB_TOKEN" \
|
||||
# -- 1. Get-or-create release -----------------------------------------
|
||||
echo "Checking for existing release for tag: $TAG"
|
||||
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 [ -n "$RELEASE_ID" ]; then
|
||||
echo "Found existing release ID: $RELEASE_ID (reusing it)"
|
||||
else
|
||||
echo "No existing release -- creating one for tag: $TAG"
|
||||
RELEASE_ID=$(curl -s -X POST "$API/releases" \
|
||||
-H "Authorization: token $GH_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{
|
||||
\"tag_name\": \"$TAG\",
|
||||
\"name\": \"$TAG\",
|
||||
\"draft\": false,
|
||||
\"prerelease\": false
|
||||
}" | python3 -c "import sys,json; print(json.load(sys.stdin).get('id','') or '')")
|
||||
echo "Release ID: $RELEASE_ID"
|
||||
-d {
|
||||
"\"tag_name\": \"$TAG\",
|
||||
"\"name\": \"$TAG\",
|
||||
"\"draft\": false,
|
||||
"\"prerelease\": false
|
||||
} | python3 -c "import sys,json; print(json.load(sys.stdin).get('id','') or '')")
|
||||
echo "Created release ID: $RELEASE_ID"
|
||||
fi
|
||||
|
||||
if [ -z "$RELEASE_ID" ]; then
|
||||
echo "ERROR: Failed to create release"
|
||||
echo "ERROR: Could not get or create release for tag $TAG"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# -- 2. Attach APKs ---------------------------------------------------
|
||||
for APK in "Incido-${VERSION}-debug.apk" "Incido-${VERSION}-release.apk"; do
|
||||
if [ ! -f "$APK" ]; then
|
||||
echo "SKIP: $APK not found"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Write a small python helper that fetches existing asset IDs
|
||||
# for this release, so we can delete stale ones before re-uploading.
|
||||
python3 - << 'PYEOF'
|
||||
import sys, json, urllib.request
|
||||
|
||||
api = sys.argv[1]
|
||||
release_id = sys.argv[2]
|
||||
apk_name = sys.argv[3]
|
||||
token = sys.argv[4]
|
||||
|
||||
req = urllib.request.Request(
|
||||
f"{api}/releases/{release_id}/assets",
|
||||
headers={"Authorization": f"token {token}"}
|
||||
)
|
||||
with urllib.request.urlopen(req) as resp:
|
||||
assets = json.load(resp)
|
||||
|
||||
for a in assets:
|
||||
if a.get("name") == apk_name:
|
||||
print(a["id"], flush=True)
|
||||
break
|
||||
else:
|
||||
print("", flush=True)
|
||||
PYEOF
|
||||
|
||||
ASSET_ID=$(python3 - "$API" "$RELEASE_ID" "$APK" "$GH_TOKEN")
|
||||
|
||||
if [ -n "$ASSET_ID" ]; then
|
||||
echo "Deleting stale asset $ASSET_ID ($APK)"
|
||||
curl -s -X DELETE "$API/releases/$RELEASE_ID/assets/$ASSET_ID" \
|
||||
-H "Authorization: token $GH_TOKEN" > /dev/null
|
||||
fi
|
||||
|
||||
echo "Attaching $APK to release $RELEASE_ID"
|
||||
RESPONSE=$(curl -s \
|
||||
--max-time 300 \
|
||||
@@ -120,8 +156,8 @@ jobs:
|
||||
--retry-delay 10 \
|
||||
--retry-connrefuse \
|
||||
-X POST \
|
||||
"https://git.trevena.info/api/v1/repos/nick/incido/releases/$RELEASE_ID/assets?name=$APK" \
|
||||
-H "Authorization: token $GITHUB_TOKEN" \
|
||||
"$API/releases/$RELEASE_ID/assets?name=$APK" \
|
||||
-H "Authorization: token $GH_TOKEN" \
|
||||
-H "Content-Type: application/octet-stream" \
|
||||
--data-binary "@$APK")
|
||||
echo "Attach response: $RESPONSE"
|
||||
|
||||
Reference in new issue
Block a user