ci: upload build outputs to repository release
Some checks failed
Cross Build Windows Release on Linux / cross-build-windows (push) Failing after 0s
Build Windows Release with MSYS2 / build-windows-msys2 (push) Has been cancelled

This commit is contained in:
luochen570
2026-05-30 14:11:23 +08:00
parent 8f6152a382
commit 4832838a0d

View File

@@ -43,6 +43,7 @@ jobs:
ninja-build \
p7zip \
p7zip-plugins \
python3 \
mingw64-gcc-c++ \
mingw64-qt6-qtbase \
mingw64-opencv
@@ -128,35 +129,91 @@ jobs:
dist-windows-cross/chengnan-windows-x86_64-cross.zip.sha256
if-no-files-found: error
- name: Upload package to Gitea Packages
- name: Upload package to repository release
if: ${{ github.event_name != 'pull_request' }}
env:
PACKAGE_TOKEN: ${{ secrets.PACKAGE_TOKEN }}
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
run: |
set -euo pipefail
PACKAGE_NAME="chengnan-windows-x86_64-cross"
VERSION="${GITHUB_SHA:-manual}"
VERSION="${VERSION:0:10}"
OWNER="${GITHUB_REPOSITORY%%/*}"
API_BASE="${GITHUB_SERVER_URL:-https://git.luochen570.cn}/api/packages/${OWNER}/generic/chengnan/${VERSION}"
TOKEN="${PACKAGE_TOKEN:-${GITHUB_TOKEN:-}}"
SERVER_URL="${GITHUB_SERVER_URL:-https://git.luochen570.cn}"
REPOSITORY="${GITHUB_REPOSITORY}"
API_BASE="${SERVER_URL}/api/v1/repos/${REPOSITORY}"
TOKEN="${RELEASE_TOKEN:-${GITHUB_TOKEN:-}}"
test -n "$TOKEN"
upload_file() {
local src="$1"
local url="$2"
local status
if [ "${GITHUB_REF_TYPE:-}" = "tag" ]; then
TAG_NAME="${GITHUB_REF_NAME}"
RELEASE_NAME="${GITHUB_REF_NAME}"
PRERELEASE=false
else
TAG_NAME="build-${GITHUB_SHA:0:10}"
RELEASE_NAME="chengnan ${GITHUB_SHA:0:10}"
PRERELEASE=true
fi
json_escape() {
python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))'
}
release_payload="$(printf '{"tag_name":%s,"target_commitish":%s,"name":%s,"body":%s,"draft":false,"prerelease":%s}' \
"$(printf '%s' "$TAG_NAME" | json_escape)" \
"$(printf '%s' "${GITHUB_SHA}" | json_escape)" \
"$(printf '%s' "$RELEASE_NAME" | json_escape)" \
"$(printf 'Automated Windows x86_64 cross build from Gitea Actions.\n' | json_escape)" \
"$PRERELEASE")"
status="$(curl --show-error --silent --location \
--header "Authorization: token ${TOKEN}" \
--upload-file "$src" \
--header 'Content-Type: application/json' \
--data "$release_payload" \
--write-out '%{http_code}' \
--output /tmp/gitea-upload-response.txt \
"$url")"
--output /tmp/gitea-release-response.json \
"${API_BASE}/releases")"
if [ "$status" = "409" ] || [ "$status" = "422" ]; then
RELEASE_ID="$(curl --show-error --silent --location \
--header "Authorization: token ${TOKEN}" \
"${API_BASE}/releases/tags/${TAG_NAME}" \
| python3 -c 'import json,sys; print(json.load(sys.stdin)["id"])')"
elif [ "$status" = "201" ] || [ "$status" = "200" ]; then
RELEASE_ID="$(python3 -c 'import json,sys; print(json.load(open("/tmp/gitea-release-response.json"))["id"])')"
else
echo "Create release failed with HTTP ${status}" >&2
cat /tmp/gitea-release-response.json >&2
exit 1
fi
upload_asset() {
local src="$1"
local name
local existing_id
local status
name="$(basename "$src")"
existing_id="$(curl --show-error --silent --location \
--header "Authorization: token ${TOKEN}" \
"${API_BASE}/releases/${RELEASE_ID}/assets" \
| ASSET_NAME="$name" python3 -c 'import json,os,sys; assets=json.load(sys.stdin); print(next((str(a["id"]) for a in assets if a.get("name")==os.environ["ASSET_NAME"]), ""))')"
if [ -n "$existing_id" ]; then
curl --show-error --silent --location \
--request DELETE \
--header "Authorization: token ${TOKEN}" \
--output /tmp/gitea-release-asset-delete-response.json \
"${API_BASE}/releases/${RELEASE_ID}/assets/${existing_id}"
fi
status="$(curl --show-error --silent --location \
--header "Authorization: token ${TOKEN}" \
--form "attachment=@${src};filename=${name}" \
--write-out '%{http_code}' \
--output /tmp/gitea-release-asset-response.json \
"${API_BASE}/releases/${RELEASE_ID}/assets?name=${name}")"
if [ "$status" != "201" ] && [ "$status" != "200" ]; then
echo "Upload failed with HTTP ${status}: ${url}" >&2
cat /tmp/gitea-upload-response.txt >&2
echo "Upload release asset failed with HTTP ${status}: ${name}" >&2
cat /tmp/gitea-release-asset-response.json >&2
exit 1
fi
}
upload_file "dist-windows-cross/${PACKAGE_NAME}.zip" "${API_BASE}/${PACKAGE_NAME}.zip"
upload_file "dist-windows-cross/${PACKAGE_NAME}.zip.sha256" "${API_BASE}/${PACKAGE_NAME}.zip.sha256"
echo "Package uploaded to: ${API_BASE}/"
upload_asset "dist-windows-cross/${PACKAGE_NAME}.zip"
upload_asset "dist-windows-cross/${PACKAGE_NAME}.zip.sha256"
echo "Release assets uploaded to ${SERVER_URL}/${REPOSITORY}/releases/tag/${TAG_NAME}"