Skip to content

feat: testing workflows for telegram commits #17

feat: testing workflows for telegram commits

feat: testing workflows for telegram commits #17

name: Telegram – Last 10 Commits (Clean Text)
on:
push:
branches: ["**"]
permissions:
contents: read
jobs:
send:
runs-on: ubuntu-latest
environment: SANDBOX # Must contain TG_BOT_TOKEN and TG_CHAT_ID
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 50 # Fetch enough commits for recent history
- name: Build commit message (plain text, IST timestamps)
id: msg
shell: bash
env:
REPO: ${{ github.repository }}
ACTOR: ${{ github.actor }}
REF_NAME: ${{ github.ref_name }}
REPO_URL: ${{ github.server_url }}/${{ github.repository }}
SITE_URL: https://someshdiwan.github.io/JavaEvolution-Learning-Growing-Mastering/
COMMIT_COUNT: 10 # Number of commits to fetch
run: |
set -euo pipefail
# Validate environment variables
if [[ -z "${REPO:-}" || -z "${ACTOR:-}" || -z "${REF_NAME:-}" || -z "${REPO_URL:-}" ]]; then
echo "Error: Missing required GitHub environment variables"
exit 1
fi
# Fetch last N commits with subject | short hash | IST datetime
mapfile -t COMMITS < <(
TZ=Asia/Kolkata \
git log -n "$COMMIT_COUNT" \
--date=format-local:'%d %b %Y, %H:%M IST' \
--pretty=format:'%s | %h | %ad' 2>/dev/null || true
)
# Format commit message
if [[ ${#COMMITS[@]} -eq 0 ]]; then
COMMIT_MESSAGE="- No recent commits found"
else
COMMIT_MESSAGE=""
for row in "${COMMITS[@]}"; do
IFS='|' read -r subj short when <<<"$row"
COMMIT_MESSAGE+="- ${subj//|/} (${short}) — ${when}"$'\n\n'
done
COMMIT_MESSAGE="${COMMIT_MESSAGE%$'\n\n'}" # Trim trailing newlines
fi
# Construct Telegram message
{
printf 'text<<MSGEOF\n'
printf '📣 Recent Commits (%s)\n' "$COMMIT_COUNT"
printf 'Site: %s\n' "$SITE_URL"
printf 'Repository: %s\n' "$REPO"
printf 'Branch: %s\n' "$REF_NAME"
printf 'Pushed by: %s\n' "$ACTOR"
printf '\n%s\n' "$COMMIT_MESSAGE"
printf '\n🔗 Repo: %s\n' "$REPO_URL"
printf 'MSGEOF\n'
} >> "$GITHUB_OUTPUT"
- name: Send to Telegram (plain text with retry)
shell: bash
env:
TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }}
TG_CHAT_ID: ${{ secrets.TG_CHAT_ID }}
TEXT: ${{ steps.msg.outputs.text }}
MAX_RETRIES: 3
RETRY_DELAY: 5
run: |
set -euo pipefail
# Validate Telegram secrets
if [[ -z "${TG_BOT_TOKEN:-}" || -z "${TG_CHAT_ID:-}" ]]; then
echo "Error: Missing Telegram bot token or chat ID"
exit 1
fi
# Send message with retry logic
for ((i=1; i<=MAX_RETRIES; i++)); do
if curl -sS -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \
-H 'Content-Type: application/json' \
-d "$(jq -n \
--arg chat_id "$TG_CHAT_ID" \
--arg text "$TEXT" \
'{chat_id:$chat_id, text:$text, disable_web_page_preview:true}')" \
| jq -e '.ok == true' >/dev/null; then
echo "✅ Successfully posted to Telegram (attempt $i)"
exit 0
else
echo "⚠️ Telegram API request failed (attempt $i)"
[[ $i -lt $MAX_RETRIES ]] && sleep "$RETRY_DELAY"
fi
done
echo "❌ Failed to post to Telegram after $MAX_RETRIES attempts"
exit 1