name: Discord - Commits on: push: branches: - "**" workflow_dispatch: jobs: notify: runs-on: ubuntu-latest permissions: contents: read steps: - name: Post to Discord (embed) env: DISCORD_WEBHOOK: ${{ secrets.WEBHOOK_URL }} EVENT_JSON: ${{ toJson(github.event) }} run: | node - <<'NODE' const event = JSON.parse(process.env.EVENT_JSON); const webhook = process.env.DISCORD_WEBHOOK; if (!webhook) { console.error("❌ Secret WEBHOOK_URL manquant. (Repo Settings > Secrets and variables > Actions)"); process.exit(1); } const repoName = event.repository?.full_name || "unknown/repo"; const repoUrl = event.repository?.html_url || ""; const branch = (event.ref || "").replace("refs/heads/", ""); const compareUrl = event.compare || repoUrl; const pusher = event.pusher?.name || event.sender?.login || "someone"; const commits = (event.commits || []).slice(0, 10).map(c => { const sha = (c.id || "").slice(0, 7); const msg = (c.message || "").split("\n")[0]; const author = c.author?.name || "unknown"; const url = c.url ? c.url.replace("api.github.com/repos", "github.com").replace("/commits/", "/commit/") : ""; return { sha, msg, author, url }; }); const description = commits.length ? commits.map(c => `• **[${c.sha}](${c.url})** ${c.msg} — _${c.author}_`).join("\n") : "• (Pas de détails de commits fournis par l'événement)"; // Discord embed limits: description max ~4096 chars const safeDescription = description.length > 3900 ? description.slice(0, 3900) + "\n…" : description; const payload = { content: null, embeds: [ { title: `📌 Push sur ${repoName}`, url: compareUrl, description: safeDescription, color: 0x7289DA, author: { name: pusher, url: `https://github.com/${event.sender?.login || ""}` }, fields: [ { name: "Branche", value: `\`${branch || "?"}\``, inline: true }, { name: "Commits", value: `${event.commits?.length ?? commits.length}`, inline: true } ], footer: { text: "GitHub Actions → Discord" }, timestamp: new Date().toISOString() } ] }; fetch(webhook, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload) }) .then(async (res) => { const txt = await res.text(); if (!res.ok) { console.error(`❌ Discord error ${res.status}: ${txt}`); process.exit(1); } console.log("✅ Notification envoyée sur Discord."); }) .catch((err) => { console.error("❌ Fetch error:", err); process.exit(1); }); NODE