name: Docker CI

on:
  push:
    branches:
      - main

env:
  node-version: 22.x

jobs:
  release:
    runs-on: ubuntu-latest
    container:
      image: catthehacker/ubuntu:act-latest
    env:
      DOCKER_ORG: nzambello
      DOCKER_LATEST: nightly
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            code.nzambello.dev/nzambello/work-timer
          labels: |
            org.label-schema.docker.cmd=docker run -d -p 4000:4000 code.nzambello.dev/nzambello/work-timer:latest
          flavor: latest=false
          tags: |
            type=ref,event=branch
            type=sha
            type=raw,value=latest,enable={{is_default_branch}}

      - name: Login to Container Registry
        uses: docker/login-action@v2
        with:
          registry: code.nzambello.dev
          username: ${{ gitea.repository_owner }}
          password: ${{ secrets.ACTIONS_TOKEN }}

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          file: ./Dockerfile
          platforms: linux/amd64,linux/arm64
          push: ${{ gitea.event_name != 'pull_request' }}
          tags: |
            code.nzambello.dev/nzambello/work-timer:latest
          labels: $${{ steps.meta.outputs.labels }}

      - name: Deploy
        uses: darnfish/watchtower-update@v3.2
        with:
          url: https://watchtower.nzambello.dev/v1/update
          api_token: '${{ secrets.WATCHTOWER_API_TOKEN }}'
          images: code.nzambello.dev/nzambello/work-timer
