name: CI

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

env:
  GODOT_VERSION: "4.3"
  GODOT_BUILD: "stable"

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: docker

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:

      - name: Build Release
        if: env.GITHUB_EVENT_NAME == 'push'
        run: echo "BUILD_TYPE=release" >> $GITHUB_ENV

      - name: Build Debug/Beta
        if: env.GITHUB_EVENT_NAME == 'pull_request'
        run: echo "BUILD_TYPE=debug" >> $GITHUB_ENV

      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2
        name: Checkout Files

      - name: Download Godot
        run: |
          echo "https://github.com/godotengine/godot-builds/releases/download/$GODOT_VERSION-stable/Godot_v$GODOT_VERSION-stable_linux.arm64.zip"
          curl -L -o godot.zip https://github.com/godotengine/godot-builds/releases/download/$GODOT_VERSION-stable/Godot_v$GODOT_VERSION-stable_linux.arm64.zip
          unzip godot.zip
          mv Godot_v$GODOT_VERSION-stable_linux.arm64 /usr/bin/godot
          chmod +x /usr/bin/godot

      - name: Download Godot Export Templates
        run: |
          echo "https://code.ultrablob.me/ultrablob/godot-export-template-web-only/releases/download/4.3-stable/godot-4.3-web-only.tpz"
          curl -L -o export_templates.zip https://code.ultrablob.me/ultrablob/godot-export-template-web-only/releases/download/4.3-stable/godot-4.3-web-only.tpz
          mkdir templates
          unzip export_templates.zip -d templates

      - name: Install Export Templates
        run: |
          mkdir -p ~/.local/share/godot/export_templates/$GODOT_VERSION.stable
          mv ./templates/* ~/.local/share/godot/export_templates/$GODOT_VERSION.stable
        
      - name: Build Game
        run: |
          mkdir -v -p build/web
          EXPORT_DIR="$(readlink -f build)"
          cd $GITHUB_WORKSPACE
          /usr/bin/godot --headless --verbose --export-${{ env.BUILD_TYPE }} "Web" "$EXPORT_DIR/web/index.html"

      - name: Upload Build
        uses: https://code.forgejo.org/forgejo/upload-artifact@v4
        id: upload-build
        with:
          name: Web Build
          path: build/web
          retention-days: 3
          overwrite: true
          compression-level: 0
