name: Publish Docker image
on:
  push:
    branches-ignore:
      - dependabot/**
    tags:
      - '**'
    paths-ignore:
      - 'docs/**'
      - '**.md'
      - '**.sample'

jobs:
  push:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest
    steps:
      - name: Check out the repo
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v2
      - name: Log in to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
      - name: Set Docker meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ secrets.DOCKER_USERNAME }}/rss-to-telegram
          flavor: latest=false
          tags: |
            type=ref,event=branch
            type=pep440,pattern={{version}}
            type=pep440,pattern={{major}}.{{minor}}
            type=pep440,pattern={{major}}
            type=raw,value=latest,enable=${{ endsWith(github.ref, 'master') }}
      - name: Push to Docker Hub
        uses: docker/build-push-action@v3
        with:
          push: true
          context: .
          platforms: linux/amd64,linux/arm64
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha,scope=docker-release
          cache-to: type=gha,scope=docker-release,mode=max
      - name: Set Docker meta (exp-deps)
        if: ${{ endsWith(github.ref, 'dev') }}
        id: meta-fpe
        uses: docker/metadata-action@v4
        with:
          images: ${{ secrets.DOCKER_USERNAME }}/rss-to-telegram
          flavor: latest=false
          tags: type=raw,value=exp-deps,enable=true
      - name: Push to Docker Hub (exp-deps)
        if: ${{ endsWith(github.ref, 'dev') }}
        uses: docker/build-push-action@v3
        with:
          push: true
          context: .
          build-args: EXP_DEPS=1
          platforms: linux/amd64,linux/arm64
          tags: ${{ steps.meta-fpe.outputs.tags }}
          labels: ${{ steps.meta-fpe.outputs.labels }}
          cache-from: type=registry,ref=${{ secrets.DOCKER_USERNAME }}/rss-to-telegram:exp-deps
          cache-to: type=inline,ref=${{ secrets.DOCKER_USERNAME }}/rss-to-telegram:exp-deps

  description:
    name: Update repository description on Docker Hub
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Update Docker Hub description
        uses: peter-evans/dockerhub-description@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}
          repository: ${{ secrets.DOCKER_USERNAME }}/rss-to-telegram
