Skip to content

Commit e12debc

Browse files
committed
fix: docker images build
1 parent 5bc9d93 commit e12debc

File tree

9 files changed

+377
-100
lines changed

9 files changed

+377
-100
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# detect-go-module-root
2+
3+
Composite action:在仓库根或子目录中查找包含 `go.mod``cmd/runner-manager` 的 Go 模块根目录。
4+
5+
- **Input**`working_dir`(默认 `.`),优先检查该目录下是否有 `cmd/runner-manager`
6+
- **Output**`root`(模块根路径)、`found``true`/`false`)。
7+
8+
`ci-manager.yml``ci-agent.yml` 等复用,便于单仓或 monorepo 共用同一套 CI。
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 检测 Go 模块根目录(含 go.mod 与 cmd/runner-manager 的目录),便于单仓或 monorepo 复用。
2+
# 使用方通过 env.WORKING_DIR 传入期望根目录(默认 .),若该目录下无 cmd/runner-manager 则自动查找。
3+
name: Detect Go module root
4+
description: 输出模块根路径 root 与是否找到 found (true/false)
5+
6+
inputs:
7+
working_dir:
8+
description: 期望的模块根目录,如 . 或子目录
9+
required: false
10+
default: .
11+
12+
outputs:
13+
root:
14+
description: 模块根目录路径
15+
value: ${{ steps.detect.outputs.root }}
16+
found:
17+
description: 是否找到 (true/false)
18+
value: ${{ steps.detect.outputs.found }}
19+
20+
runs:
21+
using: composite
22+
steps:
23+
- id: detect
24+
shell: bash
25+
run: |
26+
set -e
27+
cd "$GITHUB_WORKSPACE"
28+
WORKING_DIR="${{ inputs.working_dir }}"
29+
ROOT=""
30+
if [ -d "$GITHUB_WORKSPACE/${WORKING_DIR}/cmd/runner-manager" ]; then
31+
ROOT="${WORKING_DIR}"
32+
fi
33+
if [ -z "$ROOT" ]; then
34+
ROOT=$(find . -name go.mod 2>/dev/null | while IFS= read -r f; do
35+
d=$(dirname "$f")
36+
if [ -d "$d/cmd/runner-manager" ]; then echo "$d"; break; fi
37+
done | head -1)
38+
fi
39+
if [ -z "$ROOT" ]; then
40+
FOUND_DIR=$(find . -type d -path '*/cmd/runner-manager' 2>/dev/null | head -1)
41+
if [ -n "$FOUND_DIR" ]; then
42+
ROOT=$(dirname "$(dirname "$FOUND_DIR")")
43+
fi
44+
fi
45+
if [ -n "$ROOT" ]; then
46+
echo "root=$ROOT" >> $GITHUB_OUTPUT
47+
echo "found=true" >> $GITHUB_OUTPUT
48+
echo "Using Go module root: $ROOT"
49+
else
50+
echo "root=." >> $GITHUB_OUTPUT
51+
echo "found=false" >> $GITHUB_OUTPUT
52+
echo "::notice::No go.mod + cmd/runner-manager found, skipping."
53+
fi

.github/workflows/ci-agent.yml

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# CI:Runner Agent 的测试与构建
2+
name: CI (Agent)
3+
4+
on:
5+
push:
6+
branches: [main, master, develop]
7+
pull_request:
8+
branches: [main, master, develop]
9+
10+
env:
11+
GO_VERSION: "1.26.0"
12+
WORKING_DIR: .
13+
14+
jobs:
15+
test:
16+
name: Test
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v6
21+
22+
- name: Detect Go module root
23+
id: modroot
24+
uses: ./.github/actions/detect-go-module-root
25+
with:
26+
working_dir: ${{ env.WORKING_DIR }}
27+
28+
- name: Set up Go
29+
if: steps.modroot.outputs.found == 'true'
30+
uses: actions/setup-go@v6
31+
with:
32+
go-version: ${{ env.GO_VERSION }}
33+
cache-dependency-path: ${{ steps.modroot.outputs.root }}/go.sum
34+
35+
- name: Vet
36+
if: steps.modroot.outputs.found == 'true'
37+
run: go vet ./cmd/runner-agent/... ./internal/...
38+
working-directory: ${{ steps.modroot.outputs.root }}
39+
40+
- name: Lint
41+
if: steps.modroot.outputs.found == 'true'
42+
uses: golangci/golangci-lint-action@v9
43+
with:
44+
version: latest
45+
args: --max-same-issues=100000 ./cmd/runner-agent/... ./internal/...
46+
working-directory: ${{ steps.modroot.outputs.root }}
47+
48+
- name: Run tests
49+
if: steps.modroot.outputs.found == 'true'
50+
run: go test -v ./cmd/runner-agent/... ./internal/...
51+
working-directory: ${{ steps.modroot.outputs.root }}
52+
53+
- name: Skip (no project structure)
54+
if: steps.modroot.outputs.found != 'true'
55+
run: echo "Skipped: no go.mod + cmd/runner-manager in this repo."
56+
57+
build:
58+
name: Build
59+
runs-on: ubuntu-latest
60+
needs: [test]
61+
timeout-minutes: 10
62+
steps:
63+
- name: Checkout code
64+
uses: actions/checkout@v6
65+
66+
- name: Detect Go module root
67+
id: modroot
68+
uses: ./.github/actions/detect-go-module-root
69+
with:
70+
working_dir: ${{ env.WORKING_DIR }}
71+
72+
- name: Set up Go
73+
if: steps.modroot.outputs.found == 'true'
74+
uses: actions/setup-go@v6
75+
with:
76+
go-version: ${{ env.GO_VERSION }}
77+
cache-dependency-path: ${{ steps.modroot.outputs.root }}/go.sum
78+
79+
- name: Build Runner Agent
80+
if: steps.modroot.outputs.found == 'true'
81+
run: go build -o runner-agent ./cmd/runner-agent
82+
working-directory: ${{ steps.modroot.outputs.root }}
83+
84+
- name: Verify binary
85+
if: steps.modroot.outputs.found == 'true'
86+
run: test -x runner-agent && ls -la runner-agent
87+
working-directory: ${{ steps.modroot.outputs.root }}
88+
89+
- name: Skip (no project structure)
90+
if: steps.modroot.outputs.found != 'true'
91+
run: echo "Skipped: no go.mod + cmd/runner-manager in this repo."
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
name: CI
1+
# CI:Runner Manager 的测试与构建
2+
name: CI (Manager)
23

34
on:
45
push:
@@ -8,7 +9,6 @@ on:
89

910
env:
1011
GO_VERSION: "1.26.0"
11-
# 若 Go 代码在子目录(与 publish-image 的 DOCKER_CONTEXT 一致),改为该目录如 github-actions;留空则自动检测含 go.mod 且含 cmd/runner-manager 的目录
1212
WORKING_DIR: .
1313

1414
jobs:
@@ -21,103 +21,81 @@ jobs:
2121

2222
- name: Detect Go module root
2323
id: modroot
24-
run: |
25-
set -e
26-
cd "$GITHUB_WORKSPACE"
27-
ROOT=""
28-
if [ -d "$GITHUB_WORKSPACE/${{ env.WORKING_DIR }}/cmd/runner-manager" ]; then
29-
ROOT="${{ env.WORKING_DIR }}"
30-
fi
31-
if [ -z "$ROOT" ]; then
32-
ROOT=$(find . -name go.mod 2>/dev/null | while IFS= read -r f; do
33-
d=$(dirname "$f")
34-
if [ -d "$d/cmd/runner-manager" ]; then echo "$d"; break; fi
35-
done | head -1)
36-
fi
37-
if [ -z "$ROOT" ]; then
38-
echo "::error::Could not find directory containing go.mod and cmd/runner-manager. Set env WORKING_DIR to the Go module root (e.g. . or a subdir)."
39-
exit 1
40-
fi
41-
echo "root=$ROOT" >> $GITHUB_OUTPUT
42-
echo "Using Go module root: $ROOT"
24+
uses: ./.github/actions/detect-go-module-root
25+
with:
26+
working_dir: ${{ env.WORKING_DIR }}
4327

4428
- name: Set up Go
29+
if: steps.modroot.outputs.found == 'true'
4530
uses: actions/setup-go@v6
4631
with:
4732
go-version: ${{ env.GO_VERSION }}
4833
cache-dependency-path: ${{ steps.modroot.outputs.root }}/go.sum
4934

5035
- name: Vet
51-
run: go vet ./...
36+
if: steps.modroot.outputs.found == 'true'
37+
run: go vet ./cmd/runner-manager/... ./internal/...
5238
working-directory: ${{ steps.modroot.outputs.root }}
5339

5440
- name: Lint
41+
if: steps.modroot.outputs.found == 'true'
5542
uses: golangci/golangci-lint-action@v9
5643
with:
5744
version: latest
58-
args: --max-same-issues=100000
45+
args: --max-same-issues=100000 ./cmd/runner-manager/... ./internal/...
5946
working-directory: ${{ steps.modroot.outputs.root }}
6047

6148
- name: Run tests
62-
run: go test -v ./...
49+
if: steps.modroot.outputs.found == 'true'
50+
run: go test -v ./cmd/runner-manager/... ./internal/...
6351
working-directory: ${{ steps.modroot.outputs.root }}
6452

53+
- name: Skip (no project structure)
54+
if: steps.modroot.outputs.found != 'true'
55+
run: echo "Skipped: no go.mod + cmd/runner-manager in this repo."
56+
6557
build:
6658
name: Build
6759
runs-on: ubuntu-latest
6860
needs: [test]
61+
timeout-minutes: 15
6962
steps:
7063
- name: Checkout code
7164
uses: actions/checkout@v6
7265

7366
- name: Detect Go module root
7467
id: modroot
75-
run: |
76-
set -e
77-
cd "$GITHUB_WORKSPACE"
78-
ROOT=""
79-
if [ -d "$GITHUB_WORKSPACE/${{ env.WORKING_DIR }}/cmd/runner-manager" ]; then
80-
ROOT="${{ env.WORKING_DIR }}"
81-
fi
82-
if [ -z "$ROOT" ]; then
83-
ROOT=$(find . -name go.mod 2>/dev/null | while IFS= read -r f; do
84-
d=$(dirname "$f")
85-
if [ -d "$d/cmd/runner-manager" ]; then echo "$d"; break; fi
86-
done | head -1)
87-
fi
88-
if [ -z "$ROOT" ]; then
89-
echo "::error::Could not find directory containing go.mod and cmd/runner-manager. Set env WORKING_DIR to the Go module root (e.g. . or a subdir)."
90-
exit 1
91-
fi
92-
echo "root=$ROOT" >> $GITHUB_OUTPUT
93-
echo "Using Go module root: $ROOT"
68+
uses: ./.github/actions/detect-go-module-root
69+
with:
70+
working_dir: ${{ env.WORKING_DIR }}
9471

9572
- name: Set up Go
73+
if: steps.modroot.outputs.found == 'true'
9674
uses: actions/setup-go@v6
9775
with:
9876
go-version: ${{ env.GO_VERSION }}
9977
cache-dependency-path: ${{ steps.modroot.outputs.root }}/go.sum
10078

10179
- name: Get version information
80+
if: steps.modroot.outputs.found == 'true'
10281
id: version
10382
run: |
10483
VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "dev-$(git rev-parse --short HEAD)")
10584
echo "version=$VERSION" >> $GITHUB_OUTPUT
10685
10786
- name: Build Manager
87+
if: steps.modroot.outputs.found == 'true'
10888
run: |
10989
go build -ldflags "-s -w -X main.Version=${{ steps.version.outputs.version }}" -o runner-manager ./cmd/runner-manager
11090
working-directory: ${{ steps.modroot.outputs.root }}
11191

112-
- name: Build Runner Agent (container mode)
113-
run: go build -o runner-agent ./cmd/runner-agent
114-
working-directory: ${{ steps.modroot.outputs.root }}
115-
11692
- name: Init example config
93+
if: steps.modroot.outputs.found == 'true'
11794
run: cp config.yaml.example config.yaml
11895
working-directory: ${{ steps.modroot.outputs.root }}
11996

12097
- name: Verify binary
98+
if: steps.modroot.outputs.found == 'true'
12199
run: |
122100
./runner-manager -version
123101
./runner-manager -config config.yaml &
@@ -128,3 +106,7 @@ jobs:
128106
kill $PID 2>/dev/null || true
129107
wait $PID 2>/dev/null || true
130108
working-directory: ${{ steps.modroot.outputs.root }}
109+
110+
- name: Skip (no project structure)
111+
if: steps.modroot.outputs.found != 'true'
112+
run: echo "Skipped: no go.mod + cmd/runner-manager in this repo."
Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# 参考 https://github.com/soulteary/buzzy/tree/main/.github/workflows
2-
# 在 push 到 main/master 或推送 tag 时自动构建并推送镜像到 GHCR
3-
name: Build and publish container image to GHCR
1+
# push main/master 或 tag 时:构建并推送 Runner 容器镜像到 GHCR(tag 带 -runner 后缀)
2+
name: Publish image (Runner)
43

54
on:
65
push:
@@ -12,7 +11,7 @@ on:
1211
workflow_dispatch:
1312

1413
concurrency:
15-
group: publish-${{ github.workflow }}-${{ github.ref }}
14+
group: publish-runner-${{ github.ref }}
1615
cancel-in-progress: true
1716

1817
permissions:
@@ -22,12 +21,11 @@ permissions:
2221
env:
2322
REGISTRY: ghcr.io
2423
IMAGE_NAME: ${{ github.repository }}
25-
# 构建上下文:单仓库用 .;若为 monorepo(Dockerfile 与 cmd 在子目录),改为该子目录如 ./github-actions
2624
DOCKER_CONTEXT: .
2725

2826
jobs:
2927
build:
30-
name: Build and push image
28+
name: Build and push Runner image
3129
runs-on: ubuntu-latest
3230
timeout-minutes: 45
3331
steps:
@@ -73,24 +71,6 @@ jobs:
7371
type=semver,pattern={{major}},enable=${{ startsWith(github.ref, 'refs/tags/v') }}
7472
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v') }}
7573
76-
- name: Prepare config for image
77-
run: cp config.yaml.example config.yaml
78-
working-directory: ${{ env.DOCKER_CONTEXT }}
79-
80-
- name: Build and push Manager image
81-
uses: docker/build-push-action@v6
82-
with:
83-
context: ${{ env.DOCKER_CONTEXT }}
84-
file: ${{ env.DOCKER_CONTEXT }}/Dockerfile
85-
push: true
86-
tags: ${{ steps.meta.outputs.tags }}
87-
labels: ${{ steps.meta.outputs.labels }}
88-
build-args: |
89-
VERSION=${{ steps.version.outputs.version }}
90-
platforms: linux/amd64,linux/arm64
91-
cache-from: type=gha
92-
cache-to: type=gha,mode=max
93-
9474
- name: Prepare Runner tags (same image, tag suffix -runner)
9575
id: runner-tags
9676
run: |

0 commit comments

Comments
 (0)