-
Notifications
You must be signed in to change notification settings - Fork 9
149 lines (131 loc) · 5.08 KB
/
build_all.yml
File metadata and controls
149 lines (131 loc) · 5.08 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
name: Publish Fabric Builds
on:
release:
types: [published]
jobs:
generate-branches:
name: Generate Branch Matrix
runs-on: ubuntu-latest
outputs:
branches: ${{ steps.set-branches.outputs.branches }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get Fabric-* branches from remote
id: set-branches
run: |
# 列出远程 Fabric-* 分支并构造 JSON 数组
readarray -t arr <<< "$(git ls-remote --heads origin 'Fabric-*' | awk '{print $2}' | sed 's|refs/heads/||')"
json="["
first=true
for b in "${arr[@]}"; do
if [ -n "$b" ]; then
if [ "$first" = true ]; then
first=false
else
json="${json},"
fi
# 转义双引号-理论上分支名不会包含特殊字符
json="${json}\"${b}\""
fi
done
json="${json},\"dev\"]"
echo "branches=$json" >> $GITHUB_OUTPUT
build:
name: Build & Publish
needs: generate-branches
runs-on: ubuntu-latest
environment:
name: MODRINTH_TOKEN
strategy:
fail-fast: false
matrix:
branch: ${{ fromJson(needs.generate-branches.outputs.branches) }}
steps:
- name: Checkout branch
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ matrix.branch }}
- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: 21
distribution: temurin
cache: gradle
- name: Cache Gradle
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: gradle-${{ runner.os }}-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}
restore-keys: gradle-${{ runner.os }}-
- name: Build
id: build-run
run: |
set -euo pipefail
echo "Processing branch: ${{ matrix.branch }}"
echo "Detected Gradle project"
chmod +x ./gradlew || true
./gradlew clean build
ARTIFACT=$(ls build/libs/*.jar 2>/dev/null | grep -v -- '-dev.jar' | grep -v -- '-sources.jar' | head -n1 || true)
if [ -z "$ARTIFACT" ]; then
echo "No artifact found (non -dev jar). Skipping branch."
exit 0
fi
echo "Built artifact: $ARTIFACT"
filename=$(basename "$ARTIFACT")
version="${filename%.jar}"
shorter_version=$(echo "$filename" | sed -E 's/^[Cc]oncerto-[Ff]abric-//; s/\.jar$//')
# 提取 mc 版本(形如 mc1.16.5 -> 1.16.5)
# 先尝试用 grep -P(Ubuntu runner 支持),fallback 用 sed
fabric_mc=""
if echo "$filename" | grep -oP 'mc\K[0-9]+\.[0-9]+(\.[0-9]+)?' >/dev/null 2>&1; then
fabric_mc=$(echo "$filename" | grep -oP 'mc\K[0-9]+\.[0-9]+(\.[0-9]+)?')
else
fabric_mc=$(echo "$filename" | sed -n 's/.*mc\([0-9]\+\.[0-9]\+\(\.[0-9]\+\)\?\).*/\1/p')
fi
echo "version=${version}" >> $GITHUB_OUTPUT
echo "shorter_version=${shorter_version}" >> $GITHUB_OUTPUT
echo "artifact=${ARTIFACT}" >> $GITHUB_OUTPUT
echo "mc_version=${fabric_mc}" >> $GITHUB_OUTPUT
# 根据 mc 版本生成 JSON 数组形式的 game_versions
case "$fabric_mc" in
1.16.5) gv='["1.16.x"]';;
1.17.1) gv='["1.17.x"]';;
1.18.2) gv='["1.18.x"]';;
1.19.4) gv='["1.19.x"]';;
1.20.1) gv='["1.20.1","1.20.2","1.20.3","1.20.4","1.20.5"]';;
1.20.6) gv='["1.20.6"]';;
1.21.1) gv='["1.21.1","1.21.2","1.21.3"]';;
1.21.4) gv='["1.21.4"]';;
1.21.5) gv='["1.21.5"]';;
1.21.6) gv='["1.21.6","1.21.7","1.21.8","1.21.9"]';;
1.21.10) gv='["1.21.10"]';;
"") echo "Could not parse mc version from filename: $filename"; gv='[]' ;;
*) echo "Unknown Fabric mc version: $fabric_mc" ; gv='[]' ;;
esac
echo "game_versions=${gv}" >> $GITHUB_OUTPUT
echo "Computed game_versions: ${gv}"
- name: Publish to Modrinth
if: steps.build-run.outputs.artifact != ''
uses: cloudnode-pro/modrinth-publish@v2
with:
token: ${{ secrets.MODRINTH_TOKEN }}
project: A0VZd1kW
name: ${{ steps.build-run.outputs.version }}
version: ${{ steps.build-run.outputs.shorter_version }}
changelog: ${{ github.event.release.body }}
loaders: '["fabric"]'
game-versions: ${{ steps.build-run.outputs.game_versions }}
files: ${{ steps.build-run.outputs.artifact }}
- name: Summary
if: steps.build-run.outputs.artifact != ''
run: |
echo "Branch: ${{ matrix.branch }}"
echo "Artifact: ${{ steps.build-run.outputs.artifact }}"
echo "Version: ${{ steps.build-run.outputs.version }}"
echo "Game versions: ${{ steps.build-run.outputs.game_versions }}"