diff --git a/.github/workflows/build_and_publish_docker.yaml b/.github/workflows/build_and_publish_docker.yaml new file mode 100644 index 0000000..22ed477 --- /dev/null +++ b/.github/workflows/build_and_publish_docker.yaml @@ -0,0 +1,77 @@ +name: Build and Push Docker Image + +on: + release: + types: [created] + workflow_dispatch: + push: + tags: + - '[0-9]+.[0-9]+.[0-9]+-dev' + - '[0-9]+.[0-9]+.[0-9]+-dev.[0-9]+' + - '[0-9]+.[0-9]+.[0-9]+-rc' + - '[0-9]+.[0-9]+.[0-9]+-rc.[0-9]+' + - '[0-9]+.[0-9]+.[0-9]+' + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push: + name: Build and Push Docker Image + runs-on: ubuntu-latest + + permissions: + contents: read + packages: write + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to GitHub Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract tag name + id: vars + run: | + TAG_NAME=${GITHUB_REF#refs/tags/} + IMAGE_NAME_LOWER=$(echo "${GITHUB_REPOSITORY}" | tr '[:upper:]' '[:lower:]') + echo "tag=${TAG_NAME}" >> $GITHUB_OUTPUT + echo "image_name=${IMAGE_NAME_LOWER}" >> $GITHUB_OUTPUT + + if [[ "$TAG_NAME" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "latest=true" >> $GITHUB_OUTPUT + else + echo "latest=false" >> $GITHUB_OUTPUT + fi + + - name: Build and push Docker image + uses: docker/build-push-action@v5 + with: + context: . + file: ./src/docker/Dockerfile + push: true + tags: | + ghcr.io/${{ steps.vars.outputs.image_name }}:${{ steps.vars.outputs.tag }} + ${{ steps.vars.outputs.latest == 'true' && format('ghcr.io/{0}:latest', steps.vars.outputs.image_name) || '' }} + labels: | + org.opencontainers.image.source=${{ github.repositoryUrl }} + org.opencontainers.image.version=${{ steps.vars.outputs.tag }} + org.opencontainers.image.created=${{ github.event.head_commit.timestamp || github.event.release.published_at || github.event.repository.updated_at }} + org.opencontainers.image.revision=${{ github.sha }} + org.opencontainers.image.title=${{ github.repository }} + + - name: Confirm tags pushed + run: | + echo "Published ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.vars.outputs.tag }}" + if [ "${{ steps.vars.outputs.latest }}" = "true" ]; then + echo "Also tagged as :latest" + fi diff --git a/src/docker/Dockerfile b/src/docker/Dockerfile index d7e3c67..8a73246 100644 --- a/src/docker/Dockerfile +++ b/src/docker/Dockerfile @@ -22,6 +22,10 @@ RUN pip install .[docker] # === STAGE 2: Runtime with only necessary files === FROM python:3.12-alpine +ARG IMAGE_TAG=dev +ENV IMAGE_TAG=${IMAGE_TAG} + + ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1