Skip to content

Commit 2475197

Browse files
authored
Add files via upload
1 parent b5859fa commit 2475197

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

.github/workflows/release.yml

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
name: 自动构建与发布
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
7+
jobs:
8+
build-and-release:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: write
12+
packages: write
13+
actions: read
14+
15+
steps:
16+
- name: 检出代码
17+
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
21+
- name: 获取项目信息
22+
id: project-info
23+
run: |
24+
# 从pom.xml提取项目信息
25+
PROJECT_NAME=$(mvn help:evaluate -Dexpression=project.name -q -DforceStdout | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
26+
if [ "$PROJECT_NAME" = "null" ]; then
27+
PROJECT_NAME=$(mvn help:evaluate -Dexpression=project.artifactId -q -DforceStdout)
28+
fi
29+
30+
VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout | tr -d '\n')
31+
32+
echo "name=${PROJECT_NAME}" >> $GITHUB_OUTPUT
33+
echo "version=${VERSION}" >> $GITHUB_OUTPUT
34+
35+
- name: 设置Java环境
36+
uses: actions/setup-java@v3
37+
with:
38+
java-version: '21'
39+
distribution: 'temurin'
40+
cache: maven
41+
42+
- name: Maven构建项目
43+
run: mvn -B clean package
44+
45+
- name: 验证构建结果
46+
run: |
47+
if [ ! -d "target" ]; then
48+
echo "::error::构建失败,未生成target目录"
49+
exit 1
50+
fi
51+
echo "=== 构建产物 ==="
52+
ls -lh target/
53+
54+
- name: 获取提交信息
55+
id: get-commit
56+
run: |
57+
COMMIT_MSG=$(git log -1 --pretty=format:%B)
58+
echo "commit_msg<<EOF" >> $GITHUB_OUTPUT
59+
echo "$COMMIT_MSG" >> $GITHUB_OUTPUT
60+
echo "EOF" >> $GITHUB_OUTPUT
61+
62+
- name: 计算文件哈希
63+
id: file-hashes
64+
run: |
65+
set -euo pipefail
66+
67+
echo "## 📦 文件校验哈希" > hashes.md
68+
echo '```' >> hashes.md
69+
70+
found_files=false
71+
for file in target/*.jar; do
72+
if [[ -f "$file" ]]; then
73+
hash=$(sha256sum "$file" | awk '{print $1}')
74+
filename=$(basename "$file")
75+
echo "${filename}: ${hash}" >> hashes.md
76+
found_files=true
77+
fi
78+
done
79+
80+
if [[ "$found_files" == false ]]; then
81+
echo "::warning::未找到可校验文件"
82+
echo "hash_content=未生成校验信息" >> $GITHUB_OUTPUT
83+
else
84+
echo '```' >> hashes.md
85+
echo "hash_content<<EOF" >> $GITHUB_OUTPUT
86+
cat hashes.md >> $GITHUB_OUTPUT
87+
echo "EOF" >> $GITHUB_OUTPUT
88+
fi
89+
shell: bash
90+
91+
- name: 记录构建时间
92+
id: get-date
93+
run: |
94+
echo "date=$(date +'%Y年%m月%d日 %H时%M分%S秒')" >> $GITHUB_OUTPUT
95+
96+
- name: 创建版本发布
97+
uses: softprops/action-gh-release@v1
98+
with:
99+
tag_name: ${{ steps.project-info.outputs.version }}
100+
name: ${{ steps.project-info.outputs.name }}-v${{ steps.project-info.outputs.version }}
101+
body: |
102+
## 📝 提交信息
103+
```
104+
${{ steps.get-commit.outputs.commit_msg }}
105+
```
106+
107+
## 🔧 构建详情
108+
- 项目名称: ${{ steps.project-info.outputs.name }}
109+
- 版本号: ${{ steps.project-info.outputs.version }}
110+
- 提交哈希: [${{ github.sha }}](${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }})
111+
- 构建时间: ${{ steps.get-date.outputs.date }}
112+
113+
## 🔍 文件校验
114+
${{ steps.file-hashes.outputs.hash_content }}
115+
116+
> 本发布由 GitHub Actions 自动生成
117+
files: |
118+
target/*.jar
119+
draft: false
120+
prerelease: false
121+
env:
122+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)