Files
way-node/.gitea/workflows/build.yml
T
admin b7e079ac14
Build way-node / Build and Push (push) Successful in 1m14s
fix(ci): release id 解析取顶层字段
上一轮日志显示 '✓ 资产已上传: .../releases/1',RID 解析成了 1。
Gitea 的 release JSON 里 "id" 出现多次(顶层、author.id、repo.id),
按逗号拆行后用 ^"id": 锚定行首匹配不到(首行是 {"id":458 带花括号),
改回 grep -o '"id":[0-9]*' | head -1 —— 顶层 id 总是最先出现。

已用真实 API 验证:
  /releases/tags/20261127-66caaba → 解析出 458 ✓

同时补上了 release 458 缺失的 way-node-linux-amd64 资产(手动)。
2026-11-27 11:48:58 +08:00

131 lines
5.5 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: Build way-node
# 只保留 .gitea/workflows 这一份;.github/workflows/ci.yml 依赖 ghcr.io 与
# GITHUB_TOKEN,在 Gitea 上跑不通,已重命名为 .disabled。
#
# 关键约束(第一次构建的失败教训):
# act_runner 的 job 容器里 $PWD 是容器内路径,而 docker.sock 连的是宿主 docker,
# 所以 `docker run -v "$PWD":/src` 挂进去是空目录,报
# "pattern ./internal/...: directory prefix internal does not contain main module"
# 编译因此不在 workflow 里做,交给 Dockerfile 的多阶段构建;二进制从镜像里 cp 出来。
on:
push:
branches:
- master
- dev
tags:
- 'v*'
paths-ignore:
- '**.md'
- 'docs-*.md'
workflow_dispatch:
env:
REGISTRY: git.viaeon.com
IMAGE: git.viaeon.com/admin/way-node
jobs:
build:
name: Build and Push
runs-on: ubuntu
steps:
- name: Install Docker CLI and curl
run: |
# docker 通常已存在(挂 docker.sock),所以 curl 要单独保证 —— 上一轮
# 就是因为整段 if 被跳过、curl 缺失,release 上传那步静默失败
if ! command -v docker >/dev/null 2>&1; then
if command -v apk >/dev/null 2>&1; then
apk add --no-cache docker-cli git
elif command -v apt-get >/dev/null 2>&1; then
apt-get update && apt-get install -y docker.io git
fi
fi
if ! command -v curl >/dev/null 2>&1; then
if command -v apk >/dev/null 2>&1; then
apk add --no-cache curl
elif command -v apt-get >/dev/null 2>&1; then
apt-get update && apt-get install -y curl
fi
fi
docker --version && curl --version | head -1
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Prepare version
id: ver
run: |
SHORT_SHA=$(git rev-parse --short=7 HEAD)
VERSION=$(date '+%Y%m%d')-${SHORT_SHA}
echo "short_sha=${SHORT_SHA}" >> "$GITHUB_OUTPUT"
echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
echo "版本: ${VERSION}"
- name: Login to registry
run: |
echo "${{ secrets.TOKEN }}" | docker login "${{ env.REGISTRY }}" -u "${{ github.actor }}" --password-stdin
- name: Build image
run: |
docker build \
--build-arg VERSION=${{ steps.ver.outputs.version }} \
--label "org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}" \
--label "org.opencontainers.image.revision=${{ github.sha }}" \
--label "org.opencontainers.image.version=${{ steps.ver.outputs.version }}" \
-t "${{ env.IMAGE }}:${{ steps.ver.outputs.short_sha }}" \
-t "${{ env.IMAGE }}:${{ steps.ver.outputs.version }}" \
-t "${{ env.IMAGE }}:latest" \
.
docker images --format '{{.Repository}}:{{.Tag}} {{.Size}}' | grep way-node | head -3
- name: Push image
run: |
docker push "${{ env.IMAGE }}:${{ steps.ver.outputs.short_sha }}"
docker push "${{ env.IMAGE }}:${{ steps.ver.outputs.version }}"
docker push "${{ env.IMAGE }}:latest"
- name: Extract binary from image
run: |
# install.sh 需要独立二进制,直接从刚构建的镜像里取,避免二次编译
CID=$(docker create "${{ env.IMAGE }}:latest")
docker cp "$CID:/usr/local/bin/way-node" way-node-linux-amd64
docker rm -f "$CID" >/dev/null
ls -lh way-node-linux-amd64 | awk '{print " way-node-linux-amd64", $5}'
- name: Upload binary to release
if: startsWith(github.ref, 'refs/tags/') || github.ref == 'refs/heads/master'
run: |
# 用 Gitea API 建/更新 release 并挂资产,供 install.sh 下载
TAG="${{ steps.ver.outputs.version }}"
API="${{ github.server_url }}/api/v1/repos/${{ github.repository }}"
AUTH="Authorization: token ${{ secrets.TOKEN }}"
curl -sS -X POST -H "$AUTH" -H "Content-Type: application/json" \
-d "{\"tag_name\":\"${TAG}\",\"name\":\"${TAG}\",\"body\":\"way-node ${TAG}\",\"target_commitish\":\"${{ github.sha }}\"}" \
"${API}/releases" -o /tmp/rel.json || true
# Gitea 的 release JSON 里 "id" 会出现多次(author.id、repo.id…),
# 必须先按逗号拆行,只取顶层那个 "id",否则会拿到 1
# 顶层 "id" 在 Gitea 的 release JSON 里是最先出现的那个(后面 author.id/repo.id 才是 1)
rid_of() { grep -o '"id":[0-9]*' | head -1 | cut -d: -f2; }
RID=$(rid_of < /tmp/rel.json 2>/dev/null)
if [ -z "$RID" ]; then
RID=$(curl -sS -H "$AUTH" "${API}/releases/tags/${TAG}" | rid_of)
fi
echo " 解析到 release id: ${RID}"
if [ -n "$RID" ]; then
curl -sS -X POST -H "$AUTH" -F "attachment=@way-node-linux-amd64" \
"${API}/releases/${RID}/assets?name=way-node-linux-amd64" >/dev/null \
&& echo " ✓ 资产已上传: ${API}/releases/${RID}"
else
echo " ⚠ release 创建失败,二进制未上传(镜像已推送,docker 部署不受影响)"
fi
- name: Summary
run: |
echo "镜像:"
echo " ${{ env.IMAGE }}:${{ steps.ver.outputs.short_sha }}"
echo " ${{ env.IMAGE }}:${{ steps.ver.outputs.version }}"
echo " ${{ env.IMAGE }}:latest"