name: Build Artifacts (Flatpak)
on:
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'
        required: true
        default: 'warning'
        type: choice
        options:
        - info
        - warning
        - debug
      tags:
        description: 'Test scenario tags'
        required: false
        type: boolean
  pull_request:
    branches: [master, develop]
    paths-ignore:
    - '**/*.md'
    - '.circleci/**'
    - '.cirrus.yml'
  push:
    branches: [master, develop]
    paths-ignore:
    - '**/*.md'
    - '.circleci/**'
    - '.cirrus.yml'
  release:
    types: [published]
  schedule:
    - cron: '0 16 * * *'
concurrency:
  group: ${{ github.workflow }} / ${{ startsWith(github.event_name, 'pull') && github.ref_name || github.run_id }}
  cancel-in-progress: ${{ startsWith(github.event_name, 'pull') }}
env:
  GOPROXY: direct
jobs:
  flatpak-release:
    strategy:
      fail-fast: false
      matrix:
        arch: [x86_64]
    runs-on: ubuntu-24.04
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v5
      - name: Checkout with shallow submodules
        run: |
          # unshallow must come first otherwise submodule may be get unshallowed
          git fetch --tags --unshallow
          git submodule update --init --depth 1
      - name: Change ubuntu mirror
        run: |
          sudo sed -i 's/azure.archive.ubuntu.com/azure.archive.ubuntu.com/g' /etc/apt/sources.list
          sudo apt-get update -qq
      - name: Populate depedencies
        run: |
          sudo apt-get install -y flatpak flatpak-builder
      - name: Build Flatpak package
        run: |
          flatpak remote-add --user --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
          cd flatpak
          flatpak-builder --force-clean --user --arch ${{ matrix.arch }} --install-deps-from=flathub --ccache --mirror-screenshots-url=https://dl.flathub.org/media/ --repo=repo builddir io.github.chilledheart.yass.yml
          flatpak build-bundle repo yass-${{ matrix.arch }}.flatpak io.github.chilledheart.yass --arch ${{ matrix.arch }} --runtime-repo=https://flathub.org/repo/flathub.flatpakrepo
          cd ..
          mv -fv flatpak/yass-${{ matrix.arch }}.flatpak yass-${{ matrix.arch }}-${{ github.event.release.tag_name }}.flatpak
      - uses: actions/upload-artifact@v6
        with:
          name: flatpak-release-${{ matrix.arch }}-${{ github.run_id }}
          path: |
            yass*.flatpak
          compression-level: 0 # no compression
      - name: Upload dist tarball (including debuginfo)
        if: ${{ github.event_name == 'release' }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release upload ${{ github.event.release.tag_name }} yass*.flatpak
