-
Notifications
You must be signed in to change notification settings - Fork 0
175 lines (168 loc) · 5.85 KB
/
Copy pathrelease.yml
File metadata and controls
175 lines (168 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
name: Release
# 触发方式(满足其一即可):
# 1. 推送形如 v1.0.0 的标签:git tag v1.0.0 && git push github v1.0.0
# 2. 推送 master 分支且提交信息包含版本号(如 "release:v1.0.0")
on:
push:
branches:
- master
tags:
- 'v[0-9]*'
permissions:
contents: write
packages: write
jobs:
# 解析版本号:标签优先,否则从提交信息中匹配 vX.Y.Z
resolve-version:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.detect.outputs.version }}
steps:
- uses: actions/checkout@v4
- name: Detect version
id: detect
run: |
VERSION=""
if [[ "${{ github.ref_type }}" == "tag" ]]; then
VERSION="${{ github.ref_name }}"
else
VERSION=$(git log -1 --format=%s | grep -oE 'v[0-9]+\.[0-9]+\.[0-9]+' | head -n1 || true)
fi
if [[ -n "$VERSION" ]] && [[ "$VERSION" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
echo "检测到版本号:$VERSION"
else
echo "未检测到版本号,跳过发布"
fi
# 构建前端产物(web/dist),供各平台编译复用
frontend:
needs: resolve-version
if: needs.resolve-version.outputs.version != ''
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
cache-dependency-path: web/package-lock.json
- name: Install dependencies
run: npm ci
working-directory: web
- name: Build frontend
run: npm run build
working-directory: web
- uses: actions/upload-artifact@v4
with:
name: frontend-dist
path: web/dist
# 交叉编译主流系统(Windows / Linux / macOS,amd64 + arm64)
build:
needs: [resolve-version, frontend]
if: needs.resolve-version.outputs.version != ''
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- { os: windows, arch: amd64, ext: .exe }
- { os: windows, arch: arm64, ext: .exe }
- { os: linux, arch: amd64, ext: '' }
- { os: linux, arch: arm64, ext: '' }
- { os: darwin, arch: amd64, ext: '' }
- { os: darwin, arch: arm64, ext: '' }
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Download frontend dist
uses: actions/download-artifact@v4
with:
name: frontend-dist
path: web/dist
- name: Build binary
env:
CGO_ENABLED: '0'
GOOS: ${{ matrix.os }}
GOARCH: ${{ matrix.arch }}
run: go build -trimpath -ldflags="-s -w" -o oauthgo${{ matrix.ext }} .
- name: Package (binary + web/dist)
run: |
mkdir -p _pkg/web release
cp oauthgo${{ matrix.ext }} _pkg/
cp -r web/dist _pkg/web/dist
if [[ "${{ matrix.os }}" == "windows" ]]; then
sudo apt-get update -qq >/dev/null 2>&1 && sudo apt-get install -y -qq zip >/dev/null 2>&1
(cd _pkg && zip -qr ../release/oauthgo-${{ matrix.os }}-${{ matrix.arch }}.zip oauthgo.exe web)
else
tar -czf release/oauthgo-${{ matrix.os }}-${{ matrix.arch }}.tar.gz -C _pkg oauthgo web
fi
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: oauthgo-${{ matrix.os }}-${{ matrix.arch }}
path: release/*
# 创建 GitHub Release 并附上所有平台的编译产物
release:
needs: [build, resolve-version]
if: needs.resolve-version.outputs.version != ''
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
pattern: oauthgo-*
merge-multiple: true
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ needs.resolve-version.outputs.version }}
target_commitish: ${{ github.sha }}
files: oauthgo-*
generate_release_notes: true
# 构建多架构 Docker 镜像,发布到 GitHub Container Registry 与 Docker Hub
docker:
needs: resolve-version
if: needs.resolve-version.outputs.version != ''
runs-on: ubuntu-latest
env:
DH_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DH_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
steps:
- uses: actions/checkout@v4
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to Docker Hub
if: env.DH_USERNAME != '' && env.DH_TOKEN != ''
uses: docker/login-action@v3
with:
username: ${{ env.DH_USERNAME }}
password: ${{ env.DH_TOKEN }}
- name: Build and push (GHCR)
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: |
ghcr.io/${{ github.repository_owner }}/oauthgo:${{ needs.resolve-version.outputs.version }}
ghcr.io/${{ github.repository_owner }}/oauthgo:latest
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Build and push (Docker Hub)
if: env.DH_USERNAME != '' && env.DH_TOKEN != ''
uses: docker/build-push-action@v6
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
tags: |
${{ env.DH_USERNAME }}/oauthgo:${{ needs.resolve-version.outputs.version }}
${{ env.DH_USERNAME }}/oauthgo:latest
cache-from: type=gha