Juju519 commited on
Commit
0c63bb8
·
1 Parent(s): 71adf01

ci: add Discord notification workflow

Browse files
.github/workflows/discord-notification.yml CHANGED
@@ -3,16 +3,72 @@ name: Discord Notification
3
  on:
4
  push:
5
  branches: [case_study_1_2025]
 
 
 
 
6
 
7
  jobs:
8
  notify:
9
  runs-on: ubuntu-latest
10
 
11
  steps:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  - name: Send Discord notification
 
13
  env:
14
- DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} # Make sure to set this Webhook URL in Github Secrets!
 
15
  run: |
16
- curl -X POST -H "Content-Type: application/json" \
17
- -d "{\"content\": \"A new commit was pushed to the main branch by $GITHUB_ACTOR. :smile: Ponytails rock!\"}" \
18
- $DISCORD_WEBHOOK_URL
 
 
 
3
  on:
4
  push:
5
  branches: [case_study_1_2025]
6
+ workflow_dispatch:
7
+
8
+ permissions:
9
+ contents: read
10
 
11
  jobs:
12
  notify:
13
  runs-on: ubuntu-latest
14
 
15
  steps:
16
+ - name: Guard: ensure webhook is set
17
+ id: guard
18
+ run: |
19
+ if [ -z "${{ secrets.DISCORD_WEBHOOK_URL }}" ]; then
20
+ echo "Webhook secret DISCORD_WEBHOOK_URL is not set. Skipping."
21
+ echo "continue=false" >> $GITHUB_OUTPUT
22
+ else
23
+ echo "continue=true" >> $GITHUB_OUTPUT
24
+ fi
25
+
26
+ - name: Build rich Discord embed
27
+ if: steps.guard.outputs.continue == 'true'
28
+ id: payload
29
+ uses: actions/github-script@v7
30
+ with:
31
+ script: |
32
+ const repo = context.payload.repository.full_name;
33
+ const branch = (context.ref || "").replace("refs/heads/", "") || context.payload.ref || "unknown";
34
+ const actor = context.actor;
35
+ const sha = context.sha || (context.payload.after ?? "");
36
+ const sha7 = sha ? sha.substring(0,7) : "unknown";
37
+ const head = context.payload.head_commit || null;
38
+ const message = head?.message || (context.payload.commits?.length ? `${context.payload.commits.length} commits pushed` : "(no message)");
39
+ const compare = context.payload.compare || `https://github.com/${repo}/commit/${sha}`;
40
+
41
+ // Discord embed (colors: green=5763719, red=15548997, yellow=16776960, purple-ish=5814783)
42
+ const embed = {
43
+ title: `Push to ${branch}`,
44
+ url: compare,
45
+ description: `**${repo}**\n\`${sha7}\` by **${actor}**`,
46
+ color: 5814783,
47
+ fields: [
48
+ { name: "Branch", value: branch, inline: true },
49
+ { name: "Actor", value: actor, inline: true },
50
+ { name: "Message", value: (message || "").slice(0, 1000) || "(empty)" }
51
+ ],
52
+ timestamp: new Date().toISOString()
53
+ };
54
+
55
+ const payload = {
56
+ username: "CI Bot",
57
+ // Set content to "@here" or "<@&ROLE_ID>" if you want a ping:
58
+ // content: null,
59
+ embeds: [embed]
60
+ };
61
+
62
+ core.setOutput("json", JSON.stringify(payload));
63
+
64
  - name: Send Discord notification
65
+ if: steps.guard.outputs.continue == 'true'
66
  env:
67
+ DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
68
+ PAYLOAD: ${{ steps.payload.outputs.json }}
69
  run: |
70
+ curl -sS -X POST \
71
+ -H "Content-Type: application/json" \
72
+ --retry 5 --retry-delay 2 \
73
+ -d "$PAYLOAD" \
74
+ "$DISCORD_WEBHOOK_URL"