Skip to content

Commit

Permalink
Add Docker build CI (#5)
Browse files Browse the repository at this point in the history
* Add Docker build CI

* build on main branch and release only

* always push

* build on pull request
  • Loading branch information
aoirint authored Aug 8, 2024
1 parent 720cdb5 commit f825143
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 3 deletions.
118 changes: 118 additions & 0 deletions .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
name: Build Docker

on:
push:
branches:
- 'main'
tags:
- 'v*'
pull_request:
branches:
- 'main'
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=pr
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=latest,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=raw,value=latest,suffix=-buildcache,enable={{is_default_branch}}
- 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)
if: github.event_name != 'pull_request'
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_announce_image_downloader/__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: ${{ github.event_name != 'pull_request' }}
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 }}
2 changes: 1 addition & 1 deletion amaterus_announce_image_downloader/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__VERSION__ = "0.1.0"
__version__ = "0.0.0"
2 changes: 1 addition & 1 deletion amaterus_announce_image_downloader/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from dotenv import load_dotenv

from . import __VERSION__ as APP_VERSION
from . import __version__ as APP_VERSION
from .app_config import load_app_config_from_env
from .twitter_tweet_image_cli import add_twitter_tweet_image_arguments
from .youtube_live_thumbnail_image_cli import add_youtube_live_thumbnail_image_arguments
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ select = [

[tool.poetry]
name = "amaterus-announce-image-downloader"
version = "0.1.0"
version = "0.0.0"
description = ""
authors = ["aoirint <aoirint@gmail.com>"]
readme = "README.md"
Expand Down

0 comments on commit f825143

Please sign in to comment.