name: Docker

on:
  schedule:
    - cron: "20 6 * * *"
  push:
    branches: [master]
  pull_request:
    branches: [master]
  workflow_dispatch:
env:
  BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
  TIMEZONE: Europe/Stockholm
  LOG_LEVEL: Info
  SQLITE_LOCATION: /data/jobs.sqlite
  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - name: Download code
        uses: actions/checkout@v4

      - name: Set up QEMU for cross-platform Docker images
        uses: docker/setup-qemu-action@v3
        with:
          platforms: all

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

      - name: Install Poetry
        run: pipx install poetry

      - name: Setup Python 3.11
        uses: actions/setup-python@v5
        with:
          python-version: "3.11"
          cache: "poetry"

      - name: Install dependencies
        run: poetry install

      - name: Run tests
        run: poetry run pytest

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

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

      - name: Build and push Docker image to Docker Hub
        uses: docker/build-push-action@v5
        with:
          context: .
          platforms: linux/amd64, linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          tags: thelovinator/discord-reminder-bot:latest

      - name: Build and push Docker image to GitHub Container Registry
        uses: docker/build-push-action@v5
        with:
          context: .
          platforms: linux/amd64, linux/arm64
          push: ${{ github.event_name != 'pull_request' }}
          tags: ghcr.io/thelovinator1/discord-reminder-bot:latest