Skip to content

Commit c7055ae

Browse files
committed
Discord Pushs
1 parent a2a8efc commit c7055ae

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

.github/scripts/discord_push.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
if [ -z "${DISCORD_WEBHOOK_URL:-}" ]; then
5+
echo "Missing DISCORD_WEBHOOK_URL secret."
6+
exit 1
7+
fi
8+
9+
REPO="${GITHUB_REPOSITORY}"
10+
BRANCH="${GITHUB_REF_NAME}"
11+
ACTOR="${GITHUB_ACTOR}"
12+
13+
# On push events, GitHub gives BEFORE. On manual runs, it may not.
14+
BEFORE="${GITHUB_EVENT_BEFORE:-}"
15+
AFTER="${GITHUB_SHA}"
16+
17+
if [ -n "${BEFORE}" ] && git rev-parse "${BEFORE}" >/dev/null 2>&1; then
18+
range="${BEFORE}..${AFTER}"
19+
commits="$(git log --pretty=format:'- `%h` **%an**: %s' "$range")"
20+
else
21+
commits="$(git log -n 20 --pretty=format:'- `%h` **%an**: %s')"
22+
fi
23+
24+
msg="🛠️ **${REPO}** (\`${BRANCH}\`)
25+
✅ **New push by ${ACTOR}**
26+
${commits}"
27+
28+
python3 - "$msg" <<'PY'
29+
import json, os, sys, urllib.request
30+
31+
url = os.environ["DISCORD_WEBHOOK_URL"]
32+
content = sys.argv[1]
33+
34+
# Discord limit safety
35+
if len(content) > 1900:
36+
content = content[:1900] + "\n…(truncated)"
37+
38+
data = json.dumps({"content": content}).encode("utf-8")
39+
req = urllib.request.Request(url, data=data, headers={"Content-Type":"application/json"})
40+
urllib.request.urlopen(req).read()
41+
PY

.github/workflows/discord-push.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Discord Push Updates
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch: {}
8+
9+
jobs:
10+
notify:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
with:
15+
fetch-depth: 50
16+
17+
- name: Post commits to Discord
18+
env:
19+
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
20+
run: |
21+
chmod +x .github/scripts/discord_push.sh
22+
.github/scripts/discord_push.sh

0 commit comments

Comments
 (0)