@@ -10,14 +10,14 @@ permissions:
1010jobs :
1111 send :
1212 runs-on : ubuntu-latest
13- environment : SANDBOX # Must contain TG_BOT_TOKEN and TG_CHAT_ID
13+ environment : SANDBOX # Must contain TG_BOT_TOKEN and TG_CHAT_ID
1414 steps :
15- - name : Checkout
15+ - name : Checkout repository
1616 uses : actions/checkout@v4
1717 with :
18- fetch-depth : 50 # enough for recent history
18+ fetch-depth : 50 # Fetch enough commits for recent history
1919
20- - name : Build message (plain text, IST timestamps)
20+ - name : Build commit message (plain text, IST timestamps)
2121 id : msg
2222 shell : bash
2323 env :
@@ -26,62 +26,82 @@ jobs:
2626 REF_NAME : ${{ github.ref_name }}
2727 REPO_URL : ${{ github.server_url }}/${{ github.repository }}
2828 SITE_URL : https://someshdiwan.github.io/JavaEvolution-Learning-Growing-Mastering/
29+ COMMIT_COUNT : 10 # Number of commits to fetch
2930 run : |
3031 set -euo pipefail
3132
32- # Pull last 10 commits with subject | short hash | IST datetime
33- # Use Git's format-local and force runner TZ to Asia/Kolkata
34- mapfile -t LINES < <(
33+ # Validate environment variables
34+ if [[ -z "${REPO:-}" || -z "${ACTOR:-}" || -z "${REF_NAME:-}" || -z "${REPO_URL:-}" ]]; then
35+ echo "Error: Missing required GitHub environment variables"
36+ exit 1
37+ fi
38+
39+ # Fetch last N commits with subject | short hash | IST datetime
40+ mapfile -t COMMITS < <(
3541 TZ=Asia/Kolkata \
36- git log -n 10 \
42+ git log -n "$COMMIT_COUNT" \
3743 --date=format-local:'%d %b %Y, %H:%M IST' \
38- --pretty=format:'%s%x1f%h%x1f% ad' || true
44+ --pretty=format:'%s | %h | % ad' 2>/dev/null || true
3945 )
4046
41- if [[ ${#LINES[@]} -eq 0 ]]; then
42- COMMITS="- (no commit messages found)"
47+ # Format commit message
48+ if [[ ${#COMMITS[@]} -eq 0 ]]; then
49+ COMMIT_MESSAGE="- No recent commits found"
4350 else
44- COMMITS=""
45- for row in "${LINES[@]}"; do
46- IFS=$'\x1f' read -r subj short when <<<"$row"
47- # bullet + blank line after each item
48- COMMITS+="- ${subj} (${short}) — ${when}"$'\n\n'
51+ COMMIT_MESSAGE=""
52+ for row in "${COMMITS[@]}"; do
53+ IFS='|' read -r subj short when <<<"$row"
54+ COMMIT_MESSAGE+="- ${subj//|/} (${short}) — ${when}"$'\n\n'
4955 done
50- # trim final blank line
51- COMMITS="${COMMITS%$'\n\n'}"
56+ COMMIT_MESSAGE="${COMMIT_MESSAGE%$'\n\n'}" # Trim trailing newlines
5257 fi
5358
59+ # Construct Telegram message
5460 {
5561 printf 'text<<MSGEOF\n'
56- printf '📣 Last 10 commits \n'
62+ printf '📣 Recent Commits (%s) \n' "$COMMIT_COUNT"
5763 printf 'Site: %s\n' "$SITE_URL"
58- printf 'Repo : %s\n' "$REPO"
64+ printf 'Repository : %s\n' "$REPO"
5965 printf 'Branch: %s\n' "$REF_NAME"
60- printf 'By: %s\n' "$ACTOR"
61- printf '\n'
62- printf '%s\n' "$COMMITS"
63- printf '\n'
64- printf '🔗 Repo: %s\n' "$REPO_URL"
66+ printf 'Pushed by: %s\n' "$ACTOR"
67+ printf '\n%s\n' "$COMMIT_MESSAGE"
68+ printf '\n🔗 Repo: %s\n' "$REPO_URL"
6569 printf 'MSGEOF\n'
6670 } >> "$GITHUB_OUTPUT"
6771
68- - name : Send to Telegram (plain text)
72+ - name : Send to Telegram (plain text with retry )
6973 shell : bash
7074 env :
7175 TG_BOT_TOKEN : ${{ secrets.TG_BOT_TOKEN }}
7276 TG_CHAT_ID : ${{ secrets.TG_CHAT_ID }}
7377 TEXT : ${{ steps.msg.outputs.text }}
78+ MAX_RETRIES : 3
79+ RETRY_DELAY : 5
7480 run : |
7581 set -euo pipefail
76- [[ -n "${TG_BOT_TOKEN:-}" && -n "${TG_CHAT_ID:-}" ]] || { echo "Missing TG secrets"; exit 1; }
7782
78- # Plain text: keeps your newlines and spacing as-is
79- curl -sS -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \
80- -H 'Content-Type: application/json' \
81- -d "$(jq -n \
82- --arg chat_id "$TG_CHAT_ID" \
83- --arg text "$TEXT" \
84- '{chat_id:$chat_id, text:$text, disable_web_page_preview:true}')" \
85- | jq -e '.ok == true' >/dev/null
83+ # Validate Telegram secrets
84+ if [[ -z "${TG_BOT_TOKEN:-}" || -z "${TG_CHAT_ID:-}" ]]; then
85+ echo "Error: Missing Telegram bot token or chat ID"
86+ exit 1
87+ fi
88+
89+ # Send message with retry logic
90+ for ((i=1; i<=MAX_RETRIES; i++)); do
91+ if curl -sS -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \
92+ -H 'Content-Type: application/json' \
93+ -d "$(jq -n \
94+ --arg chat_id "$TG_CHAT_ID" \
95+ --arg text "$TEXT" \
96+ '{chat_id:$chat_id, text:$text, disable_web_page_preview:true}')" \
97+ | jq -e '.ok == true' >/dev/null; then
98+ echo "✅ Successfully posted to Telegram (attempt $i)"
99+ exit 0
100+ else
101+ echo "⚠️ Telegram API request failed (attempt $i)"
102+ [[ $i -lt $MAX_RETRIES ]] && sleep "$RETRY_DELAY"
103+ fi
104+ done
86105
87- echo "✅ Posted last 10 commits to Telegram."
106+ echo "❌ Failed to post to Telegram after $MAX_RETRIES attempts"
107+ exit 1
0 commit comments