Skip to content

Discord Release Notification #42

Discord Release Notification

Discord Release Notification #42

name: Discord Release Notification
on:
release:
types: [published]
workflow_dispatch: # Allows manual trigger
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Get Release Information
id: get_release
run: |
if [ "${{ github.event_name }}" == "release" ]; then
# Use event data for automatic triggers
echo "RELEASE_NAME=${{ github.event.release.name }}" >> $GITHUB_ENV
echo "RELEASE_TAG=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
echo "RELEASE_BODY<<EOF" >> $GITHUB_ENV
echo "${{ github.event.release.body }}" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "RELEASE_URL=${{ github.event.release.html_url }}" >> $GITHUB_ENV
echo "RELEASE_AUTHOR=${{ github.event.release.author.login }}" >> $GITHUB_ENV
echo "RELEASE_AUTHOR_AVATAR=${{ github.event.release.author.avatar_url }}" >> $GITHUB_ENV
echo "ASSETS<<EOF" >> $GITHUB_ENV
echo '${{ toJson(github.event.release.assets) }}' | jq -r 'map("[\(.name)](\(.browser_download_url))") | join("\n")' >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
else
# Fetch latest release data for manual triggers
RELEASE_DATA=$(curl -s -H "Authorization: token ${{ github.token }}" \
"https://api.github.com/repos/${{ github.repository }}/releases/latest")
echo "RELEASE_NAME=$(echo "$RELEASE_DATA" | jq -r '.name')" >> $GITHUB_ENV
echo "RELEASE_TAG=$(echo "$RELEASE_DATA" | jq -r '.tag_name')" >> $GITHUB_ENV
echo "RELEASE_BODY<<EOF" >> $GITHUB_ENV
echo "$RELEASE_DATA" | jq -r '.body' >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "RELEASE_URL=$(echo "$RELEASE_DATA" | jq -r '.html_url')" >> $GITHUB_ENV
echo "RELEASE_AUTHOR=$(echo "$RELEASE_DATA" | jq -r '.author.login')" >> $GITHUB_ENV
echo "RELEASE_AUTHOR_AVATAR=$(echo "$RELEASE_DATA" | jq -r '.author.avatar_url')" >> $GITHUB_ENV
echo "ASSETS<<EOF" >> $GITHUB_ENV
echo "$RELEASE_DATA" | jq -r '.assets | map("[\(.name)](\(.browser_download_url))") | join("\n")' >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
fi
- name: Send Discord Notification
env:
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
run: |
REPO_NAME="${{ github.repository }}"
REPO_URL="https://github.com/${{ github.repository }}"
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
# Get assets or set default message
ASSETS="${{ env.ASSETS }}"
if [ -z "$ASSETS" ]; then
ASSETS="No downloadable assets"
fi
# Truncate release body if it exceeds Discord's 4096 character limit
MAX_DESC_LENGTH=4000
RELEASE_BODY="${{ env.RELEASE_BODY }}"
RELEASE_URL="${{ env.RELEASE_URL }}"
if [ ${#RELEASE_BODY} -gt $MAX_DESC_LENGTH ]; then
echo "Release body is ${#RELEASE_BODY} characters, truncating to $MAX_DESC_LENGTH"
RELEASE_BODY="${RELEASE_BODY:0:$MAX_DESC_LENGTH}... [Read full release notes]($RELEASE_URL)"
else
echo "Release body is ${#RELEASE_BODY} characters (within limit)"
fi
# Create Discord embed JSON using jq (proper escaping)
DISCORD_PAYLOAD=$(jq -n \
--arg title "🚀 New Release: ${{ env.RELEASE_NAME }}" \
--arg description "$RELEASE_BODY" \
--arg url "$RELEASE_URL" \
--arg tag "${{ env.RELEASE_TAG }}" \
--arg author "${{ env.RELEASE_AUTHOR }}" \
--arg avatar "${{ env.RELEASE_AUTHOR_AVATAR }}" \
--arg assets "$ASSETS" \
--arg timestamp "$TIMESTAMP" \
--arg repo "$REPO_NAME" \
--arg repo_url "$REPO_URL" \
'{
embeds: [{
title: $title,
description: $description,
url: $url,
color: 3066993,
fields: [
{
name: "📦 Version",
value: ("`" + $tag + "`"),
inline: true
},
{
name: "👤 Author",
value: ("[" + $author + "](https://github.com/" + $author + ")"),
inline: true
},
{
name: "📂 Repository",
value: ("[" + $repo + "](" + $repo_url + ")"),
inline: false
},
{
name: "⬇️ Downloads",
value: $assets,
inline: false
}
],
author: {
name: $author,
icon_url: $avatar,
url: ("https://github.com/" + $author)
},
timestamp: $timestamp,
footer: {
text: ("Released from " + $repo)
}
}]
}') || { echo "Failed to create JSON payload"; exit 1; }
# Send to Discord with error handling
echo "Sending notification to Discord..."
RESPONSE=$(curl -s -w "\n%{http_code}" -H "Content-Type: application/json" \
-d "$DISCORD_PAYLOAD" \
"$DISCORD_WEBHOOK_URL")
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
RESPONSE_BODY=$(echo "$RESPONSE" | sed '$d')
if [ "$HTTP_CODE" -eq 204 ] || [ "$HTTP_CODE" -eq 200 ]; then
echo "Successfully sent Discord notification (HTTP $HTTP_CODE)"
else
echo "Failed to send Discord notification (HTTP $HTTP_CODE)"
echo "Response: $RESPONSE_BODY"
exit 1
fi