This repository has been archived by the owner on Jan 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
179 lines (154 loc) · 7.46 KB
/
blank.yml
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
name: CI
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
workflow_dispatch:
schedule:
- cron: '0 */5 * * *' # Run every 5 hours
jobs:
build:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 1 # Fetch all history for all branches and tags
fetch-tags: false
persist-credentials: true
token: ${{ secrets.FINE_GRAINED_PAT }} # Use fine-grained PAT for checkout
- uses: graalvm/setup-graalvm@v1
with:
java-version: '22'
distribution: 'graalvm'
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Add Cloudflare repository
run: |
sudo mkdir -p --mode=0755 /usr/share/keyrings
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared jammy main' | sudo tee /etc/apt/sources.list.d/cloudflared.list
sudo apt-get update
- name: Cache apt packages
uses: awalsh128/cache-apt-pkgs-action@latest
with:
packages: cloudflared
version: 1.0
- name: Debug - List directory contents
run: ls -la
- name: Run Spigot and cloudflared with auto-restart and workflow recreation
if: "!contains(github.event.head_commit.message, 'Auto commit every 30 seconds [no ci]')"
continue-on-error: true
env:
FINE_GRAINED_PAT: ${{ secrets.FINE_GRAINED_PAT }}
REPO: ${{ github.repository }}
CF_TUNNEL_CERT: ${{secrets.CF_CERT}}
CF_TUNNEL_UUID: ${{secrets.CF_TUNNEL_UUID_1}}
CF_TUNNEL_UUID_CONTENT: ${{secrets.CF_TUNNEL_UUID_CONTENT}}
run: |
cd $GITHUB_WORKSPACE
mkdir ~/.cloudflared
echo "${CF_TUNNEL_CERT}" > ~/.cloudflared/cert.pem
touch tunnel-config.yml
echo "${CF_TUNNEL_UUID_CONTENT}" > credentials.json
echo "url: tcp://localhost:25565" >> tunnel-config.yml
echo "tunnel: ${CF_TUNNEL_UUID}" >> tunnel-config.yml
echo "credentials-file: credentials.json" >> tunnel-config.yml
start_spigot() {
echo "Starting Spigot..."
java -XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true -Xms16G -Xmx16G -jar spigot.jar nogui 2>&1 | sed '/login:/s/.*/<REDACTED>/' &
SPIGOT_PID=$!
echo "Spigot started with PID $SPIGOT_PID"
}
start_cloudflared() {
cloudflared tunnel --config tunnel-config.yml run ${CF_TUNNEL_UUID} &
CLOUDFLARED_PID=$!
echo "Cloudflare Tunnel started with PID $CLOUDFLARED_PID"
}
commit_and_push() {
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add -A # Stage all changes
git reset -- tunnel-config.yml
git reset -- credentials.json
git reset -- logs/
if ! git commit -m "Auto commit every 30 seconds [no ci]"; then
echo "No changes to commit or commit failed"
return 1
fi
for i in {1..3}; do
if git push https://x-access-token:${FINE_GRAINED_PAT}@github.com/${GITHUB_REPOSITORY}.git HEAD:main --force; then
echo "Push successful on attempt $i"
return 0
else
echo "Push failed on attempt $i, retrying..."
git pull --rebase
fi
done
echo "Failed to push after 3 attempts"
return 1
}
delete_auto_commit_workflows() {
workflow_ids=$(curl -s -H "Authorization: token $FINE_GRAINED_PAT" \
"https://api.github.com/repos/${REPO}/actions/runs" | \
jq '.workflow_runs[] | select(.head_commit.message | contains("Auto commit every 30 seconds")) | .id')
for id in $workflow_ids; do
echo "Deleting workflow run $id"
curl -s -X DELETE -H "Authorization: token $FINE_GRAINED_PAT" \
"https://api.github.com/repos/${REPO}/actions/runs/$id"
done
}
create_new_workflow_run() {
curl -X POST -H "Authorization: token $FINE_GRAINED_PAT" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${REPO}/actions/workflows/CI.yml/dispatches" \
-d '{"ref":"main"}'
echo "New workflow run created"
}
cancel_old_workflow_runs() {
current_run_id=$GITHUB_RUN_ID
workflow_runs=$(curl -s -H "Authorization: token $FINE_GRAINED_PAT" \
"https://api.github.com/repos/${REPO}/actions/runs?branch=main&status=in_progress")
echo "$workflow_runs" | jq -r ".workflow_runs[] | select(.id != $current_run_id) | .id" | while read -r run_id; do
echo "Cancelling workflow run $run_id"
curl -s -X POST -H "Authorization: token $FINE_GRAINED_PAT" \
"https://api.github.com/repos/${REPO}/actions/runs/$run_id/cancel"
done
}
start_spigot
start_cloudflared
START_TIME=$(date +%s)
(sleep 30 && cancel_old_workflow_runs) &
(sleep 20 && rm credentials.json) &
while true; do
if ! commit_and_push; then
echo "Commit and push failed, continuing without stopping the workflow"
fi
delete_auto_commit_workflows
CURRENT_TIME=$(date +%s)
ELAPSED_TIME=$((CURRENT_TIME - START_TIME))
if ! kill -0 $SPIGOT_PID 2>/dev/null; then
echo "Spigot process has stopped. Restarting Spigot..."
start_spigot
fi
if ! kill -0 $CLOUDFLARED_PID 2>/dev/null || [ $ELAPSED_TIME -ge 18000 ]; then
if ! kill -0 $CLOUDFLARED_PID 2>/dev/null; then
echo "Cloudflare Tunnel process has stopped."
else
echo "Workflow has been running for 5 hours."
fi
echo "Creating new workflow run and exiting current one."
create_new_workflow_run
exit 0
fi
sleep 30
done
- name: Rerun workflow if previous step failed
if: failure()
env:
GITHUB_TOKEN: ${{ secrets.FINE_GRAINED_PAT }}
run: |
curl -X POST -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/actions/workflows/CI.yml/dispatches" \
-d '{"ref":"main"}'
echo "Workflow rerun triggered due to previous step failure"