fix(ci): 修正 act_runner 里的挂载路径问题
Build way-node / Build and Push (push) Successful in 1m0s

第一次构建失败:
  docker run -v "$PWD":/src golang:1.26 go test ./internal/...
  → pattern ./internal/...: directory prefix internal does not contain main module

根因:act_runner 的 job 容器里 $PWD 是容器内路径,而 docker.sock 连的是宿主
docker,所以 -v 挂进去的是空目录。

改法:
- 去掉 workflow 里的 docker run(test / cross compile 两处)
- 编译交给 Dockerfile 的多阶段构建(golang:1.26-alpine builder 阶段)
- 二进制从构建好的镜像里 docker cp 出来,供 install.sh 下载
- 用 Gitea API 建 release 并上传资产(替代 softprops/action-gh-release)
This commit is contained in:
2026-11-27 11:43:03 +08:00
parent 860fb66442
commit ed33412f04
+41 -24
View File
@@ -3,7 +3,11 @@ name: Build way-node
# 只保留 .gitea/workflows 这一份;.github/workflows/ci.yml 依赖 ghcr.io 与
# GITHUB_TOKEN,在 Gitea 上跑不通,已重命名为 .disabled。
#
# 构建全部在容器里做(runner 只提供 docker.sock),不依赖宿主安装 Go。
# 关键约束(第一次构建的失败教训):
# 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:
@@ -20,20 +24,19 @@ on:
env:
REGISTRY: git.viaeon.com
IMAGE: git.viaeon.com/admin/way-node
GO_IMAGE: golang:1.26
jobs:
build:
name: Test, Build, Push
name: Build and Push
runs-on: ubuntu
steps:
- name: Install Docker CLI
run: |
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
apk add --no-cache docker-cli git curl
elif command -v apt-get >/dev/null 2>&1; then
apt-get update && apt-get install -y docker.io git
apt-get update && apt-get install -y docker.io git curl
fi
fi
docker --version
@@ -52,21 +55,6 @@ jobs:
echo "version=${VERSION}" >> "$GITHUB_OUTPUT"
echo "版本: ${VERSION}"
- name: Unit tests
run: |
docker run --rm -v "$PWD":/src -w /src \
-e GOFLAGS=-mod=mod \
"${{ env.GO_IMAGE }}" \
sh -c "go test -count=1 ./internal/..."
- name: Cross compile binaries
run: |
docker run --rm -v "$PWD":/src -w /src \
-e GOFLAGS=-mod=mod \
"${{ env.GO_IMAGE }}" \
sh -c "make build-linux build-linux-arm64"
ls -lh way-node-linux-* xbctl-linux-* 2>/dev/null | awk '{print " " $9, $5}'
- name: Login to registry
run: |
echo "${{ secrets.TOKEN }}" | docker login "${{ env.REGISTRY }}" -u "${{ github.actor }}" --password-stdin
@@ -74,6 +62,7 @@ jobs:
- 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 }}" \
@@ -81,6 +70,7 @@ jobs:
-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: |
@@ -88,12 +78,39 @@ jobs:
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
RID=$(python3 -c "import json;print(json.load(open('/tmp/rel.json')).get('id',''))" 2>/dev/null || echo "")
if [ -z "$RID" ]; then
RID=$(curl -sS -H "$AUTH" "${API}/releases/tags/${TAG}" | python3 -c "import json,sys;print(json.load(sys.stdin).get('id',''))" 2>/dev/null || echo "")
fi
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 "镜像:"
echo " ${{ env.IMAGE }}:${{ steps.ver.outputs.short_sha }}"
echo " ${{ env.IMAGE }}:${{ steps.ver.outputs.version }}"
echo " ${{ env.IMAGE }}:latest"
echo
echo "本地产物:"
ls -lh way-node-linux-* xbctl-linux-* 2>/dev/null || true