|
| 1 | +name: Discord notify |
| 2 | + |
| 3 | +# Posts to Discord via webhooks when: |
| 4 | +# - new commits land on main → #dev-log (DISCORD_WEBHOOK_DEV) |
| 5 | +# - a GitHub release published → #announcements (DISCORD_WEBHOOK_ANNOUNCE) |
| 6 | +# Set both secrets under Settings → Secrets and variables → Actions. |
| 7 | +# If a secret is missing, that job no-ops instead of failing. |
| 8 | +on: |
| 9 | + push: |
| 10 | + branches: [main] |
| 11 | + release: |
| 12 | + types: [published] |
| 13 | + |
| 14 | +jobs: |
| 15 | + commits: |
| 16 | + if: github.event_name == 'push' |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - name: Post commits to #dev-log |
| 20 | + uses: actions/github-script@v7 |
| 21 | + env: |
| 22 | + DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_DEV }} |
| 23 | + with: |
| 24 | + script: | |
| 25 | + const webhook = process.env.DISCORD_WEBHOOK; |
| 26 | + if (!webhook) { core.info("DISCORD_WEBHOOK not set — skipping."); return; } |
| 27 | +
|
| 28 | + const commits = context.payload.commits || []; |
| 29 | + if (commits.length === 0) { core.info("No commits in payload — skipping."); return; } |
| 30 | +
|
| 31 | + const repo = `${context.repo.owner}/${context.repo.repo}`; |
| 32 | + const lines = commits.map((c) => { |
| 33 | + const sha = c.id.slice(0, 7); |
| 34 | + const msg = c.message.split("\n")[0].slice(0, 120); |
| 35 | + return `[\`${sha}\`](${c.url}) ${msg} — ${c.author.name}`; |
| 36 | + }); |
| 37 | +
|
| 38 | + const payload = { |
| 39 | + embeds: [{ |
| 40 | + title: `📦 ${commits.length} new commit${commits.length > 1 ? "s" : ""} on main`, |
| 41 | + description: lines.join("\n").slice(0, 4000), |
| 42 | + url: context.payload.compare, |
| 43 | + color: 0x5865f2, |
| 44 | + footer: { text: repo }, |
| 45 | + timestamp: new Date().toISOString(), |
| 46 | + }], |
| 47 | + }; |
| 48 | +
|
| 49 | + const res = await fetch(webhook, { |
| 50 | + method: "POST", |
| 51 | + headers: { "Content-Type": "application/json" }, |
| 52 | + body: JSON.stringify(payload), |
| 53 | + }); |
| 54 | + if (!res.ok) core.setFailed(`Discord responded ${res.status}: ${await res.text()}`); |
| 55 | +
|
| 56 | + release: |
| 57 | + if: github.event_name == 'release' |
| 58 | + runs-on: ubuntu-latest |
| 59 | + steps: |
| 60 | + - name: Post release to #announcements |
| 61 | + uses: actions/github-script@v7 |
| 62 | + env: |
| 63 | + DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK_ANNOUNCE }} |
| 64 | + with: |
| 65 | + script: | |
| 66 | + const webhook = process.env.DISCORD_WEBHOOK; |
| 67 | + if (!webhook) { core.info("DISCORD_WEBHOOK not set — skipping."); return; } |
| 68 | +
|
| 69 | + const r = context.payload.release; |
| 70 | + const repo = `${context.repo.owner}/${context.repo.repo}`; |
| 71 | + const body = (r.body || "").trim().slice(0, 3500) || "_No release notes._"; |
| 72 | +
|
| 73 | + const payload = { |
| 74 | + content: "@here", |
| 75 | + embeds: [{ |
| 76 | + title: `🚀 ${r.name || r.tag_name}`, |
| 77 | + description: body, |
| 78 | + url: r.html_url, |
| 79 | + color: 0x57f287, |
| 80 | + footer: { text: repo }, |
| 81 | + timestamp: r.published_at, |
| 82 | + }], |
| 83 | + }; |
| 84 | +
|
| 85 | + const res = await fetch(webhook, { |
| 86 | + method: "POST", |
| 87 | + headers: { "Content-Type": "application/json" }, |
| 88 | + body: JSON.stringify(payload), |
| 89 | + }); |
| 90 | + if (!res.ok) core.setFailed(`Discord responded ${res.status}: ${await res.text()}`); |
0 commit comments