---
on:
  schedule:
    - cron: '0 4 * * *' # Every day at 4:00am
  push:
    tags:
      - '*'

name: Publish tagged images to Docker Hub

jobs:
  docker:
    runs-on: docker
    steps:
      - uses: actions/checkout@v3

      # Check if the tag has the v<nmumber>.<number>.<number> format. If yes, it means we are publishing an official release.
      # In this situation, we need to set `output.stable` to create/update the following tags (additionally to the `vX.Y.Z` Docker tag):
      # - a `vX.Y` (without patch version) Docker tag
      # - a `latest` Docker tag
      - name: Check tag format
        if: github.event_name != 'schedule'
        id: check-tag-format
        run: |
          escaped_tag=$(printf "%q" ${{ github.ref_name }})

          if [[ $escaped_tag =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
            echo ::set-output name=stable::true
          else
            echo ::set-output name=stable::false
          fi

      # Check only the validity of the tag for official releases (not for pre-releases or other tags)
      - name: Check release validity
        if: github.event_name != 'schedule' && steps.check-tag-format.outputs.stable == 'true'
        run: bash .github/scripts/check-release.sh

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

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

      - name: Login to Docker Hub
        if: github.event_name != 'schedule'
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: getmeili/meilisearch
          # The latest and `vX.Y` tags are only pushed for the official Meilisearch releases
          # See https://github.com/docker/metadata-action#latest-tag
          flavor: latest=false
          tags: |
            type=ref,event=tag
            type=semver,pattern=v{{major}}.{{minor}},enable=${{ steps.check-tag-format.outputs.stable == 'true' }}
            type=raw,value=latest,enable=${{ steps.check-tag-format.outputs.stable == 'true' }}

      - name: Build and push
        uses: docker/build-push-action@v3
        with:
          # We do not push tags for the cron jobs, this is only for test purposes
          push: ${{ github.event_name != 'schedule' }}
          platforms: linux/amd64,linux/arm64
          tags: ${{ steps.meta.outputs.tags }}

      # /!\ Don't touch this without checking with Cloud team
      - name: Send CI information to Cloud team
        if: github.event_name != 'schedule'
        uses: peter-evans/repository-dispatch@v2
        with:
          token: ${{ secrets.MEILI_BOT_GH_PAT }}
          repository: meilisearch/meilisearch-cloud
          event-type: cloud-docker-build
          client-payload: '{ "meilisearch_version": "${{ github.ref_name }}", "stable": "${{ steps.check-tag-format.outputs.stable }}" }'