name: Commit Log to Discord on: push: branches: [main, master] jobs: discord: runs-on: ubuntu-latest steps: - name: Send embed to Discord # 📖 A broken notification must never turn pushes red: the step is # 📖 still marked failed (visible annotation) but the workflow stays # 📖 green. If this logs 404 or 401 the webhook was deleted or # 📖 regenerated in Discord: recreate it there, then update the # 📖 DISCORD_WEBHOOK_GITHUB secret (Settings > Secrets and variables # 📖 > Actions). continue-on-error: true env: DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_GITHUB }} REPO_NAME: ${{ github.event.repository.name }} REPOSITORY: ${{ github.repository }} COMMIT_SHA: ${{ github.sha }} COMMIT_MESSAGE: ${{ github.event.head_commit.message }} REF_NAME: ${{ github.ref_name }} run: | node - <<'EOF' const https = require('https') const webhook = process.env.DISCORD_WEBHOOK_URL if (!webhook) { console.log('⚠️ DISCORD_WEBHOOK_GITHUB not set — skipping Discord commit notification') process.exit(0) } // 📖 Commit messages can contain quotes, apostrophes, backticks, and // 📖 multi-line bodies. Build JSON in Node instead of interpolating // 📖 directly into shell heredocs so any commit text is safe. const repo = process.env.REPO_NAME || 'repository' const repository = process.env.REPOSITORY || '' const sha = process.env.COMMIT_SHA || '' const shaShort = sha.slice(0, 7) const ref = process.env.REF_NAME || 'main' const firstLine = String(process.env.COMMIT_MESSAGE || 'No commit message') .split(/\r?\n/)[0] .slice(0, 240) const body = JSON.stringify({ embeds: [{ title: `[${repo}] ${firstLine}`, url: `https://github.com/${repository}/commit/${sha}`, description: `Commit \`${shaShort}\` on \`${ref}\``, color: 5814783, footer: { text: repo }, }], }) const url = new URL(webhook) const req = https.request({ hostname: url.hostname, path: url.pathname + url.search, method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body), }, }, (res) => { console.log('Discord response status:', res.statusCode) if (res.statusCode === 404 || res.statusCode === 401) { // 📖 Discord answers 404 when the webhook was deleted or // 📖 regenerated: recreate it and update the GH secret. console.error('Webhook rejected (' + res.statusCode + '): recreate the Discord webhook, then update the DISCORD_WEBHOOK_GITHUB secret in repo Settings > Secrets and variables > Actions.') } res.resume() process.exit(res.statusCode >= 200 && res.statusCode < 300 ? 0 : 1) }) req.on('error', (err) => { console.error(err); process.exit(1) }) req.write(body) req.end() EOF