-
Notifications
You must be signed in to change notification settings - Fork 0
202 lines (176 loc) · 7.55 KB
/
Copy pathbuild.yml
File metadata and controls
202 lines (176 loc) · 7.55 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
# FILE PATH: .github/workflows/build.yml
name: ClientCore CI/CD
# Trigger the workflow on push to any branch
on:
push:
branches:
- '**'
pull_request:
branches:
- '**'
workflow_dispatch:
# Define all global environment variables
env:
# Base Webhook URL (NO ?thread_id AT THE END)
WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
THREAD_ID: ${{ secrets.DISCORD_WEBHOOK_THREAD_ID }}
# Gradle Settings
GRADLE_OPTS: "-Dorg.gradle.daemon=false"
# Bot Identity & Assets
ICON_URL: "https://gitlab.com/uploads/-/system/group/avatar/121690756/SinceRPG.png?width=48"
THUMBNAIL_URL: "https://gitlab.com/uploads/-/system/group/avatar/121690756/SinceRPG.png?width=48"
BOT_USERNAME: "ClientCore Build"
# Embed Colors (Decimal format)
COLOR_PENDING: "16766720" # Yellow
COLOR_SUCCESS: "5763719" # Green
COLOR_FAIL: "15548997" # Red
# Default Texts
NO_CHANGELOG_TEXT: "No specific changelog provided."
FAIL_DESC_TEXT: "A syntax or compilation error occurred. Please check the GitHub Actions Console."
jobs:
build_and_notify:
name: Build & Discord Notify
runs-on: ubuntu-latest
steps:
# --- STEP 0: PREPARE ENVIRONMENT ---
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Required to fetch git log history for commits
- name: Setup Java JDK 25
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '25'
cache: 'gradle' # Automatically handles Gradle caching
# --- STEP 1: NOTIFY BUILD START ---
- name: Notify Start to Discord
if: env.WEBHOOK_URL != ''
id: notify_start
run: |
COMMIT_MSG=$(git log -1 --pretty=%B)
AUTHOR=$(git log -1 --pretty=%an)
HASH=$(git log -1 --pretty=%h)
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
if [ -f "./gradlew" ]; then GRADLE_CMD="./gradlew"; else GRADLE_CMD="gradle"; fi
VERSION=$($GRADLE_CMD properties -q | grep "^version:" | awk '{print $2}' || echo "Unknown")
# Handle empty changelog
[ -z "$(echo "$COMMIT_MSG" | tr -d '[:space:]')" ] && COMMIT_MSG="$NO_CHANGELOG_TEXT"
# CREATE PENDING MESSAGE
jq -n \
--arg author "$AUTHOR triggered a build for ClientCore" \
--arg desc "$COMMIT_MSG" \
--arg footer "Version $VERSION - $HASH" \
--arg timestamp "$TIMESTAMP" \
--arg icon "$ICON_URL" \
--arg thumb "$THUMBNAIL_URL" \
--arg bot_name "$BOT_USERNAME" \
--argjson color "$COLOR_PENDING" \
'{
username: $bot_name,
avatar_url: $icon,
embeds: [{
color: $color,
author: { name: $author, icon_url: $icon },
title: "Building...",
description: $desc,
thumbnail: { url: $thumb },
footer: { text: $footer },
timestamp: $timestamp
}]
}' > build_start.json
# Send message and extract ID, save to GITHUB_ENV
RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -d @build_start.json "${WEBHOOK_URL}?thread_id=${THREAD_ID}&wait=true")
MSG_ID=$(echo $RESPONSE | jq -r '.id')
if [ "$MSG_ID" != "null" ]; then echo "MSG_ID=$MSG_ID" >> $GITHUB_ENV; fi
# --- STEP 2: ACTUAL BUILD PROCESS ---
- name: Build with Gradle
run: |
if [ -f "./gradlew" ]; then
chmod +x ./gradlew
./gradlew clean build
else
gradle clean build
fi
# --- STEP 3A: NOTIFY BUILD SUCCESS ---
- name: Notify Success & Upload JAR to Discord
if: success() && env.WEBHOOK_URL != ''
run: |
COMMIT_MSG=$(git log -1 --pretty=%B)
AUTHOR=$(git log -1 --pretty=%an)
HASH=$(git log -1 --pretty=%h)
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
if [ -f "./gradlew" ]; then GRADLE_CMD="./gradlew"; else GRADLE_CMD="gradle"; fi
VERSION=$($GRADLE_CMD properties -q | grep "^version:" | awk '{print $2}' || echo "Unknown")
[ -z "$(echo "$COMMIT_MSG" | tr -d '[:space:]')" ] && COMMIT_MSG="$NO_CHANGELOG_TEXT"
# EDIT MESSAGE ON SUCCESS
if [ -n "$MSG_ID" ]; then
jq -n \
--arg author "$AUTHOR pushed an update for ClientCore" \
--arg desc "$COMMIT_MSG" \
--arg footer "Version $VERSION - $HASH" \
--arg timestamp "$TIMESTAMP" \
--arg icon "$ICON_URL" \
--arg thumb "$THUMBNAIL_URL" \
--argjson color "$COLOR_SUCCESS" \
'{
embeds: [{
color: $color,
author: { name: $author, icon_url: $icon },
title: "Build Successful",
description: $desc,
thumbnail: { url: $thumb },
footer: { text: $footer },
timestamp: $timestamp
}]
}' > build_success.json
JAR_FILE=$(find build/libs -maxdepth 1 -name "*-all.jar" ! -name "*-sources.jar" ! -name "*-javadoc.jar" | head -n 1)
if [ -n "$JAR_FILE" ] && [ -f "$JAR_FILE" ]; then
COMMIT_COUNT=$(git rev-list --count HEAD)
mkdir -p discord_upload
RENAMED_JAR="discord_upload/ClientCore-build-${COMMIT_COUNT}.jar"
cp "$JAR_FILE" "$RENAMED_JAR"
curl -s -X PATCH -F "payload_json=<build_success.json;type=application/json" -F "file=@$RENAMED_JAR" "${WEBHOOK_URL}/messages/${MSG_ID}?thread_id=${THREAD_ID}"
else
curl -s -X PATCH -H "Content-Type: application/json" -d @build_success.json "${WEBHOOK_URL}/messages/${MSG_ID}?thread_id=${THREAD_ID}"
fi
fi
# --- STEP 3B: NOTIFY BUILD FAILURE ---
- name: Notify Failure to Discord
if: failure() && env.WEBHOOK_URL != ''
run: |
COMMIT_MSG=$(git log -1 --pretty=%B)
HASH=$(git log -1 --pretty=%h)
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
PIPELINE_URL="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
[ -z "$(echo "$COMMIT_MSG" | tr -d '[:space:]')" ] && COMMIT_MSG="$FAIL_DESC_TEXT"
# EDIT MESSAGE ON FAILURE
if [ -n "$MSG_ID" ]; then
jq -n \
--arg url "$PIPELINE_URL" \
--arg footer "Build Failed - $HASH" \
--arg timestamp "$TIMESTAMP" \
--arg icon "$ICON_URL" \
--arg desc "$COMMIT_MSG" \
--argjson color "$COLOR_FAIL" \
'{
embeds: [{
color: $color,
author: { name: "Build Failed", icon_url: $icon },
title: "Click here to view Pipeline Logs",
url: $url,
description: $desc,
footer: { text: $footer },
timestamp: $timestamp
}]
}' > build_fail.json
curl -s -X PATCH -H "Content-Type: application/json" -d @build_fail.json "${WEBHOOK_URL}/messages/${MSG_ID}?thread_id=${THREAD_ID}"
fi
# --- STEP 4: SAVE ARTIFACTS TO GITHUB ---
- name: Upload JAR Artifacts to GitHub
if: always()
uses: actions/upload-artifact@v4
with:
name: ClientCore-Build
path: build/libs/*.jar
retention-days: 7