Skip to content

Docker Build and Publish #12

Docker Build and Publish

Docker Build and Publish #12

name: Docker Build and Publish
on:
workflow_run:
workflows: ["Release Please"]
types:
- completed
push:
branches: [ main ]
tags:
- 'v*'
pull_request:
branches: [ main ]
env:
REGISTRY: ghcr.io
IMAGE_NAME: echohello-dev/prometheus-toggl-track-exporter
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
outputs:
is_release: ${{ steps.check-release.outputs.release_found }}
release_tag: ${{ steps.check-release.outputs.release_tag }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check for release
id: check-release
run: |
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "no-tag")
if [[ $LATEST_TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Found release tag: $LATEST_TAG"
echo "release_found=true" >> $GITHUB_OUTPUT
echo "release_tag=$LATEST_TAG" >> $GITHUB_OUTPUT
else
echo "No release tag found"
echo "release_found=false" >> $GITHUB_OUTPUT
fi
- name: Cache asdf
uses: actions/cache@v4
id: asdf-cache
with:
path: ~/.asdf
key: ${{ runner.os }}-asdf-${{ hashFiles('.tool-versions') }}
restore-keys: |
${{ runner.os }}-asdf-
- name: Cache Poetry
uses: actions/cache@v4
id: poetry-cache
with:
path: ~/.cache/pypoetry
key: ${{ runner.os }}-poetry-${{ hashFiles('**/poetry.lock') }}
restore-keys: |
${{ runner.os }}-poetry-
- name: Install asdf
uses: asdf-vm/actions/install@1117842ea70e2711a0072e3a71265cbfe2c830be # v0.16
- name: Setup asdf plugins
run: |
asdf plugin add python
asdf plugin add poetry
asdf plugin add task
- name: Install tools with asdf
run: |
asdf install
pip install poetry
asdf reshim python
- name: Install dependencies
run: task install
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=latest,enable=${{ steps.check-release.outputs.release_found == 'true' }}
type=raw,value=${{ steps.check-release.outputs.release_tag }},enable=${{ steps.check-release.outputs.release_found == 'true' }}
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,format=long
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max