feat: testing workflows for telegram-notify.yml #3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Telegram Notifier | ||
| on: | ||
| push: | ||
| branches: ["**"] | ||
| tags: ["*"] | ||
| pull_request: | ||
| types: [opened, reopened, synchronize, closed] | ||
| release: | ||
| types: [published] | ||
| workflow_dispatch: | ||
| inputs: | ||
| text: | ||
| description: "Message to send to Telegram" | ||
| required: true | ||
| chat_id: | ||
| description: "Optional: override TG_CHAT_ID (e.g. @YourChannel or numeric id)" | ||
| required: false | ||
| parse_mode: | ||
| description: "Telegram parse mode (plain|MarkdownV2|HTML)" | ||
| required: false | ||
| default: "plain" | ||
| concurrency: | ||
| group: telegram-${{ github.ref }}-${{ github.event_name }} | ||
| cancel-in-progress: false | ||
| jobs: | ||
| notify: | ||
| runs-on: ubuntu-latest | ||
| environment: SANDBOX | ||
| steps: | ||
| - name: Check out (for commit list) | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
| - name: Build message | ||
| id: msg | ||
| env: | ||
| # Context values (moved to env so the script is pure bash) | ||
| REPO: ${{ github.repository }} | ||
| ACTOR: ${{ github.actor }} | ||
| EVENT: ${{ github.event_name }} | ||
| REF_NAME: ${{ github.ref_name }} | ||
| RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | ||
| REPO_URL: ${{ github.server_url }}/${{ github.repository }} | ||
| GIT_BEFORE: ${{ github.event.before }} | ||
| GIT_SHA: ${{ github.sha }} | ||
| PR_NUMBER: ${{ github.event.pull_request.number }} | ||
| PR_TITLE: ${{ github.event.pull_request.title }} | ||
| PR_URL: ${{ github.event.pull_request.html_url }} | ||
| PR_ACTION: ${{ github.event.action }} | ||
| PR_MERGED: ${{ github.event.pull_request.merged }} | ||
| REL_TAG: ${{ github.event.release.tag_name }} | ||
| REL_NAME: ${{ github.event.release.name }} | ||
| REL_URL: ${{ github.event.release.html_url }} | ||
| DISPATCH_TEXT: ${{ github.event.inputs.text }} | ||
| shell: bash | ||
| run: | | ||
| set -euo pipefail | ||
| msg_header() { | ||
| echo "📣 $1 | ||
| Repo: $REPO | ||
| By: $ACTOR | ||
| Ref: $REF_NAME | ||
| Run: $RUN_URL" | ||
| } | ||
| if [[ "$EVENT" == "workflow_dispatch" ]]; then | ||
| MESSAGE="$(msg_header "Manual notification")"$'\n\n'"${DISPATCH_TEXT:-}" | ||
| elif [[ "$EVENT" == "push" ]]; then | ||
| BEFORE="${GIT_BEFORE:-}" | ||
| AFTER="$GIT_SHA" | ||
| if [[ -z "$BEFORE" || "$BEFORE" =~ ^0+$ ]]; then | ||
| COMMITS="$(git log --pretty=format:'- %s (%h) by %an' "$AFTER" -n 10 || true)" | ||
| else | ||
| COMMITS="$(git log --pretty=format:'- %s (%h) by %an' "$BEFORE..$AFTER" | head -n 10 || true)" | ||
| fi | ||
| [[ -z "${COMMITS:-}" ]] && COMMITS="- (no commit messages found)" | ||
| MESSAGE="$(msg_header "New push detected")"$'\n'"Last changes:"$'\n'"$COMMITS"$'\n\n'"Repo: $REPO_URL" | ||
| elif [[ "$EVENT" == "pull_request" ]]; then | ||
| if [[ "$PR_ACTION" == "closed" ]]; then | ||
| if [[ "$PR_MERGED" == "true" ]]; then | ||
| STATUS="PR merged ✅" | ||
| else | ||
| STATUS="PR closed ❌" | ||
| fi | ||
| else | ||
| STATUS="PR updated ✏️" | ||
| fi | ||
| MESSAGE="$(msg_header "$STATUS")"$'\n'"#${PR_NUMBER}: ${PR_TITLE}"$'\n'"${PR_URL}" | ||
| elif [[ "$EVENT" == "release" ]]; then | ||
| NAME="${REL_NAME:-$REL_TAG}" | ||
| MESSAGE="$(msg_header "Release published 🏷️")"$'\n'"Tag: ${REL_TAG}"$'\n'"Name: ${NAME}"$'\n'"${REL_URL}" | ||
| else | ||
| MESSAGE="$(msg_header "Event: $EVENT")" | ||
| fi | ||
| { | ||
| echo "text<<EOF" | ||
| echo "$MESSAGE" | ||
| echo "EOF" | ||
| } >> "$GITHUB_OUTPUT" | ||
| - name: Send to Telegram | ||
| env: | ||
| TG_BOT_TOKEN: ${{ secrets.TG_BOT_TOKEN }} | ||
| TG_CHAT_ID_DEFAULT: ${{ secrets.TG_CHAT_ID }} | ||
| TEXT: ${{ steps.msg.outputs.text }} | ||
| OVERRIDE_CHAT_ID: ${{ github.event.inputs.chat_id }} | ||
| PARSE_MODE: ${{ github.event.inputs.parse_mode }} | ||
| run: | | ||
| set -euo pipefail | ||
| CHAT_ID="${OVERRIDE_CHAT_ID:-$TG_CHAT_ID_DEFAULT}" | ||
| if [[ -z "$TG_BOT_TOKEN" || -z "$CHAT_ID" ]]; then | ||
| echo "Telegram secrets missing (TG_BOT_TOKEN or TG_CHAT_ID)." >&2 | ||
| exit 1 | ||
| fi | ||
| case "${PARSE_MODE:-plain}" in | ||
| MarkdownV2|HTML) PMODE="$PARSE_MODE" ;; | ||
| *) PMODE="" ;; | ||
| esac | ||
| if [[ -n "$PMODE" ]]; then | ||
| PAYLOAD="$(jq -n --arg chat_id "$CHAT_ID" --arg text "$TEXT" --arg pm "$PMODE" \ | ||
| '{chat_id:$chat_id, text:$text, parse_mode:$pm, disable_web_page_preview:true}')" | ||
| else | ||
| PAYLOAD="$(jq -n --arg chat_id "$CHAT_ID" --arg text "$TEXT" \ | ||
| '{chat_id:$chat_id, text:$text, disable_web_page_preview:true}')" | ||
| fi | ||
| curl -sS -X POST "https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \ | ||
| -H 'Content-Type: application/json' \ | ||
| -d "$PAYLOAD" \ | ||
| | jq -e '.ok == true' >/dev/null | ||
| echo "✅ Message sent to Telegram." | ||