name: Build Application

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

jobs:
  build:
    runs-on: stable
    container:
      image: git.euph.dev/actions/runner-java-21:latest
    steps:
      - name: "Checkout"
        uses: "https://git.euph.dev/actions/checkout@v3"
      - name: "Install Angular CLI"
        run: sudo npm i -g @angular/cli
      - name: "Install Dependencies"
        run: npm ci
      - name: "Generate API Client"
        run: |
         API_VERSION="$(cat package.json | jq -r '.api_version')"
         API_URL="https://git.euph.dev/TowerDefence/Server/releases/download/$API_VERSION/api.yml"
         rm -rf src/app/core/server
         mkdir -p src/app/core/server
         npx @openapitools/openapi-generator-cli \
           generate \
           --openapitools src/app/core/openapitools.json \
           -i "$API_URL" \
           -g typescript-angular \
           -o src/app/core/server
      - name: Build Bundle
        run: ng build
      - name: Upload Binary as Artifact
        uses: "https://git.euph.dev/actions/upload-artifact@v3"
        with:
          name: web
          path: dist/tower-defence-administration/browser/*

  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: 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-administration:${{ github.ref_name }}
            ${{ contains(github.ref_name, 'rc') == false && 'git.euph.dev/towerdefence/web-administration:latest' || '' }}

  release:
    runs-on: stable
    container:
      image: git.euph.dev/actions/runner-basic:latest
    needs:
      - build
      - build-docker
    steps:
      - name: Create Empty Release Asset Dir
        run: mkdir -p tmp/this/dir/does/not/exist/yet
      - 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: tmp/this/dir/does/not/exist/yet
          release-notes: |
            # Tower Defence - Administration ${{ github.ref_name }}
            Run this release with docker like this:
            \`\`\`sh
            docker run --rm -p 4200:80 git.euph.dev/towerdefence/web-administration:${{ github.ref_name }}
            \`\`\`
            It will be available under [\`localhost:4200\`](localhost:4200)
            <br><br>
            For more information read the [Documentation](https://git.euph.dev/TowerDefence/Dokumentation/wiki/Administration/Config)