feat: Add configurable Ntfy and Gmail options, Ntfy Basic Auth, and parallel notification sending.

This commit is contained in:
2025-12-29 16:26:37 +07:00
parent 2298c3a4b3
commit b458564344
2 changed files with 133 additions and 35 deletions

View File

@@ -3,11 +3,50 @@ description: "Notify deployment result via ntfy, Gmail SMTP, and Telegram"
inputs: inputs:
status: status:
description: 'Deployment status (e.g., success, failure)'
required: true required: true
title: title:
description: 'Notification title'
required: true required: true
message: message:
description: 'Notification message body'
required: true required: true
# Configuration
ntfy_url:
description: 'Ntfy server URL'
required: false
default: 'https://ntfy.hcmc.online'
ntfy_topic:
description: 'Ntfy topic'
required: false
default: 'act_runner_deployment'
ntfy_username:
description: 'Ntfy Basic Auth Username'
required: false
default: 'homesrv'
ntfy_password:
description: 'Ntfy Basic Auth Password'
required: false
server_url:
description: 'Git server BASE URL (e.g., https://git.hcmc.online)'
required: false
default: 'https://git.hcmc.online'
# Email Configuration
gmail_user:
description: 'Gmail username (sender)'
required: false
default: 'sitienbmt@gmail.com'
gmail_to:
description: 'Email recipient'
required: false
default: 'sitienbmt@gmail.com'
gmail_password:
description: 'Gmail App Password'
required: false
runs: runs:
using: "composite" using: "composite"
@@ -15,17 +54,24 @@ runs:
- name: Send notification - name: Send notification
shell: bash shell: bash
env: env:
# Core
STATUS: ${{ inputs.status }} STATUS: ${{ inputs.status }}
TITLE: ${{ inputs.title }} TITLE: ${{ inputs.title }}
MESSAGE: ${{ inputs.message }} MESSAGE: ${{ inputs.message }}
NTFY_URL: "https://ntfy.hcmc.online" # Ntfy
NTFY_TOPIC: "deploy" NTFY_URL: ${{ inputs.ntfy_url }}
NTFY_TOPIC: ${{ inputs.ntfy_topic }}
GMAIL_USER: "sitienbmt@gmail.com" NTFY_USERNAME: ${{ inputs.ntfy_username }}
GMAIL_TO: "sitienbmt@gmail.com" NTFY_PASSWORD: ${{ inputs.ntfy_password || secrets.NTFY_PASSWORD }}
GMAIL_PASS: ${{ secrets.GMAIL_APP_PASSWORD }} GIT_URL: ${{ inputs.server_url }}
# Gmail
GMAIL_USER: ${{ inputs.gmail_user }}
GMAIL_TO: ${{ inputs.gmail_to }}
GMAIL_PASS: ${{ inputs.gmail_password || secrets.GMAIL_APP_PASSWORD }}
# Telegram
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
run: | run: |

106
notify.sh
View File

@@ -1,39 +1,91 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -e
# Determine Icon based on Status
ICON="🚀" ICON="🚀"
if [ "$STATUS" = "failure" ]; then if [ "$STATUS" = "failure" ]; then
ICON="❌" ICON="❌"
fi fi
# Prepare Text for Telegram (Markdown)
TEXT="$ICON *$TITLE*%0A%0A$MESSAGE" TEXT="$ICON *$TITLE*%0A%0A$MESSAGE"
# ---- ntfy ---- echo "Starting notifications..."
if [ -n "$NTFY_URL" ] && [ -n "$NTFY_TOPIC" ]; then
curl -s -X POST "$NTFY_URL/$NTFY_TOPIC" \
-H "Title: $ICON $TITLE" \
-H "Priority: 4" \
-d "$MESSAGE"
fi
# ---- Gmail SMTP ---- # Function: Send to Ntfy
if [ -n "$GMAIL_USER" ] && [ -n "$GMAIL_TO" ]; then send_ntfy() {
{ if [ -n "$NTFY_URL" ] && [ -n "$NTFY_TOPIC" ]; then
echo "Subject: $ICON $TITLE" local click_url="$GIT_URL/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
echo "From: $GMAIL_USER" local auth_args=""
echo "To: $GMAIL_TO"
echo "" # Check for Basic Auth
echo "$MESSAGE" if [ -n "$NTFY_USERNAME" ] && [ -n "$NTFY_PASSWORD" ]; then
} | sendmail -S smtp.gmail.com:587 \ auth_args="-u $NTFY_USERNAME:$NTFY_PASSWORD"
-au"$GMAIL_USER" \ fi
-ap"$GMAIL_PASS" \
"$GMAIL_TO"
fi
# ---- Telegram ---- echo "[Ntfy] Sending..."
if [ -n "$TELEGRAM_BOT_TOKEN" ] && [ -n "$TELEGRAM_CHAT_ID" ]; then if curl -s $auth_args -X POST "$NTFY_URL/$NTFY_TOPIC" \
curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \ -H "Title: $ICON $TITLE" \
-d chat_id="$TELEGRAM_CHAT_ID" \ -H "Priority: 4" \
-d parse_mode="Markdown" \ -H "Click: $click_url" \
-d text="$TEXT" -H "Actions: view, Open Logs, $click_url" \
fi -d "$MESSAGE"; then
echo "[Ntfy] Sent."
else
echo "[Ntfy] Failed."
fi
fi
}
# Function: Send to Gmail
send_gmail() {
if [ -n "$GMAIL_USER" ] && [ -n "$GMAIL_TO" ] && [ -n "$GMAIL_PASS" ]; then
if ! command -v sendmail &> /dev/null; then
echo "[Gmail] Warning: sendmail not found, skipping."
return
fi
echo "[Gmail] Sending..."
{
echo "Subject: $ICON $TITLE"
echo "From: $GMAIL_USER"
echo "To: $GMAIL_TO"
echo ""
echo "$MESSAGE"
} | sendmail -S smtp.gmail.com:587 \
-au"$GMAIL_USER" \
-ap"$GMAIL_PASS" \
"$GMAIL_TO" && echo "[Gmail] Sent." || echo "[Gmail] Failed."
fi
}
# Function: Send to Telegram
send_telegram() {
if [ -n "$TELEGRAM_BOT_TOKEN" ] && [ -n "$TELEGRAM_CHAT_ID" ]; then
echo "[Telegram] Sending..."
if curl -s -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-d chat_id="$TELEGRAM_CHAT_ID" \
-d parse_mode="Markdown" \
-d text="$TEXT"; then
echo "[Telegram] Sent."
else
echo "[Telegram] Failed."
fi
fi
}
# Execute in parallel
send_ntfy &
PID_NTFY=$!
send_gmail &
PID_GMAIL=$!
send_telegram &
PID_TG=$!
# Wait for completion
wait $PID_NTFY
wait $PID_GMAIL
wait $PID_TG
echo "All notifications processed."