Notify Telegram #8441
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: Notify Telegram | |
| on: | |
| workflow_run: | |
| workflows: ["CI/CD Pipeline"] | |
| types: | |
| - completed | |
| jobs: | |
| post-merge-failure: | |
| name: Send Telegram on post-merge failure | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| actions: read | |
| # נשלח התראה רק על ריצות של push ל-main שנכשלו | |
| # מושבת: הוספנו תנאי false כדי למנוע ריצות (כפילות מול Issues) | |
| if: > | |
| false && | |
| github.event.workflow_run.event == 'push' && | |
| github.event.workflow_run.head_branch == 'main' && | |
| github.event.workflow_run.conclusion != 'success' | |
| steps: | |
| - name: Compute metadata | |
| id: meta | |
| env: | |
| REPO: ${{ github.repository }} | |
| RUN_HTML_URL: ${{ github.event.workflow_run.html_url }} | |
| run: | | |
| started_at="${{ github.event.workflow_run.run_started_at }}" | |
| completed_at="${{ github.event.workflow_run.updated_at }}" | |
| start_ts=$(date -d "$started_at" +%s) | |
| end_ts=$(date -d "$completed_at" +%s) | |
| duration_sec=$(( end_ts - start_ts )) | |
| mins=$(( duration_sec / 60 )) | |
| secs=$(( duration_sec % 60 )) | |
| printf 'duration=%sm %ss\n' "$mins" "$secs" >> "$GITHUB_OUTPUT" | |
| # תאריך/שעה באזור ישראל (כולל שעון קיץ) | |
| ts_fmt=$(TZ="Asia/Jerusalem" date -d "$completed_at" '+%d/%m/%Y %H:%M') | |
| echo "finished_at=${ts_fmt}" >> "$GITHUB_OUTPUT" | |
| repo="${REPO:-}" | |
| run_url="${RUN_HTML_URL:-}" | |
| echo "run_url=${run_url}" >> "$GITHUB_OUTPUT" | |
| echo "repo=${repo}" >> "$GITHUB_OUTPUT" | |
| echo "workflow_name=${{ github.event.workflow_run.name }}" >> "$GITHUB_OUTPUT" | |
| echo "conclusion=${{ github.event.workflow_run.conclusion }}" >> "$GITHUB_OUTPUT" | |
| echo "branch=${{ github.event.workflow_run.head_branch }}" >> "$GITHUB_OUTPUT" | |
| - name: Inspect failed jobs | |
| id: jobs | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| RUN_ID: ${{ github.event.workflow_run.id }} | |
| run: | | |
| set -euo pipefail | |
| reposlug="${GITHUB_REPOSITORY}" | |
| url="https://api.github.com/repos/${reposlug}/actions/runs/${RUN_ID}/jobs?per_page=100" | |
| json=$(curl -fsSL -H "Authorization: Bearer ${GITHUB_TOKEN}" -H "Accept: application/vnd.github+json" "$url") | |
| failed=$(printf '%s' "$json" | jq -c '[.jobs[] | select(.conclusion != "success") | {name:.name, url:.html_url, conclusion:(.conclusion // "unknown")}]') | |
| failed_count=$(printf '%s' "$failed" | jq -r 'length') | |
| failed_names=$(printf '%s' "$failed" | jq -r 'if length>0 then ([.[].name] | join(", ")) else "" end') | |
| failed_links=$(printf '%s' "$failed" | jq -r 'if length>0 then ([.[].url] | join("\n")) else "" end') | |
| printf 'failed_count=%s\n' "$failed_count" >> "$GITHUB_OUTPUT" | |
| printf 'failed_names=%s\n' "$failed_names" >> "$GITHUB_OUTPUT" | |
| { | |
| echo "failed_links<<__TXT__" | |
| printf '%s\n' "$failed_links" | |
| echo "__TXT__" | |
| } >> "$GITHUB_OUTPUT" | |
| - name: Build message | |
| id: msg | |
| env: | |
| CONCLUSION: ${{ steps.meta.outputs.conclusion }} | |
| FINISHED_AT: ${{ steps.meta.outputs.finished_at }} | |
| WORKFLOW_NAME: ${{ steps.meta.outputs.workflow_name }} | |
| DURATION: ${{ steps.meta.outputs.duration }} | |
| RUN_URL: ${{ steps.meta.outputs.run_url }} | |
| BRANCH: ${{ steps.meta.outputs.branch }} | |
| FAILED_COUNT: ${{ steps.jobs.outputs.failed_count }} | |
| FAILED_NAMES: ${{ steps.jobs.outputs.failed_names }} | |
| FAILED_LINKS: ${{ steps.jobs.outputs.failed_links }} | |
| run: | | |
| # אסקייפ ל-MarkdownV2 של טלגרם | |
| esc() { | |
| printf '%s' "$1" | sed -e 's/[\\_\\*\\[\\]\\(\\)~`>#\\+\\-=|{}\\.!]/\\&/g' | |
| } | |
| esc_url() { printf '%s' "$1" | sed -e 's/[()]/\\&/g'; } | |
| title="$(esc "GitHub Actions – כשל אחרי merge ❌")" | |
| time_line="זמן סיום: $(esc "$FINISHED_AT")" | |
| wf_line="Workflow: $(esc "$WORKFLOW_NAME")" | |
| branch_line="סניף: $(esc "$BRANCH")" | |
| dur_line="זמן ריצה: $(esc "$DURATION")" | |
| failed_line="Jobs שנכשלו: $(esc "${FAILED_NAMES:-לא זוהו}")" | |
| link_label_run='צפה בריצה' | |
| link_line="\n\[${link_label_run}\]($(esc_url "$RUN_URL"))" | |
| links_block="" | |
| if [ -n "${FAILED_LINKS:-}" ]; then | |
| # נהפוך כל שורה ללינק עם תבליט | |
| mapfile -t lines <<<"$(printf '%s' "$FAILED_LINKS")" | |
| for l in "${lines[@]}"; do | |
| [ -z "$l" ] && continue | |
| links_block="${links_block}\n\- \[קישור ל\-Job\]($(esc_url "$l"))" | |
| done | |
| fi | |
| message="${title}\n${time_line}\n${wf_line}\n${branch_line}\n${dur_line}\n${failed_line}${link_line}${links_block}" | |
| { | |
| echo "text<<__MSG__" | |
| printf '%b\n' "$message" | |
| echo "__MSG__" | |
| } >> "$GITHUB_OUTPUT" | |
| - name: Send Telegram message | |
| env: | |
| BOT_TOKEN: ${{ secrets.BOT_TOKEN }} | |
| CHAT_ID: ${{ secrets.CHAT_ID }} | |
| TEXT: ${{ steps.msg.outputs.text }} | |
| run: | | |
| if [ -z "${BOT_TOKEN:-}" ] || [ -z "${CHAT_ID:-}" ]; then | |
| echo "BOT_TOKEN or CHAT_ID is missing" >&2 | |
| exit 1 | |
| fi | |
| api_url="https://api.telegram.org/bot${BOT_TOKEN}/sendMessage" | |
| jq -nc --arg chat_id "$CHAT_ID" --arg text "$TEXT" '{chat_id:$chat_id, text:$text, parse_mode:"MarkdownV2", disable_web_page_preview:true}' \ | |
| | curl -sS -X POST "$api_url" -H 'Content-Type: application/json' -d @- | cat | |