name: Main

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

env:
  REGISTRY: gitea.lucids-cove.duckdns.org
  OWNER: wkc
  IMAGE_NAME: no-twitter-bot-stats

jobs:
  build-and-push-docker-image:
    runs-on: ubuntu-24.04
    strategy:
      matrix:
        node-version: [20.x]
    steps:
      - name: "Base requirements"
        run: |
          # packages
          apk update && apk add --no-cache git docker docker-compose nodejs gpg openssh npm ansible
          # ansible collections
          ansible-galaxy collection install community.general --force
          ansible-galaxy collection install ansible.posix --force
      - uses: actions/checkout@v4
      - name: Enable Corepack
        run: npm install -g corepack && corepack enable
      - name: Log into registry ${{ env.REGISTRY }}
        uses: docker/login-action@v2
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ env.OWNER }}
          password: ${{ secrets.TOKEN }}
      - name: Docker Hub Login
        run: echo "${{ secrets.DOCKER_HUB_TOKEN }}" | docker login -u "${{ secrets.DOCKER_HUB_USERNAME }}" --password-stdin
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2
      - name: Setup Docker buildx
        uses: docker/setup-buildx-action@v2
      - name: Extract Docker metadata
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.REGISTRY }}/${{ env.OWNER }}/${{ env.IMAGE_NAME }}
      - name: Build and Push Versioned Docker Image
        id: build-and-push
        uses: docker/build-push-action@v4
        if: ${{ github.ref != 'refs/heads/main' }}
        with:
          context: .
          push: ${{ !github.event.pull_request.head.repo.fork }}
          tags: ${{ steps.meta.outputs.tags }}
      - name: Build and Push Latest Docker Image
        id: build-and-push-latest
        uses: docker/build-push-action@v4
        if: ${{ github.ref == 'refs/heads/main' }}
        with:
          context: .
          push: true
          tags: ${{ env.REGISTRY }}/${{ env.OWNER }}/${{ env.IMAGE_NAME }}:latest
