# Gitea Actions workflow for StarPunk
# Builds and pushes container images on version tags

name: Build Container

on:
  # Trigger on version tags
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'
  # Allow manual trigger from Gitea UI
  workflow_dispatch:

jobs:
  build:
    runs-on: docker
    steps:
      - name: Install dependencies
        run: |
          if command -v apk > /dev/null; then
            apk add --no-cache nodejs npm docker git
          elif command -v apt-get > /dev/null; then
            apt-get update && apt-get install -y nodejs npm docker.io git
          elif command -v yum > /dev/null; then
            yum install -y nodejs npm docker git
          fi

      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Extract registry URL
        id: registry
        run: |
          # Extract hostname from server URL (remove protocol)
          REGISTRY_URL=$(echo "${{ github.server_url }}" | sed 's|https://||' | sed 's|http://||')
          echo "url=${REGISTRY_URL}" >> $GITHUB_OUTPUT

      - name: Login to Gitea Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ steps.registry.outputs.url }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          file: ./Containerfile
          push: true
          tags: |
            ${{ steps.registry.outputs.url }}/${{ github.repository }}:${{ github.ref_name }}
            ${{ steps.registry.outputs.url }}/${{ github.repository }}:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max
