name: Build/release

on:
  workflow_dispatch:
  push:
    branches:
      - 'main'
    tags:
      - '*'

env:
  VERSION: "v0.1.0"
  YARN_URL: "https://github.com/yarnpkg/yarn/releases/download/v1.22.22/yarn-1.22.22.js"

jobs:
  action-release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v5

      - name: Enable Corepack for Yarn
        run: corepack enable

      - name: Install NodeJS and NPM
        uses: actions/setup-node@v5
        with:
          node-version: latest

      - name: Install build dependencies not present
        run: |
          mv /etc/apt/sources.list.d/microsoft-prod.list /etc/apt/sources.list.d/microsoft-prod.list.disabled

          until apt-get update; do
            echo -e "-----\napt-get update failed, retrying in 1s...\n-----"
            sleep 1s
          done

          apt-get install -y \
            dpkg \
            flatpak \
            flatpak-builder \
            rpm \
            libglib2.0-dev \
            wget \
            curl \
            fakeroot \
            xvfb

          flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo

      - name: Install dependencies
        run: |
          yarn install

      - name: Build the app
        run: |
          yarn build

      - name: Release
        uses: akkuman/gitea-release-action@v1
        if: github.ref_type == 'tag'
        with:
          files: |
            ./release/*.deb
            ./release/*.rpm
            ./release/*.flatpak
            ./release/latest-linux.yml
          server_url: ${{ gitea.server_url }}
          token: ${{ secrets.GITEA_TOKEN }} || ${{ secrets.GITHUB_TOKEN }}
          repository: "DaanSelen/ms365-electron-sandbox"
          tag_name: "${{ env.VERSION }}"
          name: "${{ env.VERSION }}"
          sha256sum: true
