-
Notifications
You must be signed in to change notification settings - Fork 9
111 lines (101 loc) · 3.95 KB
/
rev-tag-notifier.yml
File metadata and controls
111 lines (101 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
name: Increment rev tag and notify Telegram
on:
push:
branches:
- '**'
permissions:
contents: write
jobs:
increment-and-notify:
runs-on: ubuntu-latest
steps:
- name: Checkout pushed ref (full)
uses: actions/checkout@v4
with:
fetch-depth: 0 # needed to list tags and read history
- name: Configure git user
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Find latest rev tag (by ref sort)
id: find_latest
run: |
# Pattern: rev-<number>
latest=$(git tag --list 'rev-*' --sort=-v:refname | head -n1 || true)
if [ -z "$latest" ]; then
echo "::set-output name=latest::"
echo "::set-output name=num::"
else
num=${latest#rev-}
echo "::set-output name=latest::$latest"
echo "::set-output name=num::$num"
fi
- name: Determine next rev number
id: next_rev
run: |
latest="${{ steps.find_latest.outputs.latest }}"
num="${{ steps.find_latest.outputs.num }}"
if [ -z "$latest" ]; then
next=1
else
if echo "$num" | grep -Eq '^[0-9]+$'; then
next=$((num + 1))
else
next=1
fi
fi
tag="rev-${next}"
echo "::set-output name=tag::$tag"
echo "::set-output name=number::$next"
- name: Create and push rev tag
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
tag="${{ steps.next_rev.outputs.tag }}"
echo "Creating tag $tag at $GITHUB_SHA"
git tag "$tag" "$GITHUB_SHA"
git push "https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}.git" "refs/tags/${tag}"
- name: Find latest rev tag (by creation date for notification)
id: latest_for_notify
run: |
# find latest rev-* tag by creation date; leave empty if none
entry=$(git for-each-ref --sort=-creatordate --format='%(refname:short) %(creatordate:unix)' refs/tags | grep '^rev-[0-9]\+' | head -n1 || true)
if [ -z "$entry" ]; then
echo "::set-output name=tag::"
echo "::set-output name=ts::"
else
tag=$(echo "$entry" | awk '{print $1}')
ts=$(echo "$entry" | awk '{print $2}')
echo "::set-output name=tag::$tag"
echo "::set-output name=ts::$ts"
fi
- name: Build Telegram message
id: message
run: |
repo="${GITHUB_REPOSITORY}"
branch="${GITHUB_REF#refs/heads/}"
sha_full="${GITHUB_SHA}"
sha="${sha_full::7}"
author="$(git --no-pager show -s --format='%an' $GITHUB_SHA || echo '')"
msg="$(git --no-pager show -s --format='%s' $GITHUB_SHA || echo '')"
latest_tag="${{ steps.latest_for_notify.outputs.tag }}"
tag_part="Rev: ${latest_tag:-(none)}"
body="Repo: ${repo}%0ABranch: ${branch}%0ACommit: ${sha}%0AAuthor: ${author}%0AMessage: ${msg}%0A${tag_part}"
echo "::set-output name=body::$body"
- name: Send Telegram notification
env:
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
run: |
if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$TELEGRAM_CHAT_ID" ]; then
echo "Missing TELEGRAM_BOT_TOKEN or TELEGRAM_CHAT_ID secrets; skipping send."
exit 0
fi
body="${{ steps.message.outputs.body }}"
curl -sS --fail -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
-d "chat_id=${TELEGRAM_CHAT_ID}" \
-d "text=${body}" \
-d "parse_mode=HTML"
- name: Output created tag
run: |
echo "Created tag: ${{ steps.next_rev.outputs.tag }}"