From 30483e1f94912585a66d88de7c3dda96a02a1396 Mon Sep 17 00:00:00 2001 From: Yuri V Date: Wed, 30 Oct 2024 17:19:30 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20Update=20README=20and=20action.y?= =?UTF-8?q?ml=20for=20build-and-push-docker-image-action?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔧 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 --- README.md | 81 +++++++++++++++++++++++++++++++++++++++++++++++++- action.yml | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 163 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7659699..78afd7e 100644 --- a/README.md +++ b/README.md @@ -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-val) + +## License + +This project is licensed under the [MIT License](LICENSE). diff --git a/action.yml b/action.yml index b1ca1d5..ba59123 100644 --- a/action.yml +++ b/action.yml @@ -6,7 +6,7 @@ author: 'Yuri V (@yuri-val)' inputs: telegram_to: - description: 'Telegram recipient ID' + description: 'Telegram recipient/chat ID' required: true telegram_token: description: 'Telegram bot token' @@ -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: "💬 STARTED: build
${{ github.repository }}
" + + - 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: "✅ SUCCESSFUL: built
${{ github.repository }}
version
${{ steps.date.outputs.date }}
" + + - 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: "❌ FAIL: build failed for
${{ github.repository }}
"