Skip to content

Add Docker Build CI (#20) #6

Add Docker Build CI (#20)

Add Docker Build CI (#20) #6

Workflow file for this run

name: Build Docker
on:
push:
branches:
- 'main'
tags:
- 'v*'
workflow_dispatch:
permissions:
contents: read
packages: write
id-token: write
jobs:
docker-build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Setup Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v3
- name: Generate Docker metadata for ghcr.io
id: meta-ghcr
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=ref,event=branch,suffix=-{{sha}}
type=ref,event=branch
type=pep440,pattern={{version}}
type=pep440,pattern={{major}}
type=pep440,pattern={{major}}.{{minor}}
type=raw,value=latest,enable={{is_default_branch}}
- name: Generate Docker build cache source metadata
id: meta-buildcache-source
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=raw,value=main-buildcache,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
type=ref,event=branch,suffix=-buildcache
- name: Generate Docker build cache destination metadata
id: meta-buildcache-destination
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=ref,event=branch,suffix=-buildcache
- name: Generate Docker build cache metadata vars
id: meta-buildcache-vars
shell: bash
env:
DOCKER_METADATA_BUILDCACHE_SOURCE_OUTPUT_JSON: ${{ steps.meta-buildcache-source.outputs.json }}
DOCKER_METADATA_BUILDCACHE_DESTINATION_OUTPUT_JSON: ${{ steps.meta-buildcache-destination.outputs.json }}
run: |
{
echo "cache-from<<EOF"
for tag in $(jq -r '.tags[]' <<< "$DOCKER_METADATA_BUILDCACHE_SOURCE_OUTPUT_JSON"); do
echo "type=registry,ref=${tag}"
done
echo "EOF"
} >> $GITHUB_OUTPUT
{
echo "cache-to<<EOF"
for tag in $(jq -r '.tags[]' <<< "$DOCKER_METADATA_BUILDCACHE_DESTINATION_OUTPUT_JSON"); do
echo "type=registry,ref=${tag},mode=max"
done
echo "EOF"
} >> $GITHUB_OUTPUT
- name: Login to GitHub Container Registry (ghcr.io)
uses: docker/login-action@v3
with:
registry: 'ghcr.io'
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Replace Version
shell: bash
if: startsWith(github.ref, 'refs/tags/v')
run: |
TAG="${{ github.ref_name }}"
# strip first char 'v'
VERSION=${TAG:1}
sed -i "s/__version__ = \"0.0.0\"/__version__ = \"${VERSION}\"/" amaterus_admin_gradio/__init__.py
sed -i "s/version = \"0.0.0\"/version = \"${VERSION}\"/" pyproject.toml
- name: Build and Deploy Docker image
uses: docker/build-push-action@v5
with:
builder: ${{ steps.buildx.outputs.name }}
context: .
file: ./Dockerfile
push: true
platforms: |
linux/amd64
linux/arm64
tags: |
${{ steps.meta-ghcr.outputs.tags }}
cache-from: ${{ steps.meta-buildcache-vars.outputs.cache-from }}
cache-to: ${{ steps.meta-buildcache-vars.outputs.cache-to }}
labels: ${{ steps.meta-ghcr.outputs.labels }}
annotations: ${{ steps.meta-ghcr.outputs.annotations }}