name: Docker
on:
  schedule:
    - cron: '0 1 * * *'
  push:
    tags:
      - '[0-9]+.[0-9]+.[0-9]+'
  pull_request:
    branches: [ main ]
    paths:
      - 'packaging/docker/**'
      - '.github/workflows/docker.yml'
jobs:
  docker-images:
    name: Docker Images
    if: github.repository_owner == 'miniflux'
    permissions:
      packages: write
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
        with:
          fetch-depth: 0

      - name: Generate Alpine Docker tags
        id: docker_alpine_tags
        uses: docker/metadata-action@80c7e94dd9b9319bd5eb7a0e0fe9291e23a2a2e9 # v6.1.0
        with:
          images: |
            docker.io/${{ github.repository_owner }}/miniflux
            ghcr.io/${{ github.repository_owner }}/miniflux
            quay.io/${{ github.repository_owner }}/miniflux
          tags: |
            type=ref,event=pr
            type=schedule,pattern=nightly
            type=semver,pattern={{raw}}

      - name: Generate Distroless Docker tags
        id: docker_distroless_tags
        uses: docker/metadata-action@80c7e94dd9b9319bd5eb7a0e0fe9291e23a2a2e9 # v6.1.0
        with:
          images: |
            docker.io/${{ github.repository_owner }}/miniflux
            ghcr.io/${{ github.repository_owner }}/miniflux
            quay.io/${{ github.repository_owner }}/miniflux
          tags: |
            type=ref,event=pr
            type=schedule,pattern=nightly
            type=semver,pattern={{raw}}
          flavor: |
            suffix=-distroless,onlatest=true

      - name: Set up QEMU
        uses: docker/setup-qemu-action@06116385d9baf250c9f4dcb4858b16962ea869c3 # v4.1.0

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0

      - name: Login to DockerHub
        if: ${{ github.event_name != 'pull_request' }}
        uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Login to GitHub Container Registry
        if: ${{ github.event_name != 'pull_request' }}
        uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Login to Quay Container Registry
        if: ${{ github.event_name != 'pull_request' }}
        uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0
        with:
          registry: quay.io
          username: ${{ secrets.QUAY_USERNAME }}
          password: ${{ secrets.QUAY_TOKEN }}

      - name: Build and Push Alpine images
        uses: docker/build-push-action@f9f3042f7e2789586610d6e8b85c8f03e5195baf # v7.2.0
        with:
          context: .
          file: ./packaging/docker/alpine/Dockerfile
          platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/riscv64
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.docker_alpine_tags.outputs.tags }}

      - name: Build and Push Distroless images
        uses: docker/build-push-action@f9f3042f7e2789586610d6e8b85c8f03e5195baf # v7.2.0
        with:
          context: .
          file: ./packaging/docker/distroless/Dockerfile
          platforms: linux/amd64,linux/arm64,linux/riscv64
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.docker_distroless_tags.outputs.tags }}
