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:
      - uses: actions/checkout@v3
      - run: pipx install poetry
      - uses: actions/setup-python@v4
        with:
          python-version: "3.10"
          cache: 'poetry'
      - run: poetry install
      - run: poetry run pytest
      - name: Login to Docker Hub
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v2
        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@v2
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Extract Docker metadata
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: |
            thelovinator/discord-reminder-bot
            ghcr.io/thelovinator1/discord-reminder-bot
          flavor: latest=${{ github.ref == 'refs/heads/master' }}
      - name: Build and push Docker image
        id: build-and-push
        uses: docker/build-push-action@v3
        with:
          context: .
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}