Skip to content

Commit 5b326cd

Browse files
committed
ci: update ci build
1 parent 31c3c3c commit 5b326cd

File tree

3 files changed

+257
-8
lines changed

3 files changed

+257
-8
lines changed

.github/workflows/build.yml

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,57 @@
1-
name: Build and Test
1+
name: CI
22

33
on:
44
push:
5-
branches: [main, master]
5+
branches: [main, master, develop]
66
pull_request:
7-
branches: [main, master]
7+
branches: [main, master, develop]
8+
9+
env:
10+
GO_VERSION: "1.26.0"
811

912
jobs:
10-
build:
13+
test:
14+
name: Test
1115
runs-on: ubuntu-latest
1216
steps:
13-
- uses: actions/checkout@v6
17+
- name: Checkout code
18+
uses: actions/checkout@v6
1419

1520
- name: Set up Go
1621
uses: actions/setup-go@v6
1722
with:
18-
go-version: "1.26.0"
23+
go-version: ${{ env.GO_VERSION }}
1924
cache-dependency-path: go.sum
2025

2126
- name: Vet
2227
run: go vet ./...
2328

2429
- name: Run tests
25-
run: go test ./...
30+
run: go test -v ./...
31+
32+
build:
33+
name: Build
34+
runs-on: ubuntu-latest
35+
needs: [test]
36+
steps:
37+
- name: Checkout code
38+
uses: actions/checkout@v6
39+
40+
- name: Set up Go
41+
uses: actions/setup-go@v6
42+
with:
43+
go-version: ${{ env.GO_VERSION }}
44+
cache-dependency-path: go.sum
45+
46+
- name: Get version information
47+
id: version
48+
run: |
49+
VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "dev-$(git rev-parse --short HEAD)")
50+
echo "version=$VERSION" >> $GITHUB_OUTPUT
2651
2752
- name: Build
28-
run: go build -ldflags "-X main.Version=${{ github.sha }}" -o runner-manager .
53+
run: |
54+
go build -ldflags "-s -w -X main.Version=${{ steps.version.outputs.version }}" -o runner-manager .
2955
3056
- name: Init example config
3157
run: cp config.yaml.example config.yaml
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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
4+
5+
on:
6+
push:
7+
branches:
8+
- main
9+
- master
10+
tags:
11+
- 'v*'
12+
workflow_dispatch:
13+
14+
concurrency:
15+
group: publish-${{ github.workflow }}-${{ github.ref }}
16+
cancel-in-progress: true
17+
18+
permissions:
19+
contents: read
20+
packages: write
21+
22+
env:
23+
REGISTRY: ghcr.io
24+
IMAGE_NAME: ${{ github.repository }}
25+
26+
jobs:
27+
build:
28+
name: Build and push image
29+
runs-on: ubuntu-latest
30+
timeout-minutes: 45
31+
steps:
32+
- name: Checkout
33+
uses: actions/checkout@v6
34+
35+
- name: Set up Docker Buildx
36+
uses: docker/setup-buildx-action@v3
37+
38+
- name: Log in to GHCR
39+
uses: docker/login-action@v3
40+
with:
41+
registry: ${{ env.REGISTRY }}
42+
username: ${{ github.actor }}
43+
password: ${{ secrets.GITHUB_TOKEN }}
44+
45+
- name: Compute canonical image name (lowercase)
46+
id: vars
47+
shell: bash
48+
run: |
49+
set -eu
50+
IMAGE_REF="${IMAGE_NAME:-$GITHUB_REPOSITORY}"
51+
CANONICAL_IMAGE="${REGISTRY}/${IMAGE_REF,,}"
52+
echo "canonical=${CANONICAL_IMAGE}" >> "$GITHUB_OUTPUT"
53+
54+
- name: Get version for build
55+
id: version
56+
run: |
57+
VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "dev-$(git rev-parse --short HEAD)")
58+
echo "version=$VERSION" >> $GITHUB_OUTPUT
59+
60+
- name: Extract Docker metadata (tags, labels)
61+
id: meta
62+
uses: docker/metadata-action@v5
63+
with:
64+
images: ${{ steps.vars.outputs.canonical }}
65+
tags: |
66+
type=ref,event=branch
67+
type=ref,event=tag
68+
type=sha,format=short,prefix=sha-
69+
type=semver,pattern={{version}},enable=${{ startsWith(github.ref, 'refs/tags/v') }}
70+
type=semver,pattern={{major}}.{{minor}},enable=${{ startsWith(github.ref, 'refs/tags/v') }}
71+
type=semver,pattern={{major}},enable=${{ startsWith(github.ref, 'refs/tags/v') }}
72+
type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v') }}
73+
74+
- name: Prepare config for image
75+
run: cp config.yaml.example config.yaml
76+
77+
- name: Build and push
78+
uses: docker/build-push-action@v6
79+
with:
80+
context: .
81+
file: ./Dockerfile
82+
push: true
83+
tags: ${{ steps.meta.outputs.tags }}
84+
labels: ${{ steps.meta.outputs.labels }}
85+
build-args: |
86+
VERSION=${{ steps.version.outputs.version }}
87+
platforms: linux/amd64,linux/arm64
88+
cache-from: type=gha
89+
cache-to: type=gha,mode=max

.github/workflows/release.yml

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# 参考 https://github.com/soulteary/stargate
2+
# 推送 tag v*.*.* 时自动构建多架构 Docker 镜像并发布到 GHCR,同时创建 GitHub Release
3+
name: Release
4+
5+
on:
6+
push:
7+
tags:
8+
- 'v*.*.*'
9+
10+
env:
11+
GO_VERSION: "1.26.0"
12+
REGISTRY: ghcr.io
13+
IMAGE_NAME: ${{ github.repository }}
14+
15+
jobs:
16+
release:
17+
name: Release
18+
runs-on: ubuntu-latest
19+
permissions:
20+
contents: write
21+
packages: write
22+
23+
steps:
24+
- name: Checkout code
25+
uses: actions/checkout@v6
26+
with:
27+
fetch-depth: 0
28+
29+
- name: Set up Go
30+
uses: actions/setup-go@v6
31+
with:
32+
go-version: ${{ env.GO_VERSION }}
33+
cache-dependency-path: go.sum
34+
35+
- name: Run tests
36+
run: go test -v ./...
37+
38+
- name: Extract version from tag
39+
id: version
40+
run: |
41+
VERSION=${GITHUB_REF#refs/tags/}
42+
VERSION=${VERSION#v}
43+
COMMIT=$(git rev-parse --short HEAD)
44+
BUILD_DATE=$(date -u +%FT%T%z)
45+
echo "version=$VERSION" >> $GITHUB_OUTPUT
46+
echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
47+
echo "commit=$COMMIT" >> $GITHUB_OUTPUT
48+
echo "build_date=$BUILD_DATE" >> $GITHUB_OUTPUT
49+
50+
- name: Build binaries
51+
run: |
52+
mkdir -p dist
53+
VERSION=${{ steps.version.outputs.version }}
54+
COMMIT=${{ github.sha }}
55+
LDFLAGS="-s -w -X main.Version=v${VERSION}"
56+
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "${LDFLAGS}" -o dist/runner-manager-linux-amd64 .
57+
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -ldflags "${LDFLAGS}" -o dist/runner-manager-linux-arm64 .
58+
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "${LDFLAGS}" -o dist/runner-manager-darwin-amd64 .
59+
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go build -ldflags "${LDFLAGS}" -o dist/runner-manager-darwin-arm64 .
60+
61+
- name: Generate checksums
62+
run: |
63+
cd dist
64+
sha256sum * > checksums.txt
65+
66+
- name: Prepare config for image
67+
run: cp config.yaml.example config.yaml
68+
69+
- name: Set up Docker Buildx
70+
uses: docker/setup-buildx-action@v3
71+
72+
- name: Log in to Container Registry
73+
uses: docker/login-action@v3
74+
with:
75+
registry: ${{ env.REGISTRY }}
76+
username: ${{ github.actor }}
77+
password: ${{ secrets.GITHUB_TOKEN }}
78+
79+
- name: Extract metadata
80+
id: meta
81+
uses: docker/metadata-action@v5
82+
with:
83+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
84+
tags: |
85+
type=ref,event=tag
86+
type=semver,pattern={{version}}
87+
type=semver,pattern={{major}}.{{minor}}
88+
type=semver,pattern={{major}}
89+
type=raw,value=latest,enable=true
90+
91+
- name: Build and push Docker image
92+
uses: docker/build-push-action@v6
93+
with:
94+
context: .
95+
file: ./Dockerfile
96+
push: true
97+
tags: ${{ steps.meta.outputs.tags }}
98+
labels: ${{ steps.meta.outputs.labels }}
99+
build-args: |
100+
VERSION=${{ steps.version.outputs.tag }}
101+
cache-from: type=gha
102+
cache-to: type=gha,mode=max
103+
platforms: linux/amd64,linux/arm64
104+
105+
- name: Create Release
106+
uses: softprops/action-gh-release@v2
107+
with:
108+
files: dist/*
109+
name: Release ${{ steps.version.outputs.tag }}
110+
body: |
111+
## Changes
112+
113+
详见提交历史与变更说明。
114+
115+
## 使用方式
116+
117+
### Docker 镜像 (GHCR)
118+
119+
```bash
120+
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.tag }}
121+
```
122+
123+
### 二进制
124+
125+
- Linux AMD64: `runner-manager-linux-amd64`
126+
- Linux ARM64: `runner-manager-linux-arm64`
127+
- macOS AMD64: `runner-manager-darwin-amd64`
128+
- macOS ARM64: `runner-manager-darwin-arm64`
129+
130+
### 校验和
131+
132+
见 `checksums.txt` 中的 SHA256。
133+
draft: false
134+
prerelease: false

0 commit comments

Comments
 (0)