name: Debian Packages
permissions: read-all
on:
  workflow_dispatch:
  push:
    tags:
      - '[0-9]+.[0-9]+.[0-9]+'
  schedule:
    - cron: '0 0 * * 1,4' # Runs at 00:00 UTC on Monday and Thursday
  pull_request:
    branches: [ main ]
    paths:
      - 'packaging/debian/**' # Only run on changes to the debian packaging files
      - '.github/workflows/debian_packages.yml'
jobs:
  test-packages:
    if: (github.event_name == 'schedule' && github.repository_owner == 'miniflux')
      || github.event_name == 'pull_request'
    name: Test Packages
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      with:
          fetch-depth: 0
    - name: Set up QEMU
      uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
      id: buildx
      with:
          install: true
    - name: Available Docker Platforms
      run: echo ${{ steps.buildx.outputs.platforms }}
    - name: Build Debian Packages
      run: make debian-packages
    - name: List generated files
      run: ls -l *.deb
  build-packages-manually:
    if: github.event_name == 'workflow_dispatch'
    name: Build Packages Manually
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      with:
          fetch-depth: 0
    - name: Set up QEMU
      uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
      id: buildx
      with:
          install: true
    - name: Available Docker Platforms
      run: echo ${{ steps.buildx.outputs.platforms }}
    - name: Build Debian Packages
      run: make debian-packages
    - name: Upload package
      uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
      with:
        name: packages
        path: "*.deb"
        if-no-files-found: error
        retention-days: 3
  publish-packages:
    if: github.event_name == 'push' && github.repository_owner == 'miniflux'
    name: Publish Packages
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      with:
          fetch-depth: 0
    - name: Set up QEMU
      uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
      id: buildx
      with:
          install: true
    - name: Available Docker Platforms
      run: echo ${{ steps.buildx.outputs.platforms }}
    - name: Build Debian Packages
      run: make debian-packages
    - name: List generated files
      run: ls -l *.deb
    - name: Upload packages to repository
      env:
        FURY_TOKEN: ${{ secrets.FURY_TOKEN }}
      run: for f in *.deb; do curl -F package=@$f https://$FURY_TOKEN@push.fury.io/miniflux/; done
