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)
117 lines
4.8 KiB
YAML
117 lines
4.8 KiB
YAML
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
|
||
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 curl
|
||
elif command -v apt-get >/dev/null 2>&1; then
|
||
apt-get update && apt-get install -y docker.io git curl
|
||
fi
|
||
fi
|
||
docker --version
|
||
|
||
- 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
|
||
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 " ${{ env.IMAGE }}:${{ steps.ver.outputs.short_sha }}"
|
||
echo " ${{ env.IMAGE }}:${{ steps.ver.outputs.version }}"
|
||
echo " ${{ env.IMAGE }}:latest"
|