name: Create GitHub release after tag push

on:
  push:
    tags:
      - '*'

jobs:
  release:
    runs-on: ubuntu-latest

    # OUTPUTS
    outputs:
      newtag: ${{ steps.tag.outputs.tag }}
      uploadurl: ${{ steps.create_release.outputs.upload_url }}

    steps:
      # GET LATEST CODE
      - name: Checkout latest code
        uses: actions/checkout@master
        with:
          fetch-depth: 0

      # EXTRACT TAG FROM PUSH
      - name: Get tag
        id: tag
        uses: dawidd6/action-get-tag@v1
      - name: Use tag
        run: echo ${{steps.tag.outputs.tag}}

      # GENERATE CHANGELOG, RELEASE
      - id: conventional_changelog
        uses: ardalanamini/auto-changelog@master
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
      - name: Create Release
        id: create_release
        uses: actions/create-release@latest
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{steps.tag.outputs.tag}}
          body: |
            Automatic release of version: **${{steps.tag.outputs.tag}}**
            **Changes in this release:**
            ${{ steps.conventional_changelog.outputs.changelog }}
          draft: false
          prerelease: false

      - name: Setup Node.js environment
        uses: actions/setup-node@v2.5.1
        with:
          # Version Spec of the version to use.  Examples: 12.x, 10.15.1, >=10.15.0
          node-version: '>= 16.9.1'
          # Set this option if you want the action to check for the latest available version that satisfies the version spec
          check-latest: false

      - name: Install Quasar
        run: npm install -g @quasar/cli

      - name: Install packages
        run: npm install

      - name: Build project for NGINX release package
        run: quasar build

      - name: List files
        run: pwd && ls -al *

      - uses: actions/upload-artifact@v2
        with:
          name: build-artifacts-quasar
          path: dist/spa/
          if-no-files-found: warn # 'warn' or 'ignore' are also available, defaults to `warn`

      # NGINX Archives
      - name: Create NGINX Archive tar.gz
        run: |
          echo "**** Creating archive of quasar build ****"
          tar -cvzf lttmm-NGINX-${{ steps.tag.outputs.tag }}.tar.gz dist/spa
      - name: Create NGINX Archive zip
        uses: papeloto/action-zip@v1
        with:
          files: dist/spa
          recursive: true
          dest: lttmm-NGINX-${{ steps.tag.outputs.tag }}.zip

      # Upload NGINX Assets
      - name: Upload Release Archive tar.gz
        id: upload-release-asset-archive-targz
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./lttmm-NGINX-${{ steps.tag.outputs.tag }}.tar.gz
          asset_name: lttmm-NGINX-${{ steps.tag.outputs.tag }}.tar.gz
          asset_content_type: application/gzip
      - name: Upload Release Archive zip
        id: upload-release-asset-archive-zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./lttmm-NGINX-${{ steps.tag.outputs.tag }}.zip
          asset_name: lttmm-NGINX-${{ steps.tag.outputs.tag }}.zip
          asset_content_type: application/zip

      # DEPLOY TO GH-PAGES
      - name: Deploy to GitHub-Pages
        uses: JamesIves/github-pages-deploy-action@v4.2.5
        with:
          branch: gh-pages
          folder: dist/spa

      # DOCKER RELATED
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: griefed
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Login to GitHub Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          build-args: |
            BRANCH_OR_TAG=${{ github.ref_name }}
            HOSTER=github.com
          file: ./Dockerfile
          platforms: linux/amd64,linux/arm/v7,linux/arm64
          push: true
          tags: |
            ghcr.io/griefed/ltt-mapmaker:latest
            ghcr.io/griefed/ltt-mapmaker:${{ steps.tag.outputs.tag }}
            griefed/ltt-mapmaker:latest
            griefed/ltt-mapmaker:${{ steps.tag.outputs.tag }}

  electron:
    needs: release
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [macos-latest, ubuntu-latest, windows-latest]

    steps:
      # GET LATEST CODE
      - name: Checkout latest code
        uses: actions/checkout@master
        with:
          fetch-depth: 0

      # SETUP NODE ENVIRONMENT
      - name: Setup Node.js environment
        uses: actions/setup-node@v2.5.1
        with:
          # Version Spec of the version to use.  Examples: 12.x, 10.15.1, >=10.15.0
          node-version: '>= 16.9.1'
          # Set this option if you want the action to check for the latest available version that satisfies the version spec
          check-latest: false

      - name: Build/release Electron app
        uses: vedaprakashms/quasar_electron_build@V0.0.1
        with:
          # GitHub token, automatically provided to the action
          # (No need to define this secret in the repo settings)
          github_token: ${{ secrets.github_token }}

      - name: List files
        run: ls dist/electron

      - uses: actions/upload-artifact@v2
        with:
          name: build-artifacts-electron ${{ matrix.os }}
          path: |
            dist/electron/
            !dist/electron/UnPackaged
          if-no-files-found: warn # 'warn' or 'ignore' are also available, defaults to `warn`

      - name: Rename folders
        run: |
          mv dist/electron/Let* lttmm-${{ matrix.os }}

      # Electron Archives
      - name: Create zip
        uses: ihiroky/archive-action@v1
        with:
          root_dir: lttmm-${{ matrix.os }}
          file_path: lttmm-app-${{ matrix.os }}-${{ needs.release.outputs.newtag }}.zip
          verbose: true
      - name: Create tar.gz
        run: |
          tar -cvzf lttmm-app-${{ matrix.os }}-${{ needs.release.outputs.newtag }}.tar.gz lttmm-${{ matrix.os }}

      # Upload App Assets
      - name: Upload App Archive zip
        id: upload-release-asset-archive-zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.release.outputs.uploadurl }}
          asset_path: ./lttmm-app-${{ matrix.os }}-${{ needs.release.outputs.newtag }}.zip
          asset_name: lttmm-app-${{ matrix.os }}-${{ needs.release.outputs.newtag }}.zip
          asset_content_type: application/zip
      - name: Upload App Archive tar.gz
        id: upload-release-asset-archive-targz
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.release.outputs.uploadurl }}
          asset_path: ./lttmm-app-${{ matrix.os }}-${{ needs.release.outputs.newtag }}.tar.gz
          asset_name: lttmm-app-${{ matrix.os }}-${{ needs.release.outputs.newtag }}.tar.gz
          asset_content_type: application/gzip
