name: 'Build'

on:
    push:
        branches:
          - master
        tags:
          - '*'
          - '!flatpak'

jobs:
    # Build Project
    build:
        strategy:
            fail-fast: false
            matrix:
                mode:
                  - Client
                  - Server
                arch:
                  - AMD64
                  - ARM64
                  - ARMHF
        name: Build
        runs-on: ubuntu-latest
        container: node:lts-bullseye
        steps:
          - name: Checkout Repository
            uses: actions/checkout@v4
            with:
                submodules: true
          # Dependencies
          - name: Install Dependencies
            run: ./scripts/install-dependencies.sh ${{ matrix.arch }}
          # Build
          - name: Build
            run: ./scripts/package.sh ${{ matrix.mode }} ${{ matrix.arch }}
          - name: Upload Artifacts
            uses: actions/upload-artifact@v3
            with:
                name: ${{ matrix.mode }}-${{ matrix.arch }}
                path: ./out/*.AppImage*
    # Test Project
    test:
        strategy:
            fail-fast: false
            matrix:
                mode:
                  - Client
                  - Server
        name: Test
        runs-on: ubuntu-latest
        container: node:lts-bullseye
        steps:
          - name: Checkout Repository
            uses: actions/checkout@v4
            with:
                submodules: true
          # Dependencies
          - name: Install Dependencies
            run: ./scripts/install-dependencies.sh
          - name: Install ARM Toolchain
            if: ${{ matrix.mode == 'Client' }}
            run: apt-get install --no-install-recommends -y g++-arm-linux-gnueabihf gcc-arm-linux-gnueabihf
          # Test
          - name: Test
            run: ./scripts/test.sh ${{ matrix.mode }}
    # Create Release
    release:
        if: startsWith(github.ref, 'refs/tags/')
        needs: build
        name: Release
        runs-on: ubuntu-latest
        container: node:lts-bullseye
        steps:
          # Dependencies
          - name: Install Go
            uses: actions/setup-go@v4
            with:
                go-version: '>=1.20.1'
          # Download Artifacts
          - name: Download Artifacts
            uses: actions/download-artifact@v3
            with:
                path: out
          # Create Release
          - name: Create Release
            uses: https://gitea.com/actions/release-action@main
            with:
                files: ./out
                api_key: ${{ secrets.RELEASE_TOKEN }}
                title: v${{ github.ref_name }}
                body: "[View Changelog](https://gitea.thebrokenrail.com/minecraft-pi-reborn/minecraft-pi-reborn/src/branch/master/docs/CHANGELOG.md)"
