---
name: crontab

on:
  workflow_dispatch:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  schedule:
    - cron: '0 1 * * 6'

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.actor }}/crontab

jobs:
  build-crontab:
    runs-on: ubuntu-latest

    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository.
        uses: actions/checkout@v3
        with:
          submodules: true

      - name: Log in to the GitHub Container Registry.
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v2
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GHCR_TOKEN }}

      - name: Extract metadata (tags, labels) for Docker.
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          tags: |
            type=raw,value=latest,enable={{is_default_branch}}
            type=ref,event=branch
            type=ref,event=pr
            type=schedule,pattern={{date 'YYYYMMDD'}}

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

      - name: Set up Docker Buildx.
        uses: docker/setup-buildx-action@v2
        with:
          platforms: linux/amd64

      - name: Build and push Docker image to GitHub Container Registry.
        uses: docker/build-push-action@v4
        with:
          context: .
          platforms: linux/amd64
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          provenance: true
          sbom: true

      - name: Send notification to Discord.
        uses: sarisia/actions-status-discord@v1.12.0
        if: always()
        with:
          webhook: ${{ secrets.DISCORD_WEBHOOK }}