Files
cdxs/.github/workflows/release.yml
T
chuan a9543cd36f
Release / Prepare release (push) Successful in 5s
Release / Build release assets (push) Successful in 19m9s
ci: 移除所有的代理相关
2026-05-28 00:51:24 +08:00

151 lines
5.4 KiB
YAML

name: Release
on:
push:
branches:
- master
paths:
- ".github/workflows/release.yml"
- "scripts/publish.ps1"
- "scripts/publish.sh"
- "scripts/docker/Dockerfile.release"
tags:
- "*"
env:
GITEA_SERVER_URL: https://git.pchuan.top
RUST_IMAGE: docker.m.daocloud.io/library/rust:1-bookworm
APT_MIRROR: https://mirrors.ustc.edu.cn/debian
CARGO_TERM_COLOR: always
jobs:
prepare-release:
name: Prepare release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Create or update Gitea release
shell: bash
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
run: |
set -euo pipefail
if [[ "${GITHUB_REF:-}" == refs/tags/* ]]; then
current_tag="${GITHUB_REF_NAME}"
else
current_tag="$(git describe --tags --abbrev=0)"
fi
version="${current_tag#v}"
tag_target="$(git rev-list -n 1 "${current_tag}^{}")"
previous_tag="$(git describe --tags --abbrev=0 "${current_tag}^{}^" 2>/dev/null || true)"
api_base="${GITEA_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}"
{
echo "## Changes"
echo
echo "Built from commit: \`${GITHUB_SHA}\`"
echo
if [[ -n "${previous_tag}" ]]; then
echo "Changes since \`${previous_tag}\`:"
echo
git log --no-merges --pretty=format:'- %s (%h)' "${previous_tag}..${current_tag}"
else
echo "Initial release changes:"
echo
git log --no-merges --pretty=format:'- %s (%h)' "${current_tag}^{}"
fi
echo
echo
echo "## Artifacts"
echo
echo "- \`cdxs-${version}-windows-x64.exe\`"
echo "- \`cdxs-${version}-linux-x64\`"
echo "- \`cdxs-${version}-linux-arm64\`"
} > RELEASE_NOTES.md
body="$(python3 -c 'import json, pathlib; print(json.dumps(pathlib.Path("RELEASE_NOTES.md").read_text()))')"
payload="$(printf '{"tag_name":"%s","target_commitish":"%s","name":"%s","body":%s,"draft":false,"prerelease":false}' \
"${current_tag}" "${tag_target}" "${current_tag}" "${body}")"
status="$(curl -sS -o release.json -w '%{http_code}' \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
"${api_base}/releases/tags/${current_tag}")"
if [[ "${status}" == "200" ]]; then
release_id="$(python3 -c 'import json; print(json.load(open("release.json"))["id"])')"
curl -sS -X PATCH \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d "${payload}" \
"${api_base}/releases/${release_id}" >/dev/null
elif [[ "${status}" == "404" ]]; then
curl -sS -X POST \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d "${payload}" \
"${api_base}/releases" >/dev/null
else
cat release.json
echo "Unexpected Gitea release lookup status: ${status}" >&2
exit 1
fi
build-release-assets:
name: Build release assets
runs-on: ubuntu-latest
needs: prepare-release
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Resolve release tag
shell: bash
run: |
set -euo pipefail
if [[ "${GITHUB_REF:-}" == refs/tags/* ]]; then
release_tag="${GITHUB_REF_NAME}"
else
release_tag="$(git describe --tags --abbrev=0)"
fi
echo "RELEASE_TAG=${release_tag}" >> "${GITHUB_ENV}"
echo "VERSION=${release_tag#v}" >> "${GITHUB_ENV}"
- name: Build release assets
shell: bash
run: bash scripts/publish.sh --version "${VERSION}" --rust-image "${RUST_IMAGE}" --apt-mirror "${APT_MIRROR}" --clean
- name: Upload release assets
shell: bash
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
run: |
set -euo pipefail
api_base="${GITEA_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}"
curl -sS -H "Authorization: token ${GITEA_TOKEN}" "${api_base}/releases/tags/${RELEASE_TAG}" > release.json
release_id="$(python3 -c 'import json; print(json.load(open("release.json"))["id"])')"
curl -sS -H "Authorization: token ${GITEA_TOKEN}" "${api_base}/releases/${release_id}/assets" > assets.json
for file in dist/*; do
name="$(basename "${file}")"
existing_asset_id="$(python3 -c 'import json, sys; target = sys.argv[1]; print(next((str(asset["id"]) for asset in json.load(open("assets.json")) if asset.get("name") == target), ""))' "${name}")"
if [[ -n "${existing_asset_id}" ]]; then
curl -fsS -X DELETE \
-H "Authorization: token ${GITEA_TOKEN}" \
"${api_base}/releases/${release_id}/assets/${existing_asset_id}"
fi
curl -fsS -X POST \
-H "Authorization: token ${GITEA_TOKEN}" \
-F "attachment=@${file}" \
"${api_base}/releases/${release_id}/assets?name=${name}"
done