|
| 1 | +name: "Notify Discord" |
| 2 | +description: "Post a release announcement embed to a Discord channel webhook" |
| 3 | + |
| 4 | +inputs: |
| 5 | + webhook-url: |
| 6 | + description: "[secret] Discord channel webhook URL. Empty disables the block." |
| 7 | + required: false |
| 8 | + default: "" |
| 9 | + service-name: |
| 10 | + description: "[string] Display name of the released service" |
| 11 | + required: true |
| 12 | + version: |
| 13 | + description: "[string] Version being released (e.g., 1.2.3)" |
| 14 | + required: true |
| 15 | + release-url: |
| 16 | + description: "[string] URL to the GitHub Release" |
| 17 | + required: true |
| 18 | + release-notes: |
| 19 | + description: "[string] Markdown release notes (truncated to 4096 chars)" |
| 20 | + required: false |
| 21 | + default: "" |
| 22 | + prerelease: |
| 23 | + description: "[boolean: 'true'/'false'] Whether this is a prerelease" |
| 24 | + required: false |
| 25 | + default: "false" |
| 26 | + dry-run: |
| 27 | + description: "[boolean: 'true'/'false'] Log payload without sending" |
| 28 | + required: false |
| 29 | + default: "false" |
| 30 | + |
| 31 | +runs: |
| 32 | + using: composite |
| 33 | + steps: |
| 34 | + - name: Send Discord announcement |
| 35 | + uses: actions/github-script@v7 |
| 36 | + env: |
| 37 | + WEBHOOK_URL: ${{ inputs.webhook-url }} |
| 38 | + SERVICE_NAME: ${{ inputs.service-name }} |
| 39 | + VERSION: ${{ inputs.version }} |
| 40 | + RELEASE_URL: ${{ inputs.release-url }} |
| 41 | + RELEASE_NOTES: ${{ inputs.release-notes }} |
| 42 | + PRERELEASE: ${{ inputs.prerelease }} |
| 43 | + DRY_RUN: ${{ inputs.dry-run }} |
| 44 | + with: |
| 45 | + script: | |
| 46 | + const webhookUrl = process.env.WEBHOOK_URL; |
| 47 | + if (!webhookUrl) { |
| 48 | + core.info('Discord webhook not configured, skipping.'); |
| 49 | + return; |
| 50 | + } |
| 51 | + core.setSecret(webhookUrl); |
| 52 | +
|
| 53 | + const serviceName = process.env.SERVICE_NAME; |
| 54 | + const version = process.env.VERSION; |
| 55 | + const releaseUrl = process.env.RELEASE_URL; |
| 56 | + const rawNotes = process.env.RELEASE_NOTES || ''; |
| 57 | + const isPrerelease = process.env.PRERELEASE === 'true'; |
| 58 | + const isDryRun = process.env.DRY_RUN === 'true'; |
| 59 | +
|
| 60 | + const MAX_DESCRIPTION = 4096; |
| 61 | + const description = rawNotes.length > MAX_DESCRIPTION |
| 62 | + ? rawNotes.slice(0, MAX_DESCRIPTION - 1) + '\u2026' |
| 63 | + : rawNotes; |
| 64 | +
|
| 65 | + const titlePrefix = isPrerelease ? '[Pre-release] ' : ''; |
| 66 | + const color = isPrerelease ? 0xF1C40F : 0x2ECC71; |
| 67 | +
|
| 68 | + const payload = { |
| 69 | + embeds: [ |
| 70 | + { |
| 71 | + title: `${titlePrefix}${serviceName} v${version}`, |
| 72 | + url: releaseUrl, |
| 73 | + description, |
| 74 | + color, |
| 75 | + timestamp: new Date().toISOString(), |
| 76 | + footer: { text: context.payload.repository?.full_name || `${context.repo.owner}/${context.repo.repo}` }, |
| 77 | + }, |
| 78 | + ], |
| 79 | + }; |
| 80 | +
|
| 81 | + if (isDryRun) { |
| 82 | + core.info('Dry run enabled, not sending. Payload:'); |
| 83 | + core.info(JSON.stringify(payload, null, 2)); |
| 84 | + return; |
| 85 | + } |
| 86 | +
|
| 87 | + const response = await fetch(webhookUrl, { |
| 88 | + method: 'POST', |
| 89 | + headers: { 'Content-Type': 'application/json' }, |
| 90 | + body: JSON.stringify(payload), |
| 91 | + }); |
| 92 | +
|
| 93 | + if (!response.ok) { |
| 94 | + const body = await response.text(); |
| 95 | + core.setFailed(`Discord webhook returned ${response.status} ${response.statusText}: ${body}`); |
| 96 | + return; |
| 97 | + } |
| 98 | +
|
| 99 | + core.info(`Discord announcement posted (HTTP ${response.status}).`); |
0 commit comments