97 lines
4.0 KiB
YAML
97 lines
4.0 KiB
YAML
name: Release Resource Pack
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- '**'
|
|
tags:
|
|
- 'v*'
|
|
- '*.*.*'
|
|
workflow_dispatch:
|
|
|
|
jobs:
|
|
release:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Ensure tools are available (zip, curl, jq)
|
|
run: |
|
|
need_install=false
|
|
for b in zip curl jq; do
|
|
if ! command -v "$b" >/dev/null 2>&1; then need_install=true; fi
|
|
done
|
|
if [ "$need_install" = true ]; then
|
|
sudo apt-get update && sudo apt-get install -y zip curl jq ca-certificates
|
|
fi
|
|
|
|
- name: Zip and SHA1
|
|
run: |
|
|
set -euo pipefail
|
|
test -f pack.mcmeta || { echo "pack.mcmeta not found at repo root"; exit 1; }
|
|
TAG="${GITHUB_REF_NAME:-dev}"
|
|
REPO_NAME="${GITHUB_REPOSITORY##*/}"
|
|
ZIP_NAME="${REPO_NAME}-${TAG}.zip"
|
|
zip -r -9 "${ZIP_NAME}" pack.mcmeta pack.png assets -x "**/.DS_Store" "**/Thumbs.db"
|
|
sha1sum "${ZIP_NAME}" | tee "${ZIP_NAME}.sha1"
|
|
echo "ZIP_PATH=${ZIP_NAME}" >> "$GITHUB_ENV"
|
|
echo "SHA1_PATH=${ZIP_NAME}.sha1" >> "$GITHUB_ENV"
|
|
mkdir -p artifacts
|
|
mv -f "${ZIP_NAME}" "${ZIP_NAME}.sha1" artifacts/
|
|
|
|
- name: Upload artifact (ZIP and SHA1)
|
|
uses: actions/upload-artifact@v3
|
|
with:
|
|
name: resource-pack-${{ github.ref_name }}-${{ github.run_number }}
|
|
path: artifacts/
|
|
if-no-files-found: error
|
|
|
|
- name: Create Release and Upload Assets (Gitea API)
|
|
if: startsWith(github.ref, 'refs/tags/')
|
|
env:
|
|
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
|
|
run: |
|
|
set -euo pipefail
|
|
: "${GITEA_TOKEN:?Missing GITEA_TOKEN secret}"
|
|
TAG="${GITHUB_REF_NAME:-dev}"
|
|
OWNER="${GITHUB_REPOSITORY%/*}"
|
|
REPO="${GITHUB_REPOSITORY##*/}"
|
|
API_BASE="${GITHUB_SERVER_URL%/}/api/v1"
|
|
|
|
echo "Creating or fetching release for tag: $TAG"
|
|
create_json=$(jq -n --arg tag "$TAG" --arg name "$TAG" --arg target "${GITHUB_SHA:-}" '{tag_name:$tag, name:$name, target_commitish:$target, draft:false, prerelease:false}')
|
|
# Try to create the release
|
|
set +e
|
|
resp=$(curl -sS -w "\n%{http_code}" -H "Authorization: token ${GITEA_TOKEN}" -H 'Content-Type: application/json' \
|
|
-X POST "${API_BASE}/repos/${OWNER}/${REPO}/releases" -d "$create_json")
|
|
http_code=$(echo "$resp" | tail -n1)
|
|
body=$(echo "$resp" | head -n-1)
|
|
set -e
|
|
if [ "$http_code" = "201" ] || [ "$http_code" = "200" ]; then
|
|
release_id=$(echo "$body" | jq -r '.id')
|
|
else
|
|
# If already exists, fetch by tag
|
|
body=$(curl -sS -H "Authorization: token ${GITEA_TOKEN}" "${API_BASE}/repos/${OWNER}/${REPO}/releases/tags/${TAG}")
|
|
release_id=$(echo "$body" | jq -r '.id')
|
|
if [ -z "$release_id" ] || [ "$release_id" = "null" ]; then
|
|
echo "Failed to create or fetch release (HTTP $http_code)" >&2
|
|
echo "$body" >&2
|
|
exit 1
|
|
fi
|
|
fi
|
|
echo "Release ID: $release_id"
|
|
|
|
# Upload assets; remove existing with same name if present
|
|
for path in "$ZIP_PATH" "$SHA1_PATH"; do
|
|
name=$(basename "$path")
|
|
assets=$(curl -sS -H "Authorization: token ${GITEA_TOKEN}" "${API_BASE}/repos/${OWNER}/${REPO}/releases/${release_id}/assets")
|
|
existing_id=$(echo "$assets" | jq -r ".[] | select(.name==\"${name}\") | .id" || true)
|
|
if [ -n "${existing_id:-}" ] && [ "${existing_id}" != "null" ]; then
|
|
curl -sS -X DELETE -H "Authorization: token ${GITEA_TOKEN}" "${API_BASE}/repos/${OWNER}/${REPO}/releases/assets/${existing_id}" >/dev/null || true
|
|
fi
|
|
curl -sS -H "Authorization: token ${GITEA_TOKEN}" -F "attachment=@${path}" -F "name=${name}" \
|
|
"${API_BASE}/repos/${OWNER}/${REPO}/releases/${release_id}/assets" >/dev/null
|
|
echo "Uploaded ${name}"
|
|
done
|