tree2md 0.14.0

Like the tree command, but outputs in Markdown. Optimized for AI agents.
name: Release
on:
  push:
    tags: [ 'v*' ]

# Hardening:
# - Third-party actions are pinned to full commit SHAs (tags can be moved).
# - The token is read-only by default; each job asks only for what it needs.
# - No build cache: release binaries are built from a clean state, so a cache
#   written by another workflow run cannot leak into them.
# - Builds and publish use the committed Cargo.lock (--locked).
# - Each archive gets a signed build provenance attestation, so a replaced
#   download (and its .sha256) fails `gh attestation verify`.
permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    permissions:
      contents: read
      id-token: write      # sign the provenance attestation
      attestations: write  # store it
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            ext: ""
            pack: tar.gz
          - os: macos-latest
            target: aarch64-apple-darwin
            ext: ""
            pack: tar.gz
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            ext: .exe
            pack: zip

    steps:
      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
        with:
          persist-credentials: false
      - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable, 2026-10-16
        with:
          toolchain: stable
          targets: ${{ matrix.target }}

      - name: Verify tag == Cargo.toml
        shell: bash
        run: |
          TAG="${GITHUB_REF_NAME}"
          VERSION=$(grep '^version *= *"' Cargo.toml | sed 's/.*"\(.*\)".*/\1/')
          if [ "v$VERSION" != "$TAG" ]; then
            echo "::error::Tag($TAG) != Cargo.toml(v$VERSION)"
            exit 1
          fi
          echo "OK: $TAG matches Cargo.toml"

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

      - name: Prepare package
        shell: bash
        run: |
          BIN="tree2md${{ matrix.ext }}"
          OUT="tree2md-${{ matrix.target }}"
          mkdir -p dist/$OUT
          cp target/${{ matrix.target }}/release/$BIN dist/$OUT/
          cp README.md dist/$OUT/ 2>/dev/null || true
          cp LICENSE dist/$OUT/ 2>/dev/null || true
          cd dist
          if [ "${{ matrix.pack }}" = "zip" ]; then
            7z a -tzip "$OUT.zip" "$OUT"
            # Use certutil on Windows for SHA256
            certutil -hashfile "$OUT.zip" SHA256 | head -2 | tail -1 | tr -d ' ' > "$OUT.sha256"
            echo "  $OUT.zip" >> "$OUT.sha256"
          else
            tar czf "$OUT.tar.gz" "$OUT"
            shasum -a 256 "$OUT.tar.gz" > "$OUT.sha256"
          fi

      - name: Attest build provenance
        uses: actions/attest-build-provenance@977bb373ede98d70efdf65b84cb5f73e068dcc2a # v3.0.0
        with:
          subject-path: dist/tree2md-${{ matrix.target }}.${{ matrix.pack }}

      - name: Upload artifact
        uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
        with:
          name: tree2md-${{ matrix.target }}
          path: dist/tree2md-${{ matrix.target }}.*

  release:
    name: Create GitHub Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write  # create the release and upload assets
    steps:
      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
        with:
          fetch-depth: 0
          persist-credentials: false

      - uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
        with:
          path: artifacts

      # Extract release notes from CHANGELOG.md for this version
      - name: Extract release notes from CHANGELOG.md
        id: notes
        run: |
          TAG="${GITHUB_REF_NAME}"           # e.g. v0.5.0
          VER="${TAG#v}"                     # e.g. 0.5.0

          # Extract the section for this version from CHANGELOG.md
          awk -v ver="$VER" '
            $0 ~ "^## \\[" ver "\\]" {p=1; next}
            p && $0 ~ "^## \\[" {exit}
            p && $0 ~ "^\\[Unreleased\\]:" {exit}
            p
          ' CHANGELOG.md > RELEASE_NOTES.md

          # Add header with version and date
          echo "## tree2md ${TAG}" > RELEASE_NOTES_FINAL.md
          echo "" >> RELEASE_NOTES_FINAL.md
          cat RELEASE_NOTES.md >> RELEASE_NOTES_FINAL.md

          echo "---- RELEASE NOTES ----"
          cat RELEASE_NOTES_FINAL.md

      - name: Create release
        uses: softprops/action-gh-release@3bb12739c298aeb8a4eeaf626c5b8d85266b0e65 # v2.6.2
        with:
          name: "tree2md ${{ github.ref_name }}"
          files: artifacts/**/tree2md-*
          body_path: RELEASE_NOTES_FINAL.md
          generate_release_notes: true
          append_body: true
          draft: false
          prerelease: false

  publish:
    name: Publish to crates.io
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write      # trusted publishing: exchange the job's OIDC token for a crates.io token
    steps:
      - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
        with:
          fetch-depth: 0
          persist-credentials: false
      - uses: dtolnay/rust-toolchain@b725eda525362e7cf34af9a7243cac1e3f01a7ec # stable, 2026-10-16
        with:
          toolchain: stable

      - name: Ensure tag == Cargo.toml (strict)
        run: |
          TAG="${GITHUB_REF_NAME}"
          VERSION=$(grep '^version *= *"' Cargo.toml | sed 's/.*"\(.*\)".*/\1/')
          [ "v$VERSION" = "$TAG" ] || (echo "::error::Tag($TAG) != v$VERSION"; exit 1)
          echo "OK: $TAG == v$VERSION"

      - name: Check not already published
        run: |
          VERSION=$(grep '^version *= *"' Cargo.toml | sed 's/.*"\(.*\)".*/\1/')
          # Simple check - fails if version already exists, passes if unpublished (404) or not found
          cargo search tree2md --limit 1 | grep -q "tree2md = \"$VERSION\"" && \
            (echo "::error::Version $VERSION already on crates.io"; exit 1) || true

      # Trusted publishing: crates.io issues a token valid for this job only
      # (revoked in the action's post step), for this repository's
      # release.yml as configured in the crate's settings. No long-lived
      # registry token is stored in the repository.
      - name: Authenticate with crates.io
        id: crates-io-auth
        uses: rust-lang/crates-io-auth-action@3268eb413133e2960fa4ae0e963ca76380c54cd9 # v1.0.6

      - name: Publish
        env:
          # Read by cargo from the environment; not passed on the command line.
          CARGO_REGISTRY_TOKEN: ${{ steps.crates-io-auth.outputs.token }}
        run: cargo publish --locked