diff --git a/.github/workflows/test_action.yaml b/.github/workflows/test_action.yaml new file mode 100644 index 0000000..7e4b1c8 --- /dev/null +++ b/.github/workflows/test_action.yaml @@ -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 }} + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..624eeeb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM python:3.10-slim +WORKDIR /app +COPY . /app +RUN python -m pip install requests +ENTRYPOINT ["./entrypoint.sh"] diff --git a/README.md b/README.md index 3f82eec..d1dbd1d 100644 --- a/README.md +++ b/README.md @@ -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. \ No newline at end of file diff --git a/action.yaml b/action.yaml new file mode 100644 index 0000000..12cfcf9 --- /dev/null +++ b/action.yaml @@ -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' \ No newline at end of file diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100755 index 0000000..ddc0645 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,2 @@ +#!/bin/bash +python send_message.py \ No newline at end of file diff --git a/send_message.py b/send_message.py new file mode 100644 index 0000000..a0c66f7 --- /dev/null +++ b/send_message.py @@ -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}: {github_workflow}\n + Repository: {github_repository}''' + + version = get_version(tag) + if version is not None: + message = f'''{message} + Version: {version}''' + + if docker_tags != "": + message = f'''{message} + Docker image tags: {docker_tags}''' + + if include_commit_info == "true": + message = f'''{message} + Author: {github_actor} + Commit message: {commit} + See changes''' + + 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()