# 益铸 EntitleForge SDK 发布流水线(仅 v* tag 触发)。 # go module 库的"发版"即 git tag(客户 go get 直接引用);此流水线补齐: # Gitea Release 记录 + 源码包附件,成功后清理失败的 action 记录。 name: sdk-release on: push: tags: ["v*"] env: GOPROXY: https://goproxy.cn,direct GOSUMDB: "off" jobs: release: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version: "1.27.4" mirror: https://mirrors.aliyun.com/golang/ - name: 发布前测试(tag 必须指向全绿代码) run: go test -race -shuffle=on ./... - name: 创建 Release 并上传源码包 env: GITEA_TOKEN: ${{ secrets.REGISTRY_PUSH_TOKEN }} run: | set -euo pipefail API="${GITHUB_SERVER_URL}/api/v1" REPO="${GITHUB_REPOSITORY}" VERSION="${GITHUB_REF_NAME}" AUTH="Authorization: token ${GITEA_TOKEN}" # 产物写到仓库外:在 "." 内创建归档会触发 tar "file changed as we read it"。 ARTIFACT="${RUNNER_TEMP:-/tmp}/entitleforge-sdk-${VERSION}.tar.gz" tar -czf "${ARTIFACT}" --exclude='.git' . # 已存在同 tag release 时复用(重发/重跑幂等)。 RELEASE_ID=$(curl -fsS -H "${AUTH}" "${API}/repos/${REPO}/releases/tags/${VERSION}" \ | python3 -c "import json,sys; print(json.load(sys.stdin).get('id',''))" 2>/dev/null || true) if [ -z "${RELEASE_ID}" ]; then PAYLOAD=$(printf '{"tag_name":"%s","name":"益铸 EntitleForge SDK %s","body":"go get %s@%s(零第三方依赖)","draft":false,"prerelease":false}' \ "${VERSION}" "${VERSION}" "${REPO,,}" "${VERSION}") RELEASE_ID=$(curl -fsS -X POST -H "${AUTH}" -H "Content-Type: application/json" -d "${PAYLOAD}" \ "${API}/repos/${REPO}/releases" \ | python3 -c "import json,sys; print(json.load(sys.stdin).get('id',''))") fi [ -n "${RELEASE_ID}" ] || { echo "release id 获取失败" >&2; exit 1; } # 同名旧附件先删,保证重发同一版本时附件上传幂等。 ASSET_NAME="$(basename "${ARTIFACT}")" curl -fsS -H "${AUTH}" "${API}/repos/${REPO}/releases/${RELEASE_ID}/assets" \ | python3 -c "import json,sys; [print(a['id']) for a in json.load(sys.stdin) if a.get('name')=='${ASSET_NAME}']" \ | while read -r aid; do curl -fsS -o /dev/null -X DELETE -H "${AUTH}" \ "${API}/repos/${REPO}/releases/${RELEASE_ID}/assets/${aid}" done curl -fsS -X POST -H "${AUTH}" --upload-file "${ARTIFACT}" \ "${API}/repos/${REPO}/releases/${RELEASE_ID}/assets?name=${ASSET_NAME}" >/dev/null echo "Release ${VERSION} 创建完成(go module tag 即发版本体,源码包已附)" # 清理失败的 action 记录——always():release 失败时更要清理(否则失败轮次永久堆积) cleanup: needs: release if: always() runs-on: ubuntu-latest steps: - name: 清理失败的 workflow runs env: GITEA_TOKEN: ${{ secrets.REGISTRY_PUSH_TOKEN }} run: | set -eu API="${GITHUB_SERVER_URL}/api/v1" REPO="${GITHUB_REPOSITORY}" PAGE=1 while :; do RUNS=$(curl -sS -H "Authorization: token ${GITEA_TOKEN}" "${API}/repos/${REPO}/actions/tasks?limit=50&page=${PAGE}") TOTAL=$(echo "${RUNS}" | python3 -c "import json,sys; print(json.load(sys.stdin).get('total_count',0))") echo "${RUNS}" | python3 -c "import json,sys; [print(r['url']) for r in json.load(sys.stdin).get('workflow_runs',[]) if r.get('status') in ('failure','cancelled')]" | while read -r url; do RUN_API=$(echo "${url}" | sed "s|${GITHUB_SERVER_URL}/\\(.*\\)/actions/runs/|${API}/repos/\\1/actions/runs/|") echo "删除失败 run: ${url}" curl -sS -o /dev/null -X DELETE -H "Authorization: token ${GITEA_TOKEN}" "${RUN_API}" done [ "$((PAGE * 50))" -ge "${TOTAL}" ] && break PAGE=$((PAGE + 1)) done echo "失败 action 清理完成"