name: Create tag and build when new code gets to main
run-name: Create tag and build when new code gets to main
on:
  push:
    branches:
      - main
    tags-ignore:
      - "**"

env:
  GAME_NAME: GMTK25
  ITCHIO_USERNAME: Minimata
  ITCHIO_GAMEID: GMTK25


jobs:
  BumpTag:
    runs-on: ubuntu-latest
    outputs:
      tag_name: ${{ steps.bump-tag.outputs.new_tag }}
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
        with:
          lfs: false
      - name: Remove buggy pre-push hook
        run: |
          rm -f .git/hooks/pre-push          
      - name: Bump version and push tag
        id: bump-tag
        uses: anothrNick/github-tag-action@v1
        env:
          GITHUB_TOKEN: ${{ secrets.TOKEN }}
          GIT_API_TAGGING: false
          WITH_V: true
          PRERELEASE: false
          INITIAL_VERSION: 0.1.0
          DEFAULT_BUMP: patch

  Export:
    runs-on: ubuntu-latest
    needs: BumpTag
    container:
      image: barichello/godot-ci:4.4.1

    steps:
      - name: Install node, curl and zip
        run: |
          apt update && apt -y install curl zip nodejs          
      - name: Checkout with LFS
        uses: https://git.game-dev.space/minimata/checkout-with-lfs.git@main
        with:
          checkout-version: 3
      - name: Import resources and build solution
        run: |
          godot --headless --editor --build-solutions --quit --import --path $PWD          
      - name: Build Windows
        run: |
          mkdir -v -p build/windows
          godot --headless --verbose --build-solutions --import --export-release "Windows Desktop" build/windows/${{ env.GAME_NAME }}.exe
          zip -r Windows.zip build/windows
      - name: Linux Build
        run: |
          mkdir -v -p build/linux
          godot --headless --verbose --export-release "Linux/X11" build/linux/${{ env.GAME_NAME }}.x86_64
          zip -r Linux.zip build/linux
      - name: Mac Build
        run: |
          mkdir -v -p build/mac
          godot --headless --verbose --export-release "macOS" build/mac/${{ env.GAME_NAME }}.zip
          zip -r Mac.zip build/mac
          
      - name: Upload to Itch
        uses: KikimoraGames/itch-publish@v0.0.3
        with:
          butlerApiKey: ${{ secrets.BUTLER_TOKEN }}
          itchUsername: ${{ env.ITCHIO_USERNAME }}
          itchGameId: ${{ env.ITCHIO_GAMEID }}
          buildNumber: ${{ needs.BumpTag.outputs.tag_name }}
          gameData: Windows.zip
          buildChannel: windows
      - name: Upload to Itch
        uses: KikimoraGames/itch-publish@v0.0.3
        with:
          butlerApiKey: ${{ secrets.BUTLER_TOKEN }}
          itchUsername: ${{ env.ITCHIO_USERNAME }}
          itchGameId: ${{ env.ITCHIO_GAMEID }}
          buildNumber: ${{ needs.BumpTag.outputs.tag_name }}
          gameData: Linux.zip
          buildChannel: linux  
      - name: Upload to Itch
        uses: KikimoraGames/itch-publish@v0.0.3
        with:
          butlerApiKey: ${{ secrets.BUTLER_TOKEN }}
          itchUsername: ${{ env.ITCHIO_USERNAME }}
          itchGameId: ${{ env.ITCHIO_GAMEID }}
          buildNumber: ${{ needs.BumpTag.outputs.tag_name }}
          gameData: Mac.zip
          buildChannel: mac
