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
+66
-30
@@ -49,12 +49,6 @@ jobs:
|
|||||||
echo "ANDROID_SDK_ROOT=$SDK" >> $GITHUB_ENV
|
echo "ANDROID_SDK_ROOT=$SDK" >> $GITHUB_ENV
|
||||||
echo "PATH=$SDK/cmdline-tools/latest/bin:$SDK/platform-tools:$PATH" >> $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
|
- name: Build debug + release APKs
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.TOKEN }}
|
||||||
@@ -63,8 +57,6 @@ jobs:
|
|||||||
cd repo
|
cd repo
|
||||||
chmod +x gradlew
|
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
|
mkdir -p keystore
|
||||||
keytool -genkey -noprompt -v \
|
keytool -genkey -noprompt -v \
|
||||||
-keystore keystore/release.jks \
|
-keystore keystore/release.jks \
|
||||||
@@ -78,41 +70,85 @@ jobs:
|
|||||||
export ANDROID_HOME=/tmp/gitea-runner-work/android-sdk
|
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
|
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
|
./gradlew assembleDebug assembleRelease --no-daemon
|
||||||
|
|
||||||
# Verify both files exist before proceeding
|
|
||||||
TAG="${GITHUB_REF##*/}"
|
|
||||||
|
|
||||||
- name: Create release + attach both APKs
|
- name: Create release + attach both APKs
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.TOKEN }}
|
GH_TOKEN: ${{ secrets.TOKEN }}
|
||||||
run: |
|
run: |
|
||||||
|
set -e
|
||||||
cd repo
|
cd repo
|
||||||
TAG="${GITHUB_REF##*/}"
|
TAG="${GITHUB_REF##*/}"
|
||||||
VERSION="${TAG#v}"
|
VERSION="${TAG#v}"
|
||||||
|
API="https://git.trevena.info/api/v1/repos/nick/incido"
|
||||||
|
|
||||||
echo "Creating release for tag: $TAG"
|
# -- 1. Get-or-create release -----------------------------------------
|
||||||
RELEASE_ID=$(curl -s -X POST "https://git.trevena.info/api/v1/repos/nick/incido/releases" \
|
echo "Checking for existing release for tag: $TAG"
|
||||||
-H "Authorization: token $GITHUB_TOKEN" \
|
RELEASE_ID=$(curl -s "$API/releases/tags/$TAG" \
|
||||||
-H "Content-Type: application/json" \
|
-H "Authorization: token $GH_TOKEN" \
|
||||||
-d "{
|
| python3 -c "import sys,json; print(json.load(sys.stdin).get('id','') or '')")
|
||||||
\"tag_name\": \"$TAG\",
|
|
||||||
\"name\": \"$TAG\",
|
if [ -n "$RELEASE_ID" ]; then
|
||||||
\"draft\": false,
|
echo "Found existing release ID: $RELEASE_ID (reusing it)"
|
||||||
\"prerelease\": false
|
else
|
||||||
}" | python3 -c "import sys,json; print(json.load(sys.stdin).get('id','') or '')")
|
echo "No existing release -- creating one for tag: $TAG"
|
||||||
echo "Release ID: $RELEASE_ID"
|
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 "Created release ID: $RELEASE_ID"
|
||||||
|
fi
|
||||||
|
|
||||||
if [ -z "$RELEASE_ID" ]; then
|
if [ -z "$RELEASE_ID" ]; then
|
||||||
echo "ERROR: Failed to create release"
|
echo "ERROR: Could not get or create release for tag $TAG"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# -- 2. Attach APKs ---------------------------------------------------
|
||||||
for APK in "Incido-${VERSION}-debug.apk" "Incido-${VERSION}-release.apk"; do
|
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"
|
echo "Attaching $APK to release $RELEASE_ID"
|
||||||
RESPONSE=$(curl -s \
|
RESPONSE=$(curl -s \
|
||||||
--max-time 300 \
|
--max-time 300 \
|
||||||
@@ -120,8 +156,8 @@ jobs:
|
|||||||
--retry-delay 10 \
|
--retry-delay 10 \
|
||||||
--retry-connrefuse \
|
--retry-connrefuse \
|
||||||
-X POST \
|
-X POST \
|
||||||
"https://git.trevena.info/api/v1/repos/nick/incido/releases/$RELEASE_ID/assets?name=$APK" \
|
"$API/releases/$RELEASE_ID/assets?name=$APK" \
|
||||||
-H "Authorization: token $GITHUB_TOKEN" \
|
-H "Authorization: token $GH_TOKEN" \
|
||||||
-H "Content-Type: application/octet-stream" \
|
-H "Content-Type: application/octet-stream" \
|
||||||
--data-binary "@$APK")
|
--data-binary "@$APK")
|
||||||
echo "Attach response: $RESPONSE"
|
echo "Attach response: $RESPONSE"
|
||||||
|
|||||||
Reference in new issue
Block a user