-
Notifications
You must be signed in to change notification settings - Fork 0
218 lines (186 loc) · 7.15 KB
/
Copy pathbuild.yml
File metadata and controls
218 lines (186 loc) · 7.15 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
name: Build and Release
on:
schedule:
- cron: '29 11 * * 1' # 每周一 11:29 UTC
workflow_dispatch: # 保留手动触发功能
push:
tags:
- 'v*' # 保留标签触发功能
jobs:
build:
name: Build and Release
runs-on: ubuntu-latest
permissions:
contents: write # 添加写入权限
steps:
- name: Checkout code
uses: actions/checkout@v4 # 更新到 v4
- name: Set up Go
uses: actions/setup-go@v5 # 更新到 v5
with:
go-version: '1.21' # 更新 Go 版本
- name: Generate version
id: version
run: |
if [[ $GITHUB_REF == refs/tags/* ]]; then
echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
else
# 生成基于周数和时间的版本号:v年份.周数.HHMM
CURRENT_TIME=$(date +'%H%M')
VERSION="v$(date +'%Y.%V').${CURRENT_TIME}"
# 检查标签是否存在
if git rev-parse "$VERSION" >/dev/null 2>&1; then
echo "Tag already exists, adding random suffix..."
RANDOM_SUFFIX=$(head /dev/urandom | tr -dc 'a-z0-9' | head -c 4)
VERSION="${VERSION}-${RANDOM_SUFFIX}"
fi
# 保存版本号到环境变量
echo "VERSION=$VERSION" >> $GITHUB_ENV
# 配置 git
git config --global user.name 'GitHub Actions'
git config --global user.email 'actions@github.com'
# 创建并推送标签
echo "Creating tag: $VERSION"
git tag "$VERSION"
git push origin "$VERSION"
fi
- name: Initialize Go modules
run: |
if [ ! -f "go.mod" ]; then
echo "Initializing new Go module..."
go mod init HashSum
else
echo "Go module already initialized"
fi
# 确保依赖是最新的
echo "Downloading and verifying dependencies..."
go mod tidy
if [ $? -ne 0 ]; then
echo "Failed to download dependencies"
exit 1
fi
# 验证依赖
go mod verify
if [ $? -ne 0 ]; then
echo "Module verification failed"
exit 1
fi
- name: Build for all platforms
run: |
platforms=("windows" "linux" "darwin")
archs=("amd64" "386" "arm64")
for platform in "${platforms[@]}"; do
for arch in "${archs[@]}"; do
if [ "$platform-$arch" != "darwin-386" ]; then
echo "Building for $platform/$arch..."
# 设置输出目录
output_dir="build/${platform}-${arch}-${VERSION}"
mkdir -p "$output_dir"
# 设置输出文件名
if [ "$platform" = "windows" ]; then
output_name="HashSum.exe"
else
output_name="HashSum"
fi
# 构建
GOOS=$platform GOARCH=$arch go build -o "$output_dir/$output_name" .
# 复制README和LICENSE
cp README.md "$output_dir/"
cp LICENSE "$output_dir/" 2>/dev/null || true
# 创建独立可执行文件的副本
standalone_dir="build/standalone"
mkdir -p "$standalone_dir"
if [ "$platform" = "windows" ]; then
cp "$output_dir/$output_name" "$standalone_dir/HashSum-${platform}-${arch}-${VERSION}.exe"
else
cp "$output_dir/$output_name" "$standalone_dir/HashSum-${platform}-${arch}-${VERSION}"
chmod +x "$standalone_dir/HashSum-${platform}-${arch}-${VERSION}"
fi
# 创建压缩包
if [ "$platform" = "windows" ]; then
(cd build && zip -r "HashSum-${platform}-${arch}-${VERSION}.zip" "${platform}-${arch}-${VERSION}")
else
(cd build && tar czf "HashSum-${platform}-${arch}-${VERSION}.tar.gz" "${platform}-${arch}-${VERSION}")
fi
fi
done
done
- name: Create Release
id: create_release
uses: softprops/action-gh-release@v1
with:
name: HashSum ${{ env.VERSION }}
draft: false
prerelease: false
generate_release_notes: true
tag_name: ${{ env.VERSION }}
body: |
HashSum 自动发布版本 ${{ env.VERSION }}
发布时间:${{ github.event.head_commit.timestamp }}
### 更新内容
- 自动周更版本
- 包含所有平台的构建版本
### 支持平台
- Windows (x86, x64, ARM64)
- Linux (x86, x64, ARM64)
- macOS (x64, ARM64)
### 下载说明
1. 完整版本(包含README和LICENSE):
- Windows: `HashSum-windows-{arch}-${VERSION}.zip`
- Linux/macOS: `HashSum-{platform}-{arch}-${VERSION}.tar.gz`
2. 独立可执行文件:
- Windows: `HashSum-windows-{arch}-${VERSION}.exe`
- Linux/macOS: `HashSum-{platform}-{arch}-${VERSION}`
files: |
build/*.zip
build/*.tar.gz
build/standalone/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload Build Artifacts
uses: actions/upload-artifact@v4
with:
name: builds
path: |
build/*.zip
build/*.tar.gz
build/standalone/
retention-days: 5
- name: Send Feishu Notification
if: always() # 无论构建成功还是失败都发送通知
run: |
# 获取当前时间
CURRENT_DATE=$(date '+%Y/%m/%d %H:%M')
# 设置通知状态
if [ ${{ job.status }} == "success" ]; then
STATUS="发布成功"
ICON="✅"
else
STATUS="发布失败"
ICON="❌"
fi
# 构建通知内容
NOTIFICATION_CONTENT="{
\"msg_type\": \"post\",
\"content\": {
\"post\": {
\"zh_cn\": {
\"title\": \"${ICON} HashSum发布通知\",
\"content\": [
[
{
\"tag\": \"text\",
\"text\": \"名称:程序 HashSum 已发布\\n发布日期: ${CURRENT_DATE}\\n发布结果:${STATUS}!\"
}
]
]
}
}
}
}"
# 发送通知到飞书
curl -X POST -H "Content-Type: application/json" \
-d "${NOTIFICATION_CONTENT}" \
${{ secrets.FEISHU_WEBHOOK_URL }}
env:
TZ: 'Asia/Shanghai' # 设置时区为中国时区