snortal 1.0.6

Detect captive portal URLs on a Linux network
name: Release

on:
  push:
    tags:
      - "v*"

env:
  CARGO_TERM_COLOR: always
  BINARY_NAME: snortal

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.runner }}
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            artifact_suffix: linux-x86_64
            runner: ubuntu-latest
            use_cross: true
            package: true
          - target: x86_64-unknown-linux-musl
            artifact_suffix: linux-x86_64-musl
            runner: ubuntu-latest
            use_cross: true
          - target: i686-unknown-linux-gnu
            artifact_suffix: linux-x86
            runner: ubuntu-latest
            use_cross: true
            package: true
          - target: i686-unknown-linux-musl
            artifact_suffix: linux-x86-musl
            runner: ubuntu-latest
            use_cross: true
          - target: aarch64-unknown-linux-gnu
            artifact_suffix: linux-aarch64
            runner: ubuntu-latest
            use_cross: true
            package: true
          - target: aarch64-unknown-linux-musl
            artifact_suffix: linux-aarch64-musl
            runner: ubuntu-latest
            use_cross: true
          - target: armv7-unknown-linux-gnueabihf
            artifact_suffix: linux-armv7
            runner: ubuntu-latest
            use_cross: true
            package: true
          - target: armv7-unknown-linux-musleabihf
            artifact_suffix: linux-armv7-musl
            runner: ubuntu-latest
            use_cross: true
          - target: arm-unknown-linux-gnueabihf
            artifact_suffix: linux-armv6-hf
            runner: ubuntu-latest
            use_cross: true
          - target: arm-unknown-linux-musleabihf
            artifact_suffix: linux-armv6-musl
            runner: ubuntu-latest
            use_cross: true
          - target: arm-unknown-linux-gnueabi
            artifact_suffix: linux-armv6
            runner: ubuntu-latest
            use_cross: true
          - target: armv5te-unknown-linux-gnueabi
            artifact_suffix: linux-armv5
            runner: ubuntu-latest
            use_cross: true
          - target: riscv64gc-unknown-linux-gnu
            artifact_suffix: linux-riscv64
            runner: ubuntu-latest
            use_cross: true
          - target: x86_64-apple-darwin
            artifact_suffix: macos-x86_64
            runner: macos-latest
            use_cross: false
          - target: aarch64-apple-darwin
            artifact_suffix: macos-aarch64
            runner: macos-latest
            use_cross: false

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust stable
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install cross
        if: matrix.use_cross
        uses: taiki-e/install-action@v2
        with:
          tool: cross

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-${{ matrix.target }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-${{ matrix.target }}-cargo-

      - name: Build
        run: |
          if [ "${{ matrix.use_cross }}" = "true" ]; then
            cross build --release --locked --target ${{ matrix.target }}
          else
            cargo build --release --locked --target ${{ matrix.target }}
          fi

      - name: Rename binary
        env:
          TARGET: ${{ matrix.target }}
          SUFFIX: ${{ matrix.artifact_suffix }}
        run: |
          cp "target/${TARGET}/release/${BINARY_NAME}" "${BINARY_NAME}-${SUFFIX}"

      - name: Upload binary artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.BINARY_NAME }}-${{ matrix.artifact_suffix }}
          path: ${{ env.BINARY_NAME }}-${{ matrix.artifact_suffix }}
          retention-days: 1

      - name: Install packaging tools
        if: matrix.package
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-deb,cargo-generate-rpm

      - name: Build .deb
        if: matrix.package
        env:
          TARGET: ${{ matrix.target }}
        run: cargo deb --no-build --no-strip --target "$TARGET"

      - name: Upload .deb artifact
        if: matrix.package
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.BINARY_NAME }}-${{ matrix.artifact_suffix }}-deb
          path: target/${{ matrix.target }}/debian/*.deb
          retention-days: 1

      - name: Build .rpm
        if: matrix.package
        env:
          TARGET: ${{ matrix.target }}
        run: cargo generate-rpm --target "$TARGET"

      - name: Upload .rpm artifact
        if: matrix.package
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.BINARY_NAME }}-${{ matrix.artifact_suffix }}-rpm
          path: target/${{ matrix.target }}/generate-rpm/*.rpm
          retention-days: 1

  publish:
    name: Publish to crates.io
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust stable
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-publish-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-publish-cargo-

      - name: Publish
        run: cargo publish --locked
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  release:
    name: Create release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true

      - name: Generate PKGBUILD and .SRCINFO
        env:
          TAG: ${{ github.ref_name }}
        run: |
          VERSION="${TAG#v}"
          URL="https://github.com/goastler/snortal/archive/refs/tags/${TAG}.tar.gz"
          SHA256=$(curl -sL "$URL" | sha256sum | cut -d' ' -f1)
          cat > artifacts/PKGBUILD << EOF
          # Maintainer: George Oastler <goastler4 at gmail dot com>
          # SPDX-License-Identifier: 0BSD
          pkgname=snortal
          pkgver=${VERSION}
          pkgrel=1
          pkgdesc='Detect captive portal URLs on a Linux network'
          arch=('x86_64' 'aarch64' 'armv7h' 'i686')
          url='https://github.com/goastler/snortal'
          license=('AGPL-3.0-or-later')
          makedepends=('cargo')
          source=("\$pkgname-\$pkgver.tar.gz::https://github.com/goastler/snortal/archive/refs/tags/v\${pkgver}.tar.gz")
          sha256sums=('${SHA256}')

          prepare() {
              cd "\$pkgname-\$pkgver"
              cargo fetch --locked --target "\$CARCH-unknown-linux-gnu"
          }

          build() {
              cd "\$pkgname-\$pkgver"
              export RUSTUP_TOOLCHAIN=stable
              export CARGO_TARGET_DIR=target
              cargo build --frozen --release
          }

          check() {
              cd "\$pkgname-\$pkgver"
              cargo test --frozen
          }

          package() {
              cd "\$pkgname-\$pkgver"
              install -Dm0755 -t "\$pkgdir/usr/bin/" "target/release/\$pkgname"
          }
          EOF
          cat > artifacts/.SRCINFO << EOF
          pkgbase = snortal
          	pkgdesc = Detect captive portal URLs on a Linux network
          	pkgver = ${VERSION}
          	pkgrel = 1
          	url = https://github.com/goastler/snortal
          	arch = x86_64
          	arch = aarch64
          	arch = armv7h
          	arch = i686
          	license = AGPL-3.0-or-later
          	makedepends = cargo
          	source = snortal-${VERSION}.tar.gz::https://github.com/goastler/snortal/archive/refs/tags/v${VERSION}.tar.gz
          	sha256sums = ${SHA256}

          pkgname = snortal
          EOF

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: artifacts/*
          generate_release_notes: true

  aur-publish:
    name: Publish to AUR
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Set up AUR SSH
        env:
          AUR_SSH_KEY: ${{ secrets.AUR_SSH_KEY }}
        run: |
          mkdir -p ~/.ssh
          chmod 700 ~/.ssh
          echo "$AUR_SSH_KEY" > ~/.ssh/aur
          chmod 600 ~/.ssh/aur
          ssh-keyscan -H aur.archlinux.org >> ~/.ssh/known_hosts

      - name: Push to AUR
        env:
          TAG: ${{ github.ref_name }}
        run: |
          VERSION="${TAG#v}"
          URL="https://github.com/goastler/snortal/archive/refs/tags/${TAG}.tar.gz"
          SHA256=$(curl -sL "$URL" | sha256sum | cut -d' ' -f1)

          export GIT_SSH_COMMAND="ssh -i ~/.ssh/aur -o StrictHostKeyChecking=yes"
          git clone ssh://aur@aur.archlinux.org/snortal.git aur-repo || {
            git init aur-repo
            git -C aur-repo remote add origin ssh://aur@aur.archlinux.org/snortal.git
          }

          cat > aur-repo/PKGBUILD << EOF
          # Maintainer: George Oastler <goastler4 at gmail dot com>
          # SPDX-License-Identifier: 0BSD
          pkgname=snortal
          pkgver=${VERSION}
          pkgrel=1
          pkgdesc='Detect captive portal URLs on a Linux network'
          arch=('x86_64' 'aarch64' 'armv7h' 'i686')
          url='https://github.com/goastler/snortal'
          license=('AGPL-3.0-or-later')
          makedepends=('cargo')
          source=("\$pkgname-\$pkgver.tar.gz::https://github.com/goastler/snortal/archive/refs/tags/v\${pkgver}.tar.gz")
          sha256sums=('${SHA256}')

          prepare() {
              cd "\$pkgname-\$pkgver"
              cargo fetch --locked --target "\$CARCH-unknown-linux-gnu"
          }

          build() {
              cd "\$pkgname-\$pkgver"
              export RUSTUP_TOOLCHAIN=stable
              export CARGO_TARGET_DIR=target
              cargo build --frozen --release
          }

          check() {
              cd "\$pkgname-\$pkgver"
              cargo test --frozen
          }

          package() {
              cd "\$pkgname-\$pkgver"
              install -Dm0755 -t "\$pkgdir/usr/bin/" "target/release/\$pkgname"
          }
          EOF

          cat > aur-repo/.SRCINFO << EOF
          pkgbase = snortal
          	pkgdesc = Detect captive portal URLs on a Linux network
          	pkgver = ${VERSION}
          	pkgrel = 1
          	url = https://github.com/goastler/snortal
          	arch = x86_64
          	arch = aarch64
          	arch = armv7h
          	arch = i686
          	license = AGPL-3.0-or-later
          	makedepends = cargo
          	source = snortal-${VERSION}.tar.gz::https://github.com/goastler/snortal/archive/refs/tags/v${VERSION}.tar.gz
          	sha256sums = ${SHA256}

          pkgname = snortal
          EOF

          cd aur-repo
          git config user.name "George Oastler"
          git config user.email "goastler4@gmail.com"
          git add PKGBUILD .SRCINFO
          if ! git diff --cached --quiet; then
            git commit -m "Update to ${TAG}"
            git push origin HEAD:master
          fi