dbpulse 0.9.1

command line tool to monitor that database is available for read & write
Documentation
---
name: Deploy

on:
  push:
    tags:
      - '[0-9]+.[0-9]+.[0-9]+'
      - '[0-9]+.[0-9]+.[0-9]+-*'
      - 't*'
  workflow_dispatch:

permissions:
  contents: write
  packages: write

jobs:
  test:
    uses: ./.github/workflows/test.yml

  build:
    name: Build and release
    runs-on: ${{ matrix.os }}
    needs:
      - test

    strategy:
      matrix:
        include:
          - build: linux-amd64
            os: ubuntu-latest
            target: x86_64-unknown-linux-musl

          - build: linux-arm64
            os: ubuntu-latest
            target: aarch64-unknown-linux-musl

          - build: macos
            os: macos-latest
            target: x86_64-apple-darwin

    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Get package name from Cargo.toml
        id: package
        shell: bash
        run: |
          PACKAGE_NAME=$(awk -F '"' '/^name = / {print $2; exit}' Cargo.toml)
          echo "name=$PACKAGE_NAME" >> $GITHUB_OUTPUT
          echo "Package name: $PACKAGE_NAME"

      - name: Get the release version from the tag
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV

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

      # Linux-only prerequisites
      - name: Install system deps (musl)
        if: matrix.build == 'linux-amd64'
        run: |
          sudo apt-get update -y
          sudo apt-get install -y musl-dev musl-tools

      - name: Install packaging tools (RPM/DEB)
        if: matrix.build == 'linux-amd64'
        run: |
          cargo install cargo-generate-rpm
          cargo install cargo-deb --force --locked

      - name: Install cross for ARM64
        if: matrix.build == 'linux-arm64'
        run: cargo install cross --git https://github.com/cross-rs/cross

      # Build for Linux amd64 (musl)
      - name: Build Linux amd64 (musl)
        if: matrix.build == 'linux-amd64'
        run: cargo build --release --locked --target ${{ matrix.target }}

      # Build for Linux arm64 (musl) using cross
      - name: Build Linux arm64 (musl) with cross
        if: matrix.build == 'linux-arm64'
        run: cross build --release --locked --target ${{ matrix.target }}

      # Strip once and prep paths for both packagers
      - name: Strip and prepare packaging paths (Linux)
        if: matrix.build == 'linux-amd64'
        run: |
          BIN="target/${{ matrix.target }}/release/${{ steps.package.outputs.name }}"
          ls -al "$BIN"
          strip -s "$BIN" || true
          # Ensure cargo-generate-rpm's asset path exists without copying the file
          mkdir -p target/release
          ln -sf "../${{ matrix.target }}/release/${{ steps.package.outputs.name }}" "target/release/${{ steps.package.outputs.name }}"
          ls -al target/release/${{ steps.package.outputs.name }}

      - name: Generate RPM
        if: matrix.build == 'linux-amd64'
        run: |
          cargo generate-rpm
          RPM_PATH="$(find target/generate-rpm -type f -name '*.rpm' -print -quit)"
          echo "RPM_ASSET=$RPM_PATH" >> $GITHUB_ENV
          echo "RPM: $RPM_PATH"

      - name: Generate DEB (musl static)
        if: matrix.build == 'linux-amd64'
        run: |
          cargo deb --no-build
          DEB_PATH="$(find target/debian -maxdepth 1 -type f -name '*.deb' -print -quit)"
          echo "DEB_ASSET=$DEB_PATH" >> $GITHUB_ENV
          echo "DEB: $DEB_PATH"

      # Non-Linux builds (macOS)
      - name: Build
        if: "!startsWith(matrix.build, 'linux')"
        run: cargo build --release --locked --target ${{ matrix.target }}

      - name: Build archive
        shell: bash
        run: |
          binary_name="${{ steps.package.outputs.name }}"
          dirname="$binary_name-${{ env.VERSION }}-${{ matrix.target }}"
          mkdir "$dirname"
          cp "target/${{ matrix.target }}/release/$binary_name" "$dirname"
          tar -czf "$dirname.tar.gz" "$dirname"
          echo "ASSET=$dirname.tar.gz" >> $GITHUB_ENV

      # Upload Linux binaries as artifacts for container build
      - name: Upload Linux binary artifact
        if: startsWith(matrix.build, 'linux')
        uses: actions/upload-artifact@v7
        with:
          name: dbpulse-${{ matrix.build }}
          path: target/${{ matrix.target }}/release/${{ steps.package.outputs.name }}
          retention-days: 1

      # Skip release if the tag starts with 't'
      - name: Release
        if: startsWith(github.ref, 'refs/tags/') && !startsWith(github.ref_name, 't')
        uses: softprops/action-gh-release@v3
        with:
          draft: false
          files: |-
            ${{ env.ASSET }}
            ${{ env.RPM_ASSET }}
            ${{ env.DEB_ASSET }}

  publish:
    name: Publish
    runs-on: ubuntu-latest
    needs:
      - build
    # Only publish to crates.io on tag pushes that DON'T start with 't'
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') && !startsWith(github.ref_name,
      't')
    continue-on-error: true
    steps:
      - name: Checkout sources
        uses: actions/checkout@v6

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

      - run: cargo publish --token ${CRATES_TOKEN}
        env:
          CRATES_TOKEN: ${{ secrets.CRATES_TOKEN }}

  container:
    name: Build and publish container image
    runs-on: ubuntu-latest
    needs:
      - build
    # Build containers on all tag pushes
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      # Download pre-built binaries from build job
      - name: Download amd64 binary
        uses: actions/download-artifact@v8
        with:
          name: dbpulse-linux-amd64
          path: ./bin/amd64

      - name: Download arm64 binary
        uses: actions/download-artifact@v8
        with:
          name: dbpulse-linux-arm64
          path: ./bin/arm64

      - name: Make binaries executable
        run: |
          chmod +x ./bin/amd64/dbpulse
          chmod +x ./bin/arm64/dbpulse
          ls -la ./bin/*/dbpulse

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4

      - name: Log in to GitHub Container Registry
        if: ${{ !startsWith(github.ref_name, 't') }}
        uses: docker/login-action@v4
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Extract metadata (tags, labels)
        id: meta
        uses: docker/metadata-action@v6
        with:
          images: ghcr.io/${{ github.repository }}
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            type=raw,value=latest,enable={{is_default_branch}}

      - name: Build and push container image
        uses: docker/build-push-action@v7
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          # Only push to registry if tag doesn't start with 't' (test tag)
          push: ${{ !startsWith(github.ref_name, 't') }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          provenance: false
          sbom: false
          cache-from: type=gha
          cache-to: type=gha,mode=max