name: bulid 9 package

on:
 workflow_dispatch:
  push:
    tags:
      - v*.**

jobs:
  build:
    name: Build ${{ matrix.arch }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        arch:
          - aarch64_cortex-a53
          - aarch64_cortex-a72
          - aarch64_generic
          - arm_arm1176jzf-s_vfp
          - arm_arm926ej-s
          - arm_cortex-a15_neon-vfpv4
          - arm_cortex-a5_vfpv4
          - arm_cortex-a7
          - arm_cortex-a7_neon-vfpv4
          - arm_cortex-a8_vfpv3
          - arm_cortex-a9
          - arm_cortex-a9_neon
          - arm_cortex-a9_vfpv3-d16
          - arm_fa526
          - arm_mpcore
          - arm_xscale
          - i386_pentium-mmx
          - i386_pentium4
          - mips64_octeonplus
          - mips_24kc
          - mips_4kec
          - mips_mips32
          - mipsel_24kc
          - mipsel_24kc_24kf
          - mipsel_74kc
          - mipsel_mips32
          - x86_64

    steps:
      - uses: actions/checkout@main
        with:
          fetch-depth: 0

      - name: Build
        uses: sbwml/openwrt-gh-action-sdk@go1.21
        env:
          ARCH: ${{ matrix.arch }}-openwrt-22.03
          FEEDNAME: packages_ci
          PACKAGES: luci-app-passwall luci-app-passwall2 luci-app-ssr-plus luci-app-bypass naiveproxy tuic-client
          NO_REFRESH_CHECK: true

      - name: Upload artifacts
        uses: actions/upload-artifact@main
        with:
          name: ${{ matrix.arch }}
          path: bin/packages/${{ matrix.arch }}/packages_ci/*.ipk

      - name: Zip ipk packages
        run: |
          zip -j ${{ matrix.arch }}.zip bin/packages/${{ matrix.arch }}/packages_ci/*.ipk
          echo -e "**文件说明** 🎈:\n" >> release.txt
          echo -e "1. 默认ssr与passwall的插件与依赖整合包\n" >> release.txt
          echo -e "2. 使用方法：将整合包上传到openwrt设备的tmp目录，输入命令 \`opkg install *.ipk\`\n" >> release.txt
          echo -e "3. 默认压缩包里包含ssr passwall bypass passwall2 插件\n" >> release.txt
          echo -e "4. 如果单独安装ssr与依赖，\`rm -rf {*passwall*,*bypass*,*vssr*}\`\n" >> release.txt
          
      - name: Create Release zip
        uses: ncipollo/release-action@main
        with:
          token: ${{ secrets.ACCESS_TOKEN }}
          artifacts: "${{ matrix.arch }}.zip,bin/packages/${{ matrix.arch }}/packages_ci/*.ipk"
          allowUpdates: true
          replacesArtifacts: true
          bodyFile: "release.txt"
          tag: v3.${{ github.run_number }}_${{ matrix.arch }}

      - name: Delete workflow runs
        uses: Mattraks/delete-workflow-runs@main
        with:
          retain_days: 6
          keep_minimum_runs: 0
    
      - name: Remove old Releases
        uses: dev-drprasad/delete-older-releases@master
        with:
          keep_latest: 40
          delete_tags: true
        env:
           GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
