Skip to content

Commit

Permalink
📦 Update README and action.yml for build-and-push-docker-image-action
Browse files Browse the repository at this point in the history
🔧 Refactored the README to provide comprehensive documentation:
- Added detailed description of the action's features
- Included usage instructions with YAML example
- Listed required inputs and secrets
- Explained the workflow process

🚀 Enhanced action.yml with new functionality:
- Implemented Docker image building and pushing
- Added GitHub release creation with auto-generated changelog
- Integrated Telegram notifications for start, success, and failure events
- Improved input descriptions and removed default value for docker_repo_name
  • Loading branch information
yuri-val committed Oct 30, 2024
1 parent 8f41eee commit 30483e1
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 4 deletions.
81 changes: 80 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,80 @@
# build-and-push-docker-image-action
# Build and Push Docker Image Action

This GitHub Action builds a Docker image, pushes it to Docker Hub, creates a GitHub release, and sends Telegram notifications throughout the process.

## Features

- Builds Docker image from your repository
- Pushes the image to Docker Hub with date-based and 'latest' tags
- Creates a GitHub release with an auto-generated changelog
- Sends Telegram notifications for start, success, and failure events

## Inputs

| Input | Description | Required |
|-------|-------------|----------|
| `telegram_to` | Telegram recipient/chat ID | Yes |
| `telegram_token` | Telegram bot token | Yes |
| `docker_hub_username` | Docker Hub username | Yes |
| `docker_hub_access_token` | Docker Hub access token | Yes |
| `docker_repo_name` | Docker Hub repository name | Yes |

## Usage

To use this action in your workflow, create a `.github/workflows/docker-build-push.yml` file in your repository with the following content:

```yaml
name: Build and Push Docker Image

on:
push:
branches:
- main # or any branch you want to trigger the action

jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Build and Push Docker Image
uses: yuri-val/build-and-push-docker-image-action@v1
with:
telegram_to: ${{ secrets.TELEGRAM_TO }}
telegram_token: ${{ secrets.TELEGRAM_TOKEN }}
docker_hub_username: ${{ secrets.DOCKER_HUB_USERNAME }}
docker_hub_access_token: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
docker_repo_name: your-repo-name
```
Make sure to set up the following secrets in your repository:
- `TELEGRAM_TO`: Your Telegram recipient/chat ID
- `TELEGRAM_TOKEN`: Your Telegram bot token
- `DOCKER_HUB_USERNAME`: Your Docker Hub username
- `DOCKER_HUB_ACCESS_TOKEN`: Your Docker Hub access token

## Requirements

- Your repository must contain a `Dockerfile` in the root directory.
- You need to have a Docker Hub account and create an access token.
- You need to create a Telegram bot and obtain its token.

## How it works

1. The action checks out your repository.
2. It logs in to Docker Hub using the provided credentials.
3. The Docker image is built and pushed to Docker Hub with two tags:
- A date-based tag (e.g., `23.05.15.1234`)
- The `latest` tag
4. A new GitHub release is created with an auto-generated changelog.
5. Telegram notifications are sent at the start of the process, on successful completion, and in case of failure.

## Author

Yuri V <yuri.valigursky@gmail.com> (@yuri-val)

## License

This project is licensed under the [MIT License](LICENSE).
86 changes: 83 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ author: 'Yuri V <yuri.valigursky@gmail.com> (@yuri-val)'

inputs:
telegram_to:
description: 'Telegram recipient ID'
description: 'Telegram recipient/chat ID'
required: true
telegram_token:
description: 'Telegram bot token'
Expand All @@ -20,9 +20,89 @@ inputs:
docker_repo_name:
description: 'Docker Hub repository name'
required: true
default: 'shipvisor'

runs:
using: 'composite'
steps:
# Steps will be defined in the next section
- name: Extract repository name
shell: bash
run: echo "REPO_NAME=$(echo ${{ github.repository }} | cut -d'/' -f2)" >> $GITHUB_ENV

- name: Send Telegram Message on Start
uses: appleboy/telegram-action@master
continue-on-error: true
with:
to: ${{ inputs.telegram_to }}
token: ${{ inputs.telegram_token }}
format: html
message: "💬 <b>STARTED</b>: build <pre>${{ github.repository }}</pre>"

- name: Get Current Date
id: date
run: echo "date=$(date +'%y.%m.%d.%H%M')" >> $GITHUB_OUTPUT

- name: Checkout Repository
uses: actions/checkout@v4

- name: Get Current Commit SHA
id: get_sha
run: echo "sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT

- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ inputs.docker_hub_username }}
password: ${{ inputs.docker_hub_access_token }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and Push Docker Image
uses: docker/build-push-action@v5
with:
context: ./
file: ./Dockerfile
push: true
tags: |
${{ inputs.docker_hub_username }}/${{ inputs.docker_repo_name }}:${{ steps.date.outputs.date }}
${{ inputs.docker_hub_username }}/${{ inputs.docker_repo_name }}:latest
build-args: |
COMMIT_SHA=${{ steps.get_sha.outputs.sha }}
TAG_VERSION=${{ steps.date.outputs.date }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache

- name: Bump Version and Push Tag
id: tag_version
uses: mathieudutour/github-tag-action@v6.1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
custom_tag: ${{ steps.date.outputs.date }}

- name: Create a GitHub Release
uses: ncipollo/release-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
tag: ${{ steps.date.outputs.date }}
name: Build ${{ steps.date.outputs.date }}
body: ${{ steps.tag_version.outputs.changelog }}

- name: Send Telegram Message on Success
if: ${{ success() }}
uses: appleboy/telegram-action@master
continue-on-error: true
with:
to: ${{ inputs.telegram_to }}
token: ${{ inputs.telegram_token }}
format: html
message: "✅ <b>SUCCESSFUL</b>: built <pre>${{ github.repository }}</pre> version <pre>${{ steps.date.outputs.date }}</pre>"

- name: Send Telegram Message on Failure
if: ${{ failure() }}
uses: appleboy/telegram-action@master
continue-on-error: true
with:
to: ${{ inputs.telegram_to }}
token: ${{ inputs.telegram_token }}
format: html
message: "❌ <b>FAIL</b>: build failed for <pre>${{ github.repository }}</pre>"

0 comments on commit 30483e1

Please sign in to comment.