1+ name : Build and Test
2+
3+ on :
4+ push :
5+ branches : [ main, dev ]
6+ paths :
7+ - ' .github/workflows/build.yml'
8+ - ' src/**'
9+ - ' build.gradle.kts'
10+ tags :
11+ - ' v*' # 匹配 v 开头的标签
12+ pull_request :
13+ branches : [ main ]
14+ paths :
15+ - ' .github/workflows/build.yml'
16+ - ' src/**'
17+ - ' build.gradle.kts'
18+ tags :
19+ - ' v*' # 匹配 v 开头的标签
20+
21+ jobs :
22+ build :
23+ runs-on : ubuntu-latest
24+
25+ steps :
26+ - uses : actions/checkout@v4
27+
28+ - name : Set up JDK 21
29+ uses : actions/setup-java@v4
30+ with :
31+ java-version : ' 21'
32+ distribution : ' temurin'
33+
34+ - name : Setup Gradle
35+ uses : gradle/actions/setup-gradle@v3
36+
37+ - name : Build with Gradle
38+ run : ./gradlew build
39+
40+ - name : Run tests
41+ run : ./gradlew test
42+
43+ - name : Create shadow JAR
44+ run : ./gradlew shadowJar
45+
46+ - name : Upload JAR artifact
47+ uses : actions/upload-artifact@v4
48+ with :
49+ name : huhobot-server
50+ path : build/libs/huhobotServer-all.jar
51+ retention-days : 1 # 仅保留1天临时文件
52+
53+ release :
54+ name : Create Release
55+ needs : build
56+ if : startsWith(github.ref, 'refs/tags/')
57+ runs-on : ubuntu-latest
58+ # 新增权限配置
59+ permissions :
60+ contents : write # 必须的权限
61+ actions : read
62+ packages : write
63+
64+ steps :
65+ - name : Checkout code
66+ uses : actions/checkout@v4
67+ with :
68+ fetch-depth : 0
69+
70+ - name : Extract release info
71+ id : changelog
72+ run : |
73+ TAG_NAME=${GITHUB_REF#refs/tags/}
74+
75+ # 修复:使用正确的标签格式匹配
76+ VERSION=${TAG_NAME#v} # 去除v前缀(如果CHANGELOG使用纯版本号)
77+
78+ CHANGELOG_CONTENT=$(awk -v version="[v$VERSION]" '
79+ BEGIN {RS="## "; FS="\n"}
80+ $1 ~ version {
81+ sub(/\[.*\] - .*\n/, "")
82+ gsub(/`/, "\\`")
83+ gsub(/"/, "\\\"")
84+ print
85+ exit
86+ }
87+ ' CHANGELOG.md)
88+
89+ EOF_MARKER=$(openssl rand -base64 12)
90+ echo "body<<$EOF_MARKER" >> $GITHUB_OUTPUT
91+ echo "$CHANGELOG_CONTENT" >> $GITHUB_OUTPUT
92+ echo "$EOF_MARKER" >> $GITHUB_OUTPUT
93+
94+ # 添加标签名输出
95+ echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT
96+
97+ - name : Download Artifacts
98+ uses : actions/download-artifact@v4
99+ with :
100+ name : huhobot-server
101+ path : artifacts
102+
103+ - name : Setup workspace
104+ run : |
105+ VERSION=${GITHUB_REF_NAME#v} # 去掉v前缀
106+ echo '{"latest":"$VERSION"}' > artifacts/latest.json
107+
108+ - name : Upload to R2
109+ uses : ryand56/r2-upload-action@latest
110+ with :
111+ r2-account-id : ${{ secrets.R2_ACCOUNT_ID }}
112+ r2-access-key-id : ${{ secrets.R2_ACCESS_KEY_ID }}
113+ r2-secret-access-key : ${{ secrets.R2_SECRET_ACCESS_KEY }}
114+ r2-bucket : ${{ secrets.R2_BUCKET }}
115+ source-dir : artifacts
116+ destination-dir : mainServer
117+
118+ - name : Get timestamp
119+ id : get-time
120+ run : echo "TIME=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
121+
122+ - name : Create Release
123+ uses : softprops/action-gh-release@v1 # 改用更可靠的 Action
124+ with :
125+ tag_name : ${{ steps.changelog.outputs.tag_name }}
126+ name : HuHoBot MainServer ${{ steps.changelog.outputs.tag_name }}
127+ body : |
128+ ${{ steps.changelog.outputs.body }}
129+
130+ ### 构建信息
131+ - 构建时间: ${{ steps.get-time.outputs.TIME }}
132+ - 提交哈希: [${{ github.sha }}](${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }})
133+ files : |
134+ artifacts/*.jar
0 commit comments