name: Build rule sets
on:
  workflow_dispatch:
  push:
  schedule:
    - cron: '0 * * * *'

permissions:
  contents: write

jobs:
  generate-rule-sets:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v6
      - name: Update subnets
        uses: actions/setup-java@v5
        with:
          java-version: '17'
          distribution: 'oracle'
          java-package: 'jdk'
          architecture: 'x64'
      - run: |
          java -jar ip-ranges.jar
      - name: Push subnets
        uses: EndBug/add-and-commit@v9
        with:
          message: 'Update subnets'
      - name: Install sing-box
        run: curl -fsSL https://sing-box.app/install.sh | sh
      - name: Compile domains.srs
        run: sing-box rule-set compile --output domains.srs domains.json
      - name: Compile subnets.srs
        run: sing-box rule-set compile --output subnets.srs subnets.json
      - name: Set up variables
        id: vars
        run: |
          echo "date_now=$(date +"%Y-%m-%d %T")" >> "${GITHUB_OUTPUT}"
      - name: Upload files to Releases
        uses: ncipollo/release-action@v1
        with:
          makeLatest: true
          allowUpdates: true
          artifactErrorsFailBuild: true
          artifacts: ./*.srs
          body: |
            - Last build date: `${{steps.vars.outputs.date_now}}`
            - Reference commit: ${{github.sha}}
          name: Updatable rule sets
          removeArtifacts: true
          tag: updatable-rule-sets
