name: 'CI'

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/build.mjs appimage ${{ matrix.mode }} ${{ matrix.arch }}
          - name: Upload Artifacts
            uses: actions/upload-artifact@v3
            with:
                name: ${{ matrix.mode }} (${{ matrix.arch }})
                path: ./out/*.AppImage*
                if-no-files-found: error
    # 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
          # Test
          - name: Test
            run: ./scripts/test.sh ${{ matrix.mode }}
    # Example Mods
    example-mods:
        name: Build Example Mods
        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
            run: apt-get install --no-install-recommends -y g++-arm-linux-gnueabihf gcc-arm-linux-gnueabihf
          # Build SDK
          - name: Build SDK
            run: |
                ./scripts/build.mjs none client host
                export _MCPI_SKIP_ROOT_CHECK=1
                export DISPLAY=
                ./out/client/host/usr/bin/minecraft-pi-reborn-client --copy-sdk
          # Build Example Mods
          - name: Build Example Mods
            run: ./example-mods/build.sh
          - name: Upload Artifacts
            uses: actions/upload-artifact@v3
            with:
                name: Example Mods
                path: ./example-mods/out/*
                if-no-files-found: error
    # 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/*/*.AppImage*
                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)"