Skip to content

Option to also move empty folders (preserve folder structure / no leftovers on cache) #6

Option to also move empty folders (preserve folder structure / no leftovers on cache)

Option to also move empty folders (preserve folder structure / no leftovers on cache) #6

name: Universal Discord Notifier
on:
release:
types: [published, created]
pull_request:
types: [opened, closed, reopened]
issues:
types: [opened, closed, reopened]
workflow_run:
workflows: ["Maybe in future"]
types: [completed]
workflow_dispatch:
permissions:
actions: read
jobs:
notify_discord:
runs-on: ubuntu-latest
steps:
# Step 1: Download release info artifact (from build-fish)
- name: Download release info artifact
if: ${{ github.event_name == 'workflow_run' }}
uses: actions/download-artifact@v5
with:
name: release_info
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
# Step 2: Read release info
- name: Read release info
if: ${{ github.event_name == 'workflow_run' }}
id: read_release
run: |
RELEASE_TAG=$(jq -r '.release_tag' release_info.json)
RELEASE_NAME=$(jq -r '.release_name' release_info.json)
NEW_RELEASE=$(jq -r '.new_release' release_info.json)
echo "RELEASE_TAG=$RELEASE_TAG" >> $GITHUB_ENV
echo "RELEASE_NAME=$RELEASE_NAME" >> $GITHUB_ENV
echo "NEW_RELEASE=$NEW_RELEASE" >> $GITHUB_ENV
# Step 3: Send Discord notification
- name: Send Discord Notification
if: ${{ !(github.event_name == 'workflow_run' && env.NEW_RELEASE != 'true' && github.event.workflow_run.conclusion == 'success') }}
env:
WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
EVENT: ${{ github.event_name }}
ACTION: ${{ github.event.action || '' }}
ACTOR: ${{ github.actor }}
REPO: ${{ github.repository }}
# Release info
RELEASE_URL: ${{ github.event.release.html_url || '' }}
RELEASE_BODY: ${{ github.event.release.body || '' }}
RELEASE_TAG: ${{ github.event_name == 'release' && github.event.release.tag_name || env.RELEASE_TAG || '' }}
RELEASE_NAME: ${{ github.event_name == 'release' && github.event.release.name || env.RELEASE_NAME || '' }}
NEW_RELEASE: ${{ github.event_name == 'release' && 'true' || env.NEW_RELEASE || 'false' }}
# PR/Issue info
PR_TITLE: ${{ github.event.pull_request.title || '' }}
PR_URL: ${{ github.event.pull_request.html_url || '' }}
PR_BODY: ${{ github.event.pull_request.body || '' }}
PR_MERGED: ${{ github.event.pull_request.merged || '' }}
ISSUE_TITLE: ${{ github.event.issue.title || '' }}
ISSUE_URL: ${{ github.event.issue.html_url || '' }}
ISSUE_BODY: ${{ github.event.issue.body || '' }}
# Workflow run info
WORKFLOW_NAME: ${{ github.event.workflow_run.name || '' }}
WORKFLOW_CONCLUSION: ${{ github.event.workflow_run.conclusion || '' }}
WORKFLOW_URL: ${{ github.event.workflow_run.html_url || '' }}
run: |
# Determine event type and set Discord embed variables
URL=""
TITLE=""
BODY=""
CONTENT=""
COLOR=8421504
ICON="🧭"
case "$EVENT" in
release)
URL="${RELEASE_URL}"
TITLE="${RELEASE_NAME:-Release $RELEASE_TAG}"
BODY="$(echo "${RELEASE_BODY}" | head -c 300)"
COLOR=3447003
ICON="πŸš€"
CONTENT="**New Release Published**"
;;
pull_request)
URL="${PR_URL}"
TITLE="${PR_TITLE:-Pull Request}"
BODY="$(echo "${PR_BODY}" | head -c 300)"
if [ "$ACTION" = "opened" ]; then
COLOR=5763719
ICON="πŸ”€"
CONTENT="**Pull Request Opened**"
elif [ "$ACTION" = "closed" ] && [ "$PR_MERGED" = "true" ]; then
COLOR=8311585
ICON="πŸŽ‰"
CONTENT="**Pull Request Merged!**"
else
COLOR=10038562
ICON="❌"
CONTENT="**Pull Request Closed**"
fi
;;
issues)
URL="${ISSUE_URL}"
TITLE="${ISSUE_TITLE:-Issue}"
BODY="$(echo "${ISSUE_BODY}" | head -c 300)"
if [ "$ACTION" = "opened" ]; then
COLOR=16776960
ICON="🐞"
CONTENT="**New Issue Opened**"
elif [ "$ACTION" = "closed" ]; then
COLOR=10038562
ICON="βœ…"
CONTENT="**Issue Closed**"
else
COLOR=9807270
ICON="πŸŒ€"
CONTENT="**Issue Reopened**"
fi
;;
workflow_run)
URL="${WORKFLOW_URL:-https://github.com/${REPO}/actions}"
TITLE="${WORKFLOW_NAME:-Workflow Run}"
BODY="Workflow '${WORKFLOW_NAME}' completed with conclusion: ${WORKFLOW_CONCLUSION}"
# Append release info if available
if [ "${RELEASE_TAG}" != "" ] && [ "${NEW_RELEASE}" = "true" ]; then
BODY="$BODY"$'\n'"Built Version: $RELEASE_TAG"
TITLE="$TITLE β€” $RELEASE_TAG"
CONTENT="**Workflow Succeeded β€” New Release**"
COLOR=5763719
ICON="βœ…"
else
COLOR=5763719
ICON="βœ…"
CONTENT="**Workflow Succeeded**"
fi
if [ "$WORKFLOW_CONCLUSION" != "success" ]; then
COLOR=10038562
ICON="❌"
CONTENT="**Workflow Failed**"
fi
;;
*)
URL="https://github.com/${REPO}"
TITLE="Manual Trigger"
BODY="Workflow manually triggered by ${ACTOR}"
COLOR=8421504
ICON="🧭"
CONTENT="**Manual Trigger**"
;;
esac
# Escape variables
TITLE_ESC=$(echo "$TITLE" | sed 's/"/\\"/g')
BODY_ESC=$(echo "${BODY:-(No content)}" | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
# Build conditional fields
ACTOR_ESC=$(echo "$ACTOR" | sed 's/"/\\"/g')
RELEASE_TAG_ESC=$(echo "$RELEASE_TAG" | sed 's/"/\\"/g')
RELEASE_NAME_ESC=$(echo "$RELEASE_NAME" | sed 's/"/\\"/g')
REPO_ESC=$(echo "$REPO" | sed 's/"/\\"/g')
FIELDS="["
FIELDS+="{\"name\": \"Repository\", \"value\": \"${REPO_ESC}\", \"inline\": true},"
FIELDS+="{\"name\": \"Actor\", \"value\": \"${ACTOR_ESC}\", \"inline\": true}"
if [ "$EVENT" = "release" ] || ([ "$EVENT" = "workflow_run" ] && [ "$NEW_RELEASE" = "true" ]); then
FIELDS+=",{\"name\": \"Release Tag\", \"value\": \"${RELEASE_TAG_ESC}\", \"inline\": true}"
FIELDS+=",{\"name\": \"Release Name\", \"value\": \"${RELEASE_NAME_ESC}\", \"inline\": true}"
fi
FIELDS+="]"
# Build JSON
JSON=$(cat <<EOF
{
"content": "${ICON} ${CONTENT}",
"embeds": [
{
"title": "${TITLE_ESC}",
"url": "${URL}",
"description": "${BODY_ESC}",
"color": ${COLOR},
"fields": ${FIELDS},
"timestamp": "${TIMESTAMP}"
}
]
}
EOF
)
# Send to Discord
curl -H "Content-Type: application/json" -d "$JSON" "$WEBHOOK"