Skip to content

Commit 4928e52

Browse files
committed
ci(release): 优化发布流程并简化构建产物
1 parent 76d651d commit 4928e52

File tree

1 file changed

+82
-108
lines changed

1 file changed

+82
-108
lines changed

.github/workflows/release.yml

Lines changed: 82 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
- name: Set up Go
2828
uses: actions/setup-go@v5
2929
with:
30-
go-version: "stable"
30+
go-version: stable
3131
cache: true
3232

3333
- name: Cache Go modules
@@ -38,169 +38,143 @@ jobs:
3838
restore-keys: |
3939
${{ runner.os }}-go-
4040
41-
- name: Download dependencies
42-
run: go mod download
43-
44-
- name: Verify dependencies
45-
run: go mod verify
46-
47-
- name: Run tests
48-
run: go test -v ./...
49-
5041
- name: Install UPX
5142
run: |
5243
sudo apt-get update
5344
sudo apt-get install -y upx
5445
46+
# 取得版本号:
47+
# - workflow_dispatch:若未填写则自动 vX.Y.(Z+1),并创建/推送该 tag
48+
# - push tag:使用该 tag
5549
- name: Get version
5650
id: version
5751
run: |
52+
set -e
53+
git fetch --tags --force --prune
54+
5855
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
59-
echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
56+
INPUT_VER="${{ github.event.inputs.version }}"
57+
if [ -n "$INPUT_VER" ]; then
58+
NEW_VER="$INPUT_VER"
59+
else
60+
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
61+
ver="${LAST_TAG#v}"
62+
IFS='.' read -r MAJ MIN PAT <<< "$ver"
63+
: "${MAJ:=0}" ; : "${MIN:=0}" ; : "${PAT:=0}"
64+
NEW_VER="v${MAJ}.${MIN}.$((PAT+1))"
65+
fi
66+
echo "VERSION=$NEW_VER" >> $GITHUB_OUTPUT
6067
else
6168
echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
6269
fi
6370
71+
- name: Create and push tag (manual dispatch only)
72+
if: github.event_name == 'workflow_dispatch'
73+
run: |
74+
set -e
75+
TAG="${{ steps.version.outputs.VERSION }}"
76+
echo "Ensure tag exists: $TAG"
77+
if ! git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then
78+
git config user.name "github-actions[bot]"
79+
git config user.email "github-actions[bot]@users.noreply.github.com"
80+
git tag -a "$TAG" -m "Release $TAG"
81+
fi
82+
git push origin "$TAG"
83+
84+
- name: Download dependencies
85+
run: go mod download
86+
87+
- name: Verify dependencies
88+
run: go mod verify
89+
90+
- name: Run tests
91+
run: go test -v ./...
92+
6493
- name: Build for multiple platforms
94+
env:
95+
CGO_ENABLED: 0
6596
run: |
66-
# 创建发布目录
6797
mkdir -p release
98+
LDFLAGS="-s -w -X 'main.version=${{ steps.version.outputs.VERSION }}'"
6899
69-
# 构建 Mac 版本
70-
echo "构建 Mac 版本..."
71-
GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o release/cert-deploy-mac main.go
72-
GOOS=darwin GOARCH=arm64 go build -ldflags="-s -w" -trimpath -o release/cert-deploy-mac-arm64 main.go
100+
echo "Build macOS..."
101+
GOOS=darwin GOARCH=amd64 go build -ldflags="${LDFLAGS}" -trimpath -o release/cert-deploy-mac main.go
102+
GOOS=darwin GOARCH=arm64 go build -ldflags="${LDFLAGS}" -trimpath -o release/cert-deploy-mac-arm64 main.go
73103
74-
# 构建 Linux 版本
75-
echo "构建 Linux 版本..."
76-
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o release/cert-deploy-linux main.go
77-
GOOS=linux GOARCH=arm64 go build -ldflags="-s -w" -trimpath -o release/cert-deploy-linux-arm64 main.go
104+
echo "Build Linux..."
105+
GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" -trimpath -o release/cert-deploy-linux main.go
106+
GOOS=linux GOARCH=arm64 go build -ldflags="${LDFLAGS}" -trimpath -o release/cert-deploy-linux-arm64 main.go
78107
79-
# 构建 Windows 版本
80-
echo "构建 Windows 版本..."
81-
GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -trimpath -o release/cert-deploy-windows.exe main.go
82-
GOOS=windows GOARCH=arm64 go build -ldflags="-s -w" -trimpath -o release/cert-deploy-windows-arm64.exe main.go
108+
echo "Build Windows..."
109+
GOOS=windows GOARCH=amd64 go build -ldflags="${LDFLAGS}" -trimpath -o release/cert-deploy-windows.exe main.go
110+
GOOS=windows GOARCH=arm64 go build -ldflags="${LDFLAGS}" -trimpath -o release/cert-deploy-windows-arm64.exe main.go
83111
84112
- name: Show build info
85113
run: |
86-
echo "=== 构建信息 ==="
87-
echo "版本: ${{ steps.version.outputs.VERSION }}"
88-
echo "Go 版本: $(go version)"
89-
echo "UPX 版本: $(upx --version)"
90-
echo "构建文件:"
91-
ls -lh release/cert-deploy-*
114+
echo "=== Build Info ==="
115+
echo "Version: ${{ steps.version.outputs.VERSION }}"
116+
echo "Go: $(go version)"
117+
echo "UPX: $(upx --version)"
118+
echo "Files:"
119+
ls -lh release/
92120
93121
- name: Compress Linux binaries with UPX
94122
run: |
95-
echo "开始压缩 Linux 版本..."
123+
echo "UPX Linux binaries..."
96124
for f in release/cert-deploy-linux release/cert-deploy-linux-arm64; do
97125
if [ -f "$f" ]; then
98-
echo "UPX 压缩: $f"
99-
upx --best "$f" || echo "⚠️ 压缩失败(已忽略): $f"
126+
echo "UPX -> $f"
127+
upx --best "$f" || echo "⚠️ ignore UPX failure: $f"
100128
fi
101129
done
102-
echo "✅ Linux 压缩完成"
103-
echo "压缩后文件大小:"
130+
echo "Done."
104131
ls -lh release/cert-deploy-linux*
105132
106-
- name: Create release packages
107-
run: |
108-
# 创建各平台的发布包
109-
cd release
110-
111-
# Mac 包
112-
tar -czf cert-deploy-mac-${{ steps.version.outputs.VERSION }}.tar.gz cert-deploy-mac cert-deploy-mac-arm64
113-
echo "Mac 包大小: $(du -h cert-deploy-mac-${{ steps.version.outputs.VERSION }}.tar.gz)"
114-
115-
# Linux 包
116-
tar -czf cert-deploy-linux-${{ steps.version.outputs.VERSION }}.tar.gz cert-deploy-linux cert-deploy-linux-arm64
117-
echo "Linux 包大小: $(du -h cert-deploy-linux-${{ steps.version.outputs.VERSION }}.tar.gz)"
118-
119-
# Windows 包
120-
zip -r cert-deploy-windows-${{ steps.version.outputs.VERSION }}.zip cert-deploy-windows*.exe
121-
echo "Windows 包大小: $(du -h cert-deploy-windows-${{ steps.version.outputs.VERSION }}.zip)"
122-
123-
# 创建通用包
124-
tar -czf cert-deploy-${{ steps.version.outputs.VERSION }}.tar.gz cert-deploy-*
125-
echo "通用包大小: $(du -h cert-deploy-${{ steps.version.outputs.VERSION }}.tar.gz)"
126-
133+
# 不再打包 zip/tar.gz;直接上传可执行文件
127134
- name: Generate checksums
128135
run: |
129136
cd release
130137
sha256sum cert-deploy-* > checksums.txt
131-
echo "生成校验和文件"
138+
echo "checksums generated:"
139+
cat checksums.txt
132140
133-
- name: Upload release artifacts
141+
- name: Upload release artifacts (for job debug/download)
134142
uses: actions/upload-artifact@v4
135143
with:
136144
name: release-files
137145
path: release/
138146
retention-days: 30
139147

140-
- name: Create GitHub Release
148+
- name: Create GitHub Release (push trigger)
141149
uses: softprops/action-gh-release@v2
142150
if: github.event_name == 'push'
143151
with:
152+
generate_release_notes: true
144153
files: |
145-
release/cert-deploy-mac-${{ steps.version.outputs.VERSION }}.tar.gz
146-
release/cert-deploy-linux-${{ steps.version.outputs.VERSION }}.tar.gz
147-
release/cert-deploy-windows-${{ steps.version.outputs.VERSION }}.zip
148-
release/cert-deploy-${{ steps.version.outputs.VERSION }}.tar.gz
154+
release/cert-deploy-mac
155+
release/cert-deploy-mac-arm64
156+
release/cert-deploy-linux
157+
release/cert-deploy-linux-arm64
158+
release/cert-deploy-windows.exe
159+
release/cert-deploy-windows-arm64.exe
149160
release/checksums.txt
150-
body: |
151-
## 证书部署工具 ${{ steps.version.outputs.VERSION }}
152-
153-
### 下载说明
154-
155-
- **Mac**: `cert-deploy-mac-${{ steps.version.outputs.VERSION }}.tar.gz`
156-
- **Linux**: `cert-deploy-linux-${{ steps.version.outputs.VERSION }}.tar.gz`
157-
- **Windows**: `cert-deploy-windows-${{ steps.version.outputs.VERSION }}.zip`
158-
- **通用包**: `cert-deploy-${{ steps.version.outputs.VERSION }}.tar.gz`
159-
160-
### 安装使用
161-
162-
1. 下载对应平台的压缩包
163-
2. 解压到目标目录
164-
3. 配置 `config.yaml` 文件
165-
4. 运行 `./cert-deploy daemon` 启动服务
166-
167-
### 校验文件
168-
169-
请使用 `checksums.txt` 文件验证下载的完整性。
170161
draft: false
171162
prerelease: false
172163

173-
- name: Create Manual Release
164+
- name: Create GitHub Release (manual trigger)
174165
uses: softprops/action-gh-release@v2
175166
if: github.event_name == 'workflow_dispatch'
176167
with:
177168
tag_name: ${{ steps.version.outputs.VERSION }}
178169
name: Release ${{ steps.version.outputs.VERSION }}
170+
generate_release_notes: true
179171
files: |
180-
release/cert-deploy-mac-${{ steps.version.outputs.VERSION }}.tar.gz
181-
release/cert-deploy-linux-${{ steps.version.outputs.VERSION }}.tar.gz
182-
release/cert-deploy-windows-${{ steps.version.outputs.VERSION }}.zip
183-
release/cert-deploy-${{ steps.version.outputs.VERSION }}.tar.gz
172+
release/cert-deploy-mac
173+
release/cert-deploy-mac-arm64
174+
release/cert-deploy-linux
175+
release/cert-deploy-linux-arm64
176+
release/cert-deploy-windows.exe
177+
release/cert-deploy-windows-arm64.exe
184178
release/checksums.txt
185-
body: |
186-
## 证书部署工具 ${{ steps.version.outputs.VERSION }}
187-
188-
### 下载说明
189-
190-
- **Mac**: `cert-deploy-mac-${{ steps.version.outputs.VERSION }}.tar.gz`
191-
- **Linux**: `cert-deploy-linux-${{ steps.version.outputs.VERSION }}.tar.gz`
192-
- **Windows**: `cert-deploy-windows-${{ steps.version.outputs.VERSION }}.zip`
193-
- **通用包**: `cert-deploy-${{ steps.version.outputs.VERSION }}.tar.gz`
194-
195-
### 安装使用
196-
197-
1. 下载对应平台的压缩包
198-
2. 解压到目标目录
199-
3. 配置 `config.yaml` 文件
200-
4. 运行 `./cert-deploy daemon` 启动服务
201-
202-
### 校验文件
203-
204-
请使用 `checksums.txt` 文件验证下载的完整性。
205179
draft: false
206180
prerelease: false

0 commit comments

Comments
 (0)