name: docker

on:
  workflow_dispatch:
  push:
    branches:
      - prime
    tags:
      - "*"
  pull_request:
    branches:
      - prime

jobs:
  # Outputs from this job aren't really used, but it's here to verify that the Dockerfile builds correctly
  pack:
    runs-on: ubuntu-latest
    timeout-minutes: 10

    permissions:
      actions: write
      contents: read

    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Install Docker Buildx
        uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0

      - name: Build image
        run: >
          docker buildx build .
          --file DiscordChatExporter.Cli.dockerfile
          --platform linux/amd64,linux/arm64
          --build-arg VERSION=${{ github.ref_type == 'tag' && github.ref_name || format('999.9.9-ci-{0}', github.sha) }}
          --output type=tar,dest=DiscordChatExporter.Cli.Docker.tar

      - name: Upload image
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: DiscordChatExporter.Cli.Docker
          path: DiscordChatExporter.Cli.Docker.tar
          if-no-files-found: error

  deploy:
    # Deploy to DockerHub only on tag push or prime branch push
    if: ${{ github.ref_type == 'tag' || github.ref_type == 'branch' && github.ref_name == 'prime' }}

    runs-on: ubuntu-latest
    timeout-minutes: 10

    permissions:
      contents: read

    steps:
      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

      - name: Install Docker Buildx
        uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0

      - name: Login to DockerHub
        run: >
          echo ${{ secrets.DOCKER_TOKEN }} |
          docker login --username tyrrrz --password-stdin

      - name: Build & push image
        run: >
          docker buildx build .
          --file DiscordChatExporter.Cli.dockerfile
          --platform linux/amd64,linux/arm64
          --build-arg VERSION=${{ github.ref_type == 'tag' && github.ref_name || format('999.9.9-ci-{0}', github.sha) }}
          --push
          --tag tyrrrz/discordchatexporter:latest
          ${{ github.ref_type == 'tag' && '--tag tyrrrz/discordchatexporter:$GITHUB_REF_NAME' || '' }}
          ${{ github.ref_type == 'tag' && '--tag tyrrrz/discordchatexporter:stable' || '' }}
