Automated Docker Build and Push #81
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Automated Docker Build and Push | |
on: | |
push: | |
tags: | |
- 'v*' | |
workflow_dispatch: | |
inputs: | |
tag_name: | |
description: 'Tag Name (Optional for manual trigger)' | |
required: false | |
default: '' | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
permissions: | |
packages: write | |
contents: read | |
actions: write # Permission to create releases | |
steps: | |
- name: Check Out Repo | |
uses: actions/checkout@v3 | |
- name: Log in to GitHub Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Determine Tag Name | |
id: tag_name | |
run: | | |
if [ "${{ github.event_name }}" == "workflow_dispatch" ] && [ "${{ github.event.inputs.tag_name }}" != "" ]; then | |
echo "TAG_NAME=${{ github.event.inputs.tag_name }}" >> $GITHUB_ENV | |
elif [ "${GITHUB_REF#refs/tags/}" != "" ]; then | |
echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV | |
else | |
echo "Error: Tag name not provided." | |
exit 1 | |
fi | |
- name: Build and Push Docker image | |
uses: docker/build-push-action@v3 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
tags: | | |
ghcr.io/h0llyw00dzz/gogenai-terminal-chat:${{ env.TAG_NAME }} | |
ghcr.io/h0llyw00dzz/gogenai-terminal-chat:latest | |
- name: Create checksum.txt file | |
run: | | |
IMAGE_NAME=ghcr.io/h0llyw00dzz/gogenai-terminal-chat:${{ env.TAG_NAME }} | |
CHECKSUM=$(docker inspect --format='{{.Id}}' "$IMAGE_NAME") | |
echo "$CHECKSUM" > checksum.txt | |
- name: Get the release ID (Assuming the release exists) | |
id: get_release | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const release = await github.rest.repos.getReleaseByTag({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
tag: '${{ env.TAG_NAME }}' | |
}); | |
if (!release.data.id) { | |
throw new Error('Release not found'); | |
} | |
console.log(`Found release ID: ${release.data.id}`); | |
return release.data.id; | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Upload checksum.txt as release asset | |
run: | | |
curl \ | |
-X POST \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Content-Type: application/octet-stream" \ | |
--data-binary @checksum.txt \ | |
$(curl -s \ | |
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
"https://api.github.com/repos/${{ github.repository }}/releases/${{ steps.get_release.outputs.result }}" \ | |
| jq -r .upload_url | sed "s/{?name,label}//")?name=checksum.txt | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
- name: Clean up Buildx builder | |
if: always() | |
run: docker buildx rm |