Skip to content

Commit ef812f9

Browse files
authored
Update update.yml
Signed-off-by: Laurensius Jeffrey <[email protected]>
1 parent 556396a commit ef812f9

File tree

1 file changed

+88
-200
lines changed

1 file changed

+88
-200
lines changed

.github/workflows/update.yml

Lines changed: 88 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -2,233 +2,121 @@ name: Update Blocklist
22

33
on:
44
schedule:
5-
- cron: '0 * * * *' # Run hourly
5+
- cron: '0 * * * *' # Every hour
66
workflow_dispatch:
77

88
jobs:
99
update-blocklist:
1010
runs-on: ubuntu-latest
11-
env:
12-
# Set environment variables for all steps in this job
13-
DOMAIN_URL: ${{ secrets.SOURCE_URL }}
14-
IP_URL: ${{ secrets.SOURCE_URL2 }}
15-
SITUS_JUDI: ${{ secrets.SITUS_JUDI }}
16-
GIT_USERNAME: ${{ secrets.GIT_USERNAME || 'skiddle-bot' }} # Fallback if secret is not set
17-
GIT_EMAIL: ${{ secrets.GIT_EMAIL || '[email protected]' }} # Fallback
1811

1912
steps:
20-
- name: Checkout Repository
13+
- name: Checkout Repo
2114
uses: actions/checkout@v4
2215
with:
23-
# Use a dedicated bot token for push actions, fallback to default GITHUB_TOKEN
24-
# BOT_GITHUB_TOKEN must have 'repo' scope for pushing changes
16+
ref: ${{ github.head_ref }}
2517
token: ${{ secrets.BOT_GITHUB_TOKEN || github.token }}
26-
ref: ${{ github.head_ref }} # Ensure we checkout the correct branch
2718

2819
- name: Setup WireGuard
2920
run: |
30-
echo "Updating apt-get and installing WireGuard..."
3121
sudo apt-get update
3222
sudo apt-get install -y wireguard resolvconf
33-
echo "Writing WireGuard config to /etc/wireguard/wg0.conf..."
34-
# Ensure the secret contains the correct WireGuard config format.
35-
# Use tee with sudo to write to a protected directory.
36-
echo "${{ secrets.WIREGUARD_CONFIG }}" | sudo tee /etc/wireguard/wg0.conf > /dev/null
37-
echo "Bringing up WireGuard interface..."
23+
echo "${{ secrets.WIREGUARD_CONFIG }}" > wg0.conf
24+
sudo mv wg0.conf /etc/wireguard/wg0.conf
3825
sudo wg-quick up wg0
3926
shell: bash
40-
continue-on-error: false # Stop if WireGuard setup fails
4127

42-
- name: Download Blocklist Files
43-
id: download_files # Add an ID to reference outputs from this step (not directly used here, but good practice)
28+
- name: Download and Update Blocklist
4429
run: |
45-
echo "Downloading domain blocklist from $DOMAIN_URL..."
46-
curl --insecure -m 300 -o "domains" "$DOMAIN_URL" || { echo "Failed to download domains"; exit 1; }
47-
48-
echo "Downloading IP address blocklist from $IP_URL..."
49-
curl --insecure -m 300 -o "ipaddress_isp" "$IP_URL" || { echo "Failed to download ipaddress_isp"; exit 1; }
50-
51-
echo "Downloading gambling sites blocklist from $SITUS_JUDI..."
52-
curl --insecure -m 300 -o "situs_judi.txt" "$SITUS_JUDI" || { echo "Failed to download situs_judi.txt"; exit 1; }
53-
54-
# Check if files were downloaded and are not empty
55-
if [ ! -s domains ]; then echo "Error: 'domains' file is empty or missing."; exit 1; fi
56-
if [ ! -s ipaddress_isp ]; then echo "Error: 'ipaddress_isp' file is empty or missing."; exit 1; fi
57-
if [ ! -s situs_judi.txt ]; then echo "Error: 'situs_judi.txt' file is empty or missing."; exit 1; fi
30+
set -e
31+
now=$(date +"%Y-%m-%d_%H-%M-%S")
5832
59-
echo "All files downloaded successfully."
33+
# Download source files
34+
curl --insecure -m 300 -o "domains" "$DOMAIN_URL"
35+
curl --insecure -m 300 -o "ipaddress_isp" "$IP_URL"
36+
curl --insecure -m 300 -o "situs_judi.txt" "$SITUS_JUDI"
37+
38+
# Split large files into 50MB chunks
39+
maxsize=52428800
40+
for file in domains ipaddress_isp situs_judi.txt; do
41+
if [ -f "$file" ] && [ $(stat -c%s "$file") -gt $maxsize ]; then
42+
split -b 50M "$file" "${file}_part_"
43+
rm "$file"
44+
fi
45+
done
46+
47+
# Count entries
48+
domain_count=$(grep -chE '.' domains_part_* 2>/dev/null || grep -cE '.' domains 2>/dev/null || echo 0)
49+
ip_count=$(grep -chE '.' ipaddress_isp_part_* 2>/dev/null || grep -cE '.' ipaddress_isp 2>/dev/null || echo 0)
50+
judi_count=$(grep -chE '.' situs_judi.txt_part_* 2>/dev/null || grep -cE '.' situs_judi.txt 2>/dev/null || echo 0)
51+
52+
# Prepare summary
53+
summary="<!-- SUMMARY:START -->
54+
### 🧾 Blocklist Summary (Last Updated: $now)
55+
56+
| List | Entries |
57+
|--------------|---------|
58+
| Domains | $domain_count |
59+
| IP Address | $ip_count |
60+
| Situs Judi | $judi_count |
61+
62+
<!-- SUMMARY:END -->"
63+
64+
# Replace summary block in README.md
65+
tmpfile=$(mktemp)
66+
awk '/<!-- SUMMARY:START -->/,/<!-- SUMMARY:END -->/ {next} {print}' README.md > "$tmpfile"
67+
echo "$summary" >> "$tmpfile"
68+
mv "$tmpfile" README.md
69+
70+
# Git operations
71+
git config user.name "skiddle-bot"
72+
git config user.email "[email protected]"
73+
git add .
74+
git commit -m "Updated on $now" || echo "No changes to commit"
75+
git rebase
76+
git push -u origin main
6077
shell: bash
61-
continue-on-error: false
78+
env:
79+
DOMAIN_URL: ${{ secrets.SOURCE_URL }}
80+
IP_URL: ${{ secrets.SOURCE_URL2 }}
81+
SITUS_JUDI: ${{ secrets.SITUS_JUDI }}
6282

63-
# --- File Slicing Logic ---
64-
# This step will split 'domains' and 'ipaddress_isp' into smaller files
65-
# Modify `lines_per_file` as needed.
66-
- name: Slice Large Files
67-
id: slice_files # Add an ID for this step to reference its outputs
83+
- name: Kill WireGuard
6884
run: |
69-
echo "Starting file slicing process..."
70-
# Max lines per output file. Adjust this value based on your needs.
71-
LINES_PER_FILE=100000
72-
73-
# Initialize counts for summary
74-
DOMAINS_COUNT=0
75-
IP_COUNT=0
76-
SITUS_JUDI_COUNT=0
77-
78-
# Slice 'domains' file and count total lines
79-
if [ -f domains ]; then
80-
echo "Slicing 'domains' into files with max ${LINES_PER_FILE} lines each..."
81-
mkdir -p domains_parts
82-
# Count lines in original file before splitting
83-
DOMAINS_COUNT=$(wc -l < domains | awk '{print $1}')
84-
split -l $LINES_PER_FILE domains domains_parts/domains_part_
85-
echo "Slicing of 'domains' completed. Parts are in domains_parts/."
86-
rm domains # Remove the original large file after splitting
87-
else
88-
echo "'domains' file not found, skipping slicing."
89-
fi
90-
91-
# Slice 'ipaddress_isp' file and count total lines
92-
if [ -f ipaddress_isp ]; then
93-
echo "Slicing 'ipaddress_isp' into files with max ${LINES_PER_FILE} lines each..."
94-
mkdir -p ipaddress_isp_parts
95-
# Count lines in original file before splitting
96-
IP_COUNT=$(wc -l < ipaddress_isp | awk '{print $1}')
97-
split -l $LINES_PER_FILE ipaddress_isp ipaddress_isp_parts/ip_part_
98-
echo "Slicing of 'ipaddress_isp' completed. Parts are in ipaddress_isp_parts/."
99-
rm ipaddress_isp # Remove the original large file after splitting
100-
else
101-
echo "'ipaddress_isp' file not found, skipping slicing."
102-
fi
103-
104-
# Count lines for 'situs_judi.txt' (assuming this one doesn't need slicing)
105-
if [ -f situs_judi.txt ]; then
106-
SITUS_JUDI_COUNT=$(wc -l < situs_judi.txt | awk '{print $1}')
107-
else
108-
echo "'situs_judi.txt' file not found."
109-
fi
110-
111-
echo "File slicing and initial counting completed."
112-
113-
# Set outputs for use in README update step
114-
echo "DOMAINS_COUNT=$DOMAINS_COUNT" >> "$GITHUB_OUTPUT"
115-
echo "IP_COUNT=$IP_COUNT" >> "$GITHUB_OUTPUT"
116-
echo "SITUS_JUDI_COUNT=$SITUS_JUDI_COUNT" >> "$GITHUB_OUTPUT"
117-
85+
sudo wg-quick down wg0
11886
shell: bash
119-
continue-on-error: false
12087

121-
- name: Update README.md Summary
122-
id: readme_update # Add an ID for this step to reference its outputs
88+
- name: Notify via Discord (Success)
89+
if: success()
12390
run: |
124-
LAST_UPDATED=$(date -u +"%Y-%m-%d %H:%M:%S UTC")
125-
DOMAINS_COUNT="${{ steps.slice_files.outputs.DOMAINS_COUNT }}"
126-
IP_COUNT="${{ steps.slice_files.outputs.IP_COUNT }}"
127-
SITUS_JUDI_COUNT="${{ steps.slice_files.outputs.SITUS_JUDI_COUNT }}"
128-
129-
echo "Generating new README summary..."
130-
SUMMARY_CONTENT=$(cat <<EOF
131-
<!-- SUMMARY:START -->
132-
## Blocklist Summary
133-
134-
| Blocklist Type | Number of Entries |
135-
|----------------|-------------------|
136-
| Domains | ${DOMAINS_COUNT} |
137-
| IP Addresses | ${IP_COUNT} |
138-
| Situs Judi | ${SITUS_JUDI_COUNT} |
139-
140-
Last Updated: **${LAST_UPDATED}**
141-
<!-- SUMMARY:END -->
142-
EOF
143-
)
144-
echo "$SUMMARY_CONTENT"
145-
146-
# Use awk to replace content between markers
147-
# This robustly handles multi-line replacements
148-
awk -v start_marker="<!-- SUMMARY:START -->" \
149-
-v end_marker="<!-- SUMMARY:END -->" \
150-
-v new_content="$SUMMARY_CONTENT" \
151-
'BEGIN { in_summary = 0 }
152-
$0 == start_marker { print; print new_content; in_summary = 1 }
153-
$0 == end_marker { print; in_summary = 0 }
154-
!in_summary && $0 != start_marker && $0 != end_marker { print }
155-
' README.md > README.md.tmp && mv README.md.tmp README.md
156-
157-
echo "README.md updated with new summary."
158-
echo "LAST_UPDATED_TIME=$LAST_UPDATED" >> "$GITHUB_OUTPUT" # Output for Discord notification
91+
curl -H "Content-Type: application/json" \
92+
-X POST -d '{
93+
"embeds": [{
94+
"title": "✅ Blocklist Update Successful",
95+
"description": "The blocklist was updated and committed successfully.",
96+
"color": 3066993,
97+
"fields": [
98+
{"name": "Repository", "value": "${{ github.repository }}", "inline": true},
99+
{"name": "Time", "value": "'"$(date +"%Y-%m-%d %H:%M:%S")"'", "inline": true}
100+
],
101+
"footer": {"text": "Skiddle Bot | GitHub Actions"}
102+
}]
103+
}' ${{ secrets.DISCORD_WEBHOOK }}
159104
shell: bash
160-
continue-on-error: false
161105

162-
- name: Configure Git and Commit Changes
106+
- name: Notify via Discord (Failure)
107+
if: failure()
163108
run: |
164-
now=$(date +"%Y-%m-%d_%H-%M-%S")
165-
echo "Configuring Git user: ${GIT_USERNAME} <${GIT_EMAIL}>"
166-
git config user.name "${GIT_USERNAME}"
167-
git config user.email "${GIT_EMAIL}"
168-
169-
echo "Adding all changes to Git staging area..."
170-
git add .
171-
172-
# Check if there are any changes to commit (e.g., if files were actually updated or README changed)
173-
if git diff --cached --exit-code; then
174-
echo "No changes detected. Skipping commit."
175-
else
176-
echo "Committing changes with message: Updated blocklists and README on $now"
177-
git commit -m "Updated blocklists and README on $now"
178-
echo "Rebasing local changes with remote (to avoid merge conflicts)..."
179-
# It's safer to pull --rebase before pushing to avoid conflicts
180-
git pull --rebase origin main || { echo "Git pull --rebase failed."; exit 1; }
181-
echo "Pushing changes to origin main..."
182-
git push origin main || { echo "Git push failed. Ensure BOT_GITHUB_TOKEN has write access."; exit 1; }
183-
echo "Changes pushed successfully."
184-
fi
109+
curl -H "Content-Type: application/json" \
110+
-X POST -d '{
111+
"embeds": [{
112+
"title": "❌ Blocklist Update Failed",
113+
"description": "An error occurred during the workflow execution.",
114+
"color": 15158332,
115+
"fields": [
116+
{"name": "Repository", "value": "${{ github.repository }}", "inline": true},
117+
{"name": "Time", "value": "'"$(date +"%Y-%m-%d %H:%M:%S")"'", "inline": true}
118+
],
119+
"footer": {"text": "Skiddle Bot | GitHub Actions"}
120+
}]
121+
}' ${{ secrets.DISCORD_WEBHOOK }}
185122
shell: bash
186-
continue-on-error: false
187-
188-
- name: Teardown WireGuard
189-
# This step will always run, even if previous steps failed,
190-
# ensuring the WireGuard interface is brought down.
191-
if: always() # Ensures this step runs regardless of previous step outcomes
192-
run: |
193-
echo "Bringing down WireGuard interface..."
194-
sudo wg-quick down wg0 || echo "WireGuard interface not found or failed to bring down."
195-
shell: bash
196-
197-
- name: Send Discord Notification (Success)
198-
if: success() # Only run if all previous steps succeeded
199-
uses: peaceiris/[email protected]
200-
env:
201-
# Use the single secret for the full webhook URL
202-
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
203-
with:
204-
content: "✅ Blocklist update workflow completed successfully!"
205-
color: 0x00FF00 # Green color for success
206-
title: "Blocklist Update Success"
207-
description: |
208-
Blocklists in repository `${{ github.repository }}` were updated successfully.
209-
Workflow: [${{ github.workflow }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
210-
Commit: `${{ github.sha }}`
211-
Branch: `${{ github.ref_name }}`
212-
Last Updated in README: `${{ steps.readme_update.outputs.LAST_UPDATED_TIME }}`
213-
Domains: `${{ steps.slice_files.outputs.DOMAINS_COUNT }}`
214-
IPs: `${{ steps.slice_files.outputs.IP_COUNT }}`
215-
Situs Judi: `${{ steps.slice_files.outputs.SITUS_JUDI_COUNT }}`
216-
217-
- name: Send Discord Notification (Failure)
218-
if: failure() # Only run if any previous step failed
219-
uses: peaceiris/[email protected]
220-
env:
221-
# Use the single secret for the full webhook URL
222-
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
223-
with:
224-
content: "❌ Blocklist update workflow failed!"
225-
color: 0xFF0000 # Red color for failure
226-
title: "Blocklist Update Failed"
227-
description: |
228-
The blocklist update workflow for repository `${{ github.repository }}` failed.
229-
Please check the workflow run for details:
230-
[View Workflow Run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
231-
Workflow: `${{ github.workflow }}`
232-
Branch: `${{ github.ref_name }}`
233-
Commit: `${{ github.sha }}`
234-
continue-on-error: true # Ensure this notification step doesn't fail the workflow itself

0 commit comments

Comments
 (0)