mostro 0.17.4

Lightning Network peer-to-peer nostr platform
name: Build and Test for all targets

on:
  push:
    tags: ['v*.*.*']          # run only when a tag like v1.2.3 is pushed
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  # Generate changelog (runs once)
  changelog:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    outputs:
      content: ${{ steps.changelog.outputs.content }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Ensure all tags are present
        run: git fetch --tags --force --prune

      - name: Generate changelog
        uses: orhun/git-cliff-action@v4
        id: changelog
        with:
          config: cliff.toml
          # Use the latest tag vs previous tag automatically
          args: --latest --strip header --output CHANGELOG.md
        env:
          GITHUB_REPOSITORY: ${{ github.repository }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_REF: ${{ github.ref }}

  # Test the code
  test:
    runs-on: ubuntu-latest
    needs: changelog
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - name: Install protobuf compiler
        run: sudo apt-get update && sudo apt-get install -y protobuf-compiler
      - name: Cache cargo
        uses: Swatinem/rust-cache@v2
        with: { cache-on-failure: true }
      - name: cargo test
        run: cargo test --locked

  # Prepare matrix of targets from archs file
  matrix:
    runs-on: ubuntu-latest
    needs: test
    outputs:
      targets: ${{ steps.set-matrix.outputs.targets }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - id: set-matrix
        name: Read targets from archs
        shell: bash
        run: |
          set -euo pipefail
          targets=$(grep -v '^\s*$' archs | jq -R -s -c 'split("\n") | map(select(length>0))')
          echo "targets=$targets" >> "$GITHUB_OUTPUT"

  # Build binaries for all targets using cross
  build:
    runs-on: ubuntu-latest
    needs: matrix
    strategy:
      fail-fast: false
      matrix:
        target: ${{ fromJSON(needs.matrix.outputs.targets) }}
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

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

      - name: Cache cross binary
        id: cache-cross
        uses: actions/cache@v4
        with:
          path: ~/.cargo/bin/cross
          key: cross-${{ runner.os }}-0.2.5

      - name: Install cross ( pinned version for reproducibility )
        if: steps.cache-cross.outputs.cache-hit != 'true'
        run: cargo install cross --version 0.2.5

      - name: Build release
        run: cross build --release --locked --target ${{ matrix.target }}

      - name: Prepare conventional artifact name
        shell: bash
        run: |
          set -euo pipefail
          outdir="artifacts"
          mkdir -p "$outdir"
          case "${{ matrix.target }}" in
            *-pc-windows-*) src="target/${{ matrix.target }}/release/mostrod.exe"; dst="$outdir/mostrod-${{ matrix.target }}.exe";;
            *)              src="target/${{ matrix.target }}/release/mostrod";     dst="$outdir/mostrod-${{ matrix.target }}";;
          esac
          cp "$src" "$dst"

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: artifact-${{ matrix.target }}
          path: artifacts/*

  # Publish to crates.io (only if all builds succeed)
  # This job will only run if both test and build jobs succeed.
  # With fail-fast: false, the build job fails if ANY matrix build fails,
  # ensuring all artifacts are built before publishing.
  publish:
    runs-on: ubuntu-latest
    needs: [test, build]
    if: success()
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - name: Install protobuf compiler
        run: sudo apt-get update && sudo apt-get install -y protobuf-compiler
      - name: Cache cargo
        uses: Swatinem/rust-cache@v2
        with: { cache-on-failure: true }
      - name: Publish to crates.io
        run: cargo publish --locked
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CRATES_IO_TOKEN }}

  # Create release with all artifacts
  release:
    runs-on: ubuntu-latest
    needs: [changelog, build]
    permissions:
      contents: write
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4

      - name: Generate checksums manifest
        shell: bash
        run: |
          set -euo pipefail
          shopt -s globstar nullglob
          # Compute SHA256 for all produced binaries
          : > manifest.txt
          for f in $(ls -1 artifact-*/mostrod* | sort); do
            sha256sum "$f" >> manifest.txt
          done

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.ref_name }}
          name: Release ${{ github.ref_name }}
          body: ${{ needs.changelog.outputs.content }}
          files: |
            artifact-*/mostrod*
            manifest.txt
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}