name: Build Application

on:
  push:
    tags:
      - 'v[0-9]+\.[0-9]+\.[0-9]+(-rc\.[0-9]+)?'
      - 'v*'

jobs:
  generate:
    runs-on: stable
    container:
      image: git.euph.dev/actions/runner-java-21:1300
    steps:
      - name: "Checkout"
        uses: "https://git.euph.dev/actions/checkout@v3"
      - name: "Generate"
        run: just generate
      - name: Upload API Client as Artifact
        uses: "https://git.euph.dev/actions/upload-artifact@v3"
        with:
          name: api-client
          path: scripts/api-client/*

  build:
    runs-on: stable
    needs:
      - generate
    container:
      image: git.euph.dev/actions/runner-redot-4.3:latest
    strategy:
      matrix:
        include:
          - platform: Linux
            out: tower_defence.elf
            artifact-name: tower_defence.elf
            artifact-path: tower_defence.elf
          #- platform: OSX
          #  build-dir: build
          #  out: build/tower_defence.app
          #  artifact-name: tower_defence.app
          #  artifact-path: build
          - platform: Windows
            out: tower_defence.exe
            artifact-name: tower_defence.exe
            artifact-path: tower_defence.exe
          - platform: Web
            build-dir: build
            out: build/index.html
            artifact-name: tower_defence_web
            artifact-path: build/*
    steps:
      - name: "Checkout"
        uses: "https://git.euph.dev/actions/checkout@v3"
      - name: Download artifact from previous job
        uses: "https://git.euph.dev/actions/download-artifact@v3"
        with:
          name: api-client
          path: scripts/api-client
      - name: Build Binary
        run: |
          if [ "${{ matrix.build-dir }}" != "" ]; then
            mkdir -p ${{ matrix.build-dir}}
          fi
          redot --headless --export-release ${{ matrix.platform }} ${{ matrix.out }}
      - name: Upload Binary as Artifact
        uses: "https://git.euph.dev/actions/upload-artifact@v3"
        with:
          name: ${{ matrix.artifact-name }}
          path: ${{ matrix.artifact-path }}

  build-docker:
    runs-on: docker
    needs:
      - build
    steps:
      - name: "Checkout"
        uses: "https://git.euph.dev/actions/checkout@v3"
      - name: Download artifact from previous job
        uses: "https://git.euph.dev/actions/download-artifact@v3"
        with:
          name: tower_defence_web
          path: .forgejo/workflows/web
      - name: Login to Registry
        uses: "https://git.euph.dev/actions/docker-login@v3"
        with:
          registry: git.euph.dev
          username: ${{ secrets.DEPLOY_USER }}
          password: ${{ secrets.DEPLOY_SECRET }}
      - name: Build and push Web Image
        uses: "https://git.euph.dev/actions/docker-build-push@v5"
        with:
          context: ".forgejo/workflows/"
          push: true
          tags: |
            git.euph.dev/towerdefence/web-client:${{ github.ref_name }}
            ${{ contains(github.ref_name, 'rc') == false && 'git.euph.dev/towerdefence/web-client:latest' || '' }}

  release:
    runs-on: stable
    container:
      image: git.euph.dev/actions/runner-basic:latest
    needs:
      - build
      - build-docker
    steps:
      - name: Download Linux Release Binaries
        uses: "https://git.euph.dev/actions/download-artifact@v3"
        with:
          name: tower_defence.elf
          path: release
      - name: Download Windows release Binaries
        uses: "https://git.euph.dev/actions/download-artifact@v3"
        with:
          name: tower_defence.exe
          path: release
      - name: Create Release
        uses: "https://git.euph.dev/actions/release@v2"
        with:
          direction: upload
          tag: ${{ github.ref_name }}
          token:  ${{ secrets.DEPLOY_TOKEN }}
          prerelease: ${{ contains( github.ref_name, "rc") }}
          release-dir: release
          release-notes: |
            # Tower Defence - Client ${{ github.ref_name }}
            Run this release with docker like this:
            \`\`\`sh
            docker run --rm -p 8100:80 git.euph.dev/towerdefence/web-client:${{ github.ref_name }}
            \`\`\`
            It will be available under [\`localhost:8100\`](localhost:8100)
            <br><br>
            For more information read the [Documentation](https://git.euph.dev/TowerDefence/Dokumentation/wiki/Client/Config)