1
0

first version

This commit is contained in:
yamaks2306 2022-04-22 17:28:20 +03:00
parent ca71061655
commit 3358f0b5ca
6 changed files with 156 additions and 0 deletions

24
.github/workflows/test_action.yaml vendored Normal file

@ -0,0 +1,24 @@
name: Send message
on:
workflow_dispatch:
push:
branches:
- main
tags:
- 'v*'
jobs:
send:
name: Checkout and send message
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Send message
uses: yamaks2306/telegram-notification@main
if: always()
with:
chat_id: ${{ secrets.TG_CHAT_ID }}
token: ${{ secrets.TG_TOKEN }}

5
Dockerfile Normal file

@ -0,0 +1,5 @@
FROM python:3.10-slim
WORKDIR /app
COPY . /app
RUN python -m pip install requests
ENTRYPOINT ["./entrypoint.sh"]

@ -1,2 +1,20 @@
# telegram-notification
Notification to Telegram chat about GitHub Action workflow status with customizable messages
## Usage
To be notified in the Telegram chat about the results of a wokflow, add the next step to the end of your wokflow:
```yaml
- name: Send message
uses: yamaks2306/telegram-notification@main
if: always()
with:
chat_id: ${{ secrets.TG_CHAT_ID }}
token: ${{ secrets.TG_TOKEN }}
```
Where ```chat_id``` is the chat ID and ```token``` is the token of the telegram bot
You can specify additional parameters to customize the messages:
- ```include_commit_info``` - string "true" or "false" ("true" by default). If true, message to Telegram will contain information about commit - author, commit message and link to commit page.
- ```commit_message``` - the default setting is ```github.event.head_commit.message```. ```github.event.commits[0].message``` can be used instead. In the first case, if there were several commits, the message of the last commit will be displayed, in the second - the first one.
- ```docker_tags``` - if the previous step was to build docker images, you can specify docker tags, for example ```steps.docker_meta.outputs.tags```
- ```message``` - custom message, if specified, will be used instead of the standard message.

33
action.yaml Normal file

@ -0,0 +1,33 @@
name: 'GitHub workflow Telegram notification'
description: 'Get notification to Telegram chat about GitHub Action workflow status '
author: 'yamaks2306'
inputs:
chat_id:
description: 'Telegram chat id'
required: true
token:
description: 'Token for Telegram bot'
required: true
status:
description: 'Job status'
required: false
default: ${{ job.status }}
commit_message:
description: 'Commit message'
required: false
default: ${{ github.event.head_commit.message }}
docker_tags:
description: 'For example, steps.docker_meta.outputs.tags'
required: false
include_commit_info:
description: "If true, include commit information to message. Default - true"
required: false
default: 'true'
message:
description: "Custom message"
required: false
runs:
using: 'docker'
image: 'Dockerfile'

2
entrypoint.sh Executable file

@ -0,0 +1,2 @@
#!/bin/bash
python send_message.py

74
send_message.py Normal file

@ -0,0 +1,74 @@
import os
import requests
chat_id = os.getenv('INPUT_CHAT_ID')
if chat_id == "":
raise SystemExit('Variable chat_id is required. Exit.')
token = os.getenv('INPUT_TOKEN')
if token == "":
raise SystemExit('Variable chat_id is required. Exit.')
status = os.getenv('INPUT_STATUS')
commit = os.getenv('INPUT_COMMIT_MESSAGE')
include_commit_info = os.getenv('INPUT_INCLUDE_COMMIT_INFO')
docker_tags = os.getenv('INPUT_DOCKER_TAGS')
custom_message = os.getenv('INPUT_MESSAGE')
link=f'https://api.telegram.org/bot{token}/sendMessage'
icon = {
"failure": "🔴",
"cancelled": "",
"success": "🟢",
}
#GitHub environment variables
github_workflow = os.getenv('GITHUB_WORKFLOW')
github_repository = os.getenv('GITHUB_REPOSITORY')
github_sha = os.getenv('GITHUB_SHA')
github_actor = os.getenv('GITHUB_ACTOR')
tag = os.getenv('GITHUB_REF')
commit_link = f'https://github.com/{github_repository}/commit/{github_sha}'
def get_version(tag_string):
'''GITHUB_REF contain "refs/tags/v0.0.2" or "refs/heads/main". If second part is "tags",
return tag with version number, else - None'''
tags = tag_string.split("/")
return tags[2] if tags[1] == "tags" else None
def build_message():
'''Building message from different parts'''
message = f'''{icon[status]} {status}: <b>{github_workflow}</b>\n
Repository: <b>{github_repository}</b>'''
version = get_version(tag)
if version is not None:
message = f'''{message}
Version: <b>{version}</b>'''
if docker_tags != "":
message = f'''{message}
Docker image tags: <b>{docker_tags}</b>'''
if include_commit_info == "true":
message = f'''{message}
Author: <b>{github_actor}</b>
Commit message: <b>{commit}</b>
<a href="{commit_link}">See changes</a>'''
return message
def send_message():
'''Sending message to telegram chat'''
message = build_message() if custom_message == "" else custom_message
parameters = {
'chat_id': chat_id,
'text': message,
'parse_mode': 'HTML'
}
request = requests.get(link, params = parameters)
request.raise_for_status()
send_message()