name: Build Artifacts (RPM)
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:
  CACHE_EPOCH: 148-2
  GOPROXY: direct
jobs:
  docker_publish:
    strategy:
      fail-fast: false
      matrix:
        include:
          - container: 'fedora41'
            platform: 'linux/amd64'
          - container: 'opensuse15'
            platform: 'linux/amd64'
    runs-on: ubuntu-24.04
    permissions:
      packages: write
    steps:
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v4
        with:
          image: tonistiigi/binfmt:qemu-v7.0.0-28
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4
        with:
          platforms: ${{ matrix.platform }}
      - uses: actions/checkout@v5
      - name: Cache docker files (dummy)
        id: docker-cache
        uses: actions/cache@v5
        with:
          path: |
            docker/dummy
          key: ${{ runner.os }}-docker-${{ matrix.container }}-${{ hashFiles(format('docker/{0}.Dockerfile', matrix.container)) }}-v1
      - name: Login to GitHub Container Registry
        if: ${{ steps.docker-cache.outputs.cache-hit != 'true' }}
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Build and Push Docker Image
        if: ${{ steps.docker-cache.outputs.cache-hit != 'true' }}
        run: |
          cd docker
          docker buildx build --cache-from ghcr.io/hukeyue/${{ matrix.container }} \
            --platform ${{ matrix.platform }} \
            --push \
            -f ${{ matrix.container }}.Dockerfile \
            -t ghcr.io/hukeyue/${{ matrix.container }} \
            .
  rpm-release:
    strategy:
      fail-fast: false
      matrix:
        include:
          # - container: 'fedora41'
          #   gui_variant: gtk3
          #   use_cet: true
          # - container: 'fedora41'
          #   gui_variant: gtk4
          #   use_cet: true
          # https://rpmfind.net/linux/rpm2html/search.php?query=glibc-devel&submit=Search+...&system=&arch=
          # glibc: 2.40 for fedora41
          # - container: 'fedora41'
          #   gui_variant: qt6
          #   use_cet: true
          - container: 'opensuse15'
            gui_variant: gtk3
          - container: 'opensuse15'
            gui_variant: qt5
          # except for gtk4 and qt6 packages
          # glibc: 2.31 for opensuse15.5
          - container: 'opensuse15'
            gui_variant: gtk4
          - container: 'opensuse15'
            gui_variant: qt6
    runs-on: ubuntu-24.04
    permissions:
      contents: write
    needs: docker_publish
    container:
      image: 'ghcr.io/hukeyue/${{ matrix.container }}'
    steps:
      - uses: actions/checkout@v5
      - name: Checkout with shallow submodules
        run: |
          # ignore annoying failures
          git config --global --add safe.directory "$PWD"
          # unshallow must come first otherwise submodule may be get unshallowed
          git fetch --tags --unshallow
          git submodule update --init --depth 1
      - name: Cache toolchain
        id: toolchain-cache
        uses: actions/cache@v5
        with:
          path: |
            third_party/llvm-build/Release+Asserts
          key: ${{ runner.os }}-toolchain-${{ hashFiles('CLANG_REVISION') }}-v${{ env.CACHE_EPOCH }}
      - name: "Install dependency: prebuilt clang and clang-tidy binaries"
        if: ${{ steps.toolchain-cache.outputs.cache-hit != 'true' }}
        run: |
          ./scripts/download-clang-prebuilt-binaries.py
          rm -f third_party/llvm-build/Release+Asserts/*.tgz
      - name: Set clang environment
        run: |
          # use custom compiler
          echo "CC=${{ github.workspace }}/third_party/llvm-build/Release+Asserts/bin/clang" >> $GITHUB_ENV
          echo "CXX=${{ github.workspace }}/third_party/llvm-build/Release+Asserts/bin/clang++" >> $GITHUB_ENV
          echo "USE_CLANG=1" >> $GITHUB_ENV
      - name: Set cet option
        if: ${{ matrix.use_cet == true }}
        run: |
          # use CET
          echo "USE_CET=1" >> $GITHUB_ENV
      - name: Set c++ standard library environment
        run: |
          # use custom libc++
          echo "USE_LIBCXX=1" >> $GITHUB_ENV
      - name: Set QT6 environment
        if: ${{ matrix.gui_variant == 'qt6' }}
        run: |
          echo "USE_QT6=1" >> $GITHUB_ENV
      - name: Set QT5 environment
        if: ${{ matrix.gui_variant == 'qt5' }}
        run: |
          echo "USE_QT5=1" >> $GITHUB_ENV
      - name: Set GTK4 environment
        if: ${{ matrix.gui_variant == 'gtk4' }}
        run: |
          echo "USE_GTK4=1" >> $GITHUB_ENV
      - uses: actions/setup-go@v6
        with:
          go-version: '>=1.20.0'
          cache-dependency-path: |
            tools/go.sum
            third_party/boringssl/src/go.sum
      - name: Build RPM packages
        run: |
          ./scripts/build-rpm.sh
      - uses: actions/upload-artifact@v6
        with:
          name: rpm-release-${{ matrix.container }}-${{ matrix.gui_variant }}-${{ github.run_id }}
          path: |
            yass*.rpm
          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 --clobber ${{ github.event.release.tag_name }} yass*.rpm
