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: Publish Docker images | |
on: | |
push: | |
branches: | |
- 'main' | |
- 'master' | |
tags: | |
- '*.*.*' | |
paths: | |
- 'Dockerfile*' | |
- 'pyproject.toml' | |
- 'poetry.lock' | |
- 'requirements.txt' | |
- '**.py' | |
- '**.sh' | |
- '.dockerignore' | |
- '.env.example' | |
- '.github/workflows/**' | |
workflow_dispatch: | |
env: | |
REGISTRY_URL: ${{ vars.REGISTRY_URL }} | |
REGISTRY_USER: ${{ vars.REGISTRY_USER }} | |
jobs: | |
build: | |
name: Build and push Docker image | |
runs-on: ubuntu-latest | |
strategy: | |
fail-fast: false | |
matrix: | |
platform: | |
- linux/amd64 | |
- linux/arm64/v8 | |
steps: | |
- name: Check out the repo | |
uses: actions/checkout@v4 | |
- name: Prepare | |
run: | | |
platform=${{ matrix.platform }} | |
echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV | |
- name: Set password by container registry | |
run: | | |
case "${{ env.REGISTRY_URL }}" in | |
"ghcr.io") | |
echo "REGISTRY_PASS=${{ secrets.GITHUB_TOKEN }}" >> $GITHUB_ENV | |
;; | |
*) | |
if [ -n "${{ secrets.REGISTRY_PASS }}" ]; then | |
echo "REGISTRY_PASS=${{ secrets.REGISTRY_PASS }}" >> $GITHUB_ENV | |
else | |
echo "REGISTRY_PASS secret is not set and registry is not recognized. Exiting..." | |
exit 1 | |
fi | |
;; | |
esac | |
- name: Extract image name from Dockerfile | |
id: image_name | |
run: | | |
IMAGE=$(grep "LABEL org.opencontainers.image.title" Dockerfile | cut -d'"' -f2) | |
echo "IMAGE=$IMAGE" >> $GITHUB_OUTPUT | |
- name: Docker meta | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ steps.image_name.outputs.IMAGE }} | |
tags: | | |
type=raw,value=latest,enable={{is_default_branch}} | |
type=semver,pattern={{version}} | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to container registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY_URL }} | |
username: ${{ env.REGISTRY_USER }} | |
password: ${{ env.REGISTRY_PASS }} | |
- name: Build and push by digest | |
id: build | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
file: ./Dockerfile | |
platforms: ${{ matrix.platform }} | |
labels: ${{ steps.meta.outputs.labels }} | |
outputs: type=image,name=${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ steps.image_name.outputs.IMAGE }},push-by-digest=true,name-canonical=true,push=true | |
- name: Export digest | |
run: | | |
mkdir -p /tmp/digests | |
digest="${{ steps.build.outputs.digest }}" | |
touch "/tmp/digests/${digest#sha256:}" | |
- name: Upload digest | |
uses: actions/upload-artifact@v4 | |
with: | |
name: digests-${{ env.PLATFORM_PAIR }} | |
path: /tmp/digests/* | |
if-no-files-found: error | |
retention-days: 1 | |
merge: | |
name: Merge and push multi-platform image | |
runs-on: ubuntu-latest | |
needs: | |
- build | |
steps: | |
- name: Check out the repo | |
uses: actions/checkout@v4 | |
- name: Set password by container registry | |
run: | | |
case "${{ env.REGISTRY_URL }}" in | |
"ghcr.io") | |
echo "REGISTRY_PASS=${{ secrets.GITHUB_TOKEN }}" >> $GITHUB_ENV | |
;; | |
*) | |
if [ -n "${{ secrets.REGISTRY_PASS }}" ]; then | |
echo "REGISTRY_PASS=${{ secrets.REGISTRY_PASS }}" >> $GITHUB_ENV | |
else | |
echo "REGISTRY_PASS secret is not set and registry is not recognized. Exiting..." | |
exit 1 | |
fi | |
;; | |
esac | |
- name: Download digests | |
uses: actions/download-artifact@v4 | |
with: | |
path: /tmp/digests | |
pattern: digests-* | |
merge-multiple: true | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Extract image name from Dockerfile | |
id: image_name | |
run: | | |
IMAGE=$(grep "LABEL org.opencontainers.image.title" Dockerfile | cut -d'"' -f2) | |
echo "IMAGE=$IMAGE" >> $GITHUB_OUTPUT | |
- name: Docker meta | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ steps.image_name.outputs.IMAGE }} | |
tags: | | |
type=raw,value=latest,enable={{is_default_branch}} | |
type=semver,pattern={{version}} | |
- name: Login to container registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY_URL }} | |
username: ${{ env.REGISTRY_USER }} | |
password: ${{ env.REGISTRY_PASS }} | |
- name: Create manifest list and push | |
working-directory: /tmp/digests | |
run: | | |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "${{ steps.meta.outputs.json }}") \ | |
$(printf '${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ steps.image_name.outputs.IMAGE }}@sha256:%s ' *) | |
- name: Inspect image | |
run: | | |
docker buildx imagetools inspect ${{ env.REGISTRY_URL }}/${{ env.REGISTRY_USER }}/${{ steps.image_name.outputs.IMAGE }}:${{ steps.meta.outputs.version }} |