123 lines
5.3 KiB
Bash
Executable File
123 lines
5.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# One-command, Vault-backed Dev build/release with remote artifact verification.
|
|
set -euo pipefail
|
|
ROOT="$(cd -- "$(dirname -- "$0")/.." && pwd)"
|
|
DRY_RUN=0
|
|
for arg in "$@"; do
|
|
case "$arg" in
|
|
--dry-run) DRY_RUN=1 ;;
|
|
--help|-h) printf '%s\n' 'Usage: ./scripts/release-dev.sh [--dry-run]'; exit 0 ;;
|
|
*) printf 'Unknown option: %s\n' "$arg" >&2; exit 2 ;;
|
|
esac
|
|
done
|
|
ITEM_ID="${GITEA_VAULT_ITEM_ID:-9fb0e528-b7bd-4ad8-a390-21dc95763374}"
|
|
VAULT_HELPER="${VAULT_HELPER:-/root/.openclaw/workspace/scripts/vault.sh}"
|
|
SCRATCH="${TMPDIR:-/root/.hermes/cache/scratch}"
|
|
mkdir -p "$SCRATCH"
|
|
|
|
command -v bw >/dev/null || { echo "Bitwarden CLI (bw) is required" >&2; exit 1; }
|
|
command -v curl >/dev/null || { echo "curl is required" >&2; exit 1; }
|
|
command -v python3 >/dev/null || { echo "python3 is required" >&2; exit 1; }
|
|
command -v sha256sum >/dev/null || { echo "sha256sum is required" >&2; exit 1; }
|
|
[[ -f "$VAULT_HELPER" ]] || { echo "Vault helper not found: $VAULT_HELPER" >&2; exit 1; }
|
|
|
|
item_file=$(mktemp "$SCRATCH/mg-release-vault.XXXXXX")
|
|
trap 'rm -f "$item_file"' EXIT
|
|
|
|
# The helper loads VAULT_MASTER_PW; use Bitwarden's environment-based unlock
|
|
# so the master password is not placed in command arguments.
|
|
source "$VAULT_HELPER"
|
|
if [[ -z "${BW_SESSION:-}" ]]; then
|
|
echo "Could not obtain a Bitwarden session from the Vault helper" >&2
|
|
exit 1
|
|
fi
|
|
export BW_PASSWORD="${VAULT_MASTER_PW:-}"
|
|
if [[ -z "$BW_PASSWORD" ]]; then
|
|
echo "VAULT_MASTER_PW was not loaded by the Vault helper" >&2
|
|
exit 1
|
|
fi
|
|
export BW_SESSION="$(bw unlock --passwordenv BW_PASSWORD --raw 2>/dev/null)"
|
|
unset BW_PASSWORD
|
|
[[ -n "$BW_SESSION" ]] || { echo "Bitwarden unlock failed" >&2; exit 1; }
|
|
|
|
bw get item "$ITEM_ID" > "$item_file"
|
|
export GITEA_TOKEN="$(python3 - "$item_file" <<'PY'
|
|
import json, sys
|
|
with open(sys.argv[1], encoding="utf-8") as f:
|
|
item = json.load(f)
|
|
print(next((field["value"] for field in item.get("fields", [])
|
|
if field.get("name") == "personal_access_token"), ""), end="")
|
|
PY
|
|
)"
|
|
[[ -n "$GITEA_TOKEN" ]] || { echo "Gitea token field is missing from the configured Vault item" >&2; exit 1; }
|
|
|
|
# Run the established release flow without ever writing credentials into the
|
|
# repository or printing them. Dry-run does authenticated version allocation.
|
|
"$ROOT/scripts/release.sh" --dry-run
|
|
if [[ "$DRY_RUN" -eq 1 ]]; then
|
|
exit 0
|
|
fi
|
|
"$ROOT/scripts/release.sh"
|
|
|
|
APK="$ROOT/build/MG-Utility.apk"
|
|
python3 - "$ROOT" <<'PY'
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import urllib.parse
|
|
import urllib.request
|
|
|
|
root = Path(sys.argv[1])
|
|
build = json.loads((root / "build/assets/mg-update-build.json").read_text())
|
|
tag = f"dev-v{build['versionName']}-c{build['versionCode']}"
|
|
api = os.environ.get("GITEA_API_URL", "https://git.molberg.cloud/api/v1/repos/alex/mg-utility")
|
|
headers = {"Authorization": "token " + os.environ["GITEA_TOKEN"], "User-Agent": "MG-Utility-release-verifier"}
|
|
class SameOriginRedirect(urllib.request.HTTPRedirectHandler):
|
|
def redirect_request(self, request, response, code, message, headers, new_url):
|
|
original = urllib.parse.urlsplit(api)
|
|
redirected = urllib.parse.urlsplit(new_url)
|
|
if (original.scheme, original.netloc) != (redirected.scheme, redirected.netloc):
|
|
raise SystemExit("Refusing an authenticated cross-origin redirect")
|
|
return super().redirect_request(request, response, code, message, headers, new_url)
|
|
|
|
opener = urllib.request.build_opener(SameOriginRedirect())
|
|
with opener.open(urllib.request.Request(api + "/releases/tags/" + tag, headers=headers), timeout=60) as response:
|
|
release = json.load(response)
|
|
metadata = json.loads(release["body"])
|
|
for field in ("channel", "versionName", "versionCode", "branch"):
|
|
if metadata.get(field) != build[field]:
|
|
raise SystemExit("Release metadata mismatch: " + field)
|
|
assets = [asset for asset in release["assets"] if asset["name"] == "MG-Utility.apk"]
|
|
if len(assets) != 1:
|
|
raise SystemExit("Expected exactly one published MG-Utility.apk")
|
|
url = assets[0]["browser_download_url"]
|
|
asset_origin = urllib.parse.urlsplit(url)
|
|
api_origin = urllib.parse.urlsplit(api)
|
|
if (asset_origin.scheme, asset_origin.netloc) != (api_origin.scheme, api_origin.netloc):
|
|
raise SystemExit("Refusing to send release credentials to a different asset host")
|
|
digest = hashlib.sha256()
|
|
with opener.open(urllib.request.Request(url, headers=headers), timeout=120) as response:
|
|
while True:
|
|
chunk = response.read(1024 * 1024)
|
|
if not chunk:
|
|
break
|
|
digest.update(chunk)
|
|
local_digest = hashlib.sha256((root / "build/MG-Utility.apk").read_bytes()).hexdigest()
|
|
if digest.hexdigest() != local_digest or metadata.get("sha256") != local_digest:
|
|
raise SystemExit("Published APK SHA-256 does not match metadata and local artifact")
|
|
print("Verified published Dev APK: downloaded SHA-256 = release metadata = local artifact")
|
|
print("Version:", build["versionName"])
|
|
print("Release tag:", tag)
|
|
print("SHA-256:", local_digest)
|
|
PY
|
|
SDK="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-/opt/android-sdk}}"
|
|
TOOLS="$(find "$SDK/build-tools" -mindepth 1 -maxdepth 1 -type d | sort -V | tail -n 1)"
|
|
"$TOOLS/apksigner" verify -v --min-sdk-version 23 "$APK"
|
|
"$TOOLS/aapt2" dump badging "$APK" | sed -n '1,3p'
|
|
unzip -t "$APK" | tail -n 1
|
|
printf '\nArtifact: %s\n' "$APK"
|
|
ls -lh "$APK"
|
|
sha256sum "$APK"
|