Build way-node / Build and Push (push) Successful in 1m16s
1) install.sh: Gitea 不支持 GitHub 的 /releases/latest/download/ 简写 (实测 404),改成先用 Gitea API /releases/latest 查 tag_name, 再拼 /releases/download/<tag>/<artifact>(实测 200,66875554 bytes)。 解析用 sed,不依赖 jq/python。 2) CI: 上一轮 release 上传静默失败,日志是 '/root/.cache/act/.../7.sh: line 6: curl: command not found' 根因是装 curl 的语句写在 'if ! command -v docker' 里面, 而 docker 本来就存在(runner 挂了 docker.sock),整段被跳过。 现在 curl 单独检查安装;release id 解析也从 python3 换成 sed。
126 lines
5.2 KiB
YAML
126 lines
5.2 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 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
|
||
RID=$(sed -n 's/.*"id"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p' /tmp/rel.json 2>/dev/null | head -1)
|
||
if [ -z "$RID" ]; then
|
||
RID=$(curl -sS -H "$AUTH" "${API}/releases/tags/${TAG}" | sed -n 's/.*"id"[[:space:]]*:[[:space:]]*\([0-9][0-9]*\).*/\1/p' | head -1)
|
||
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"
|