dvcli 0.4.0

CLI client for querying Gradle build information from Develocity
Documentation
name: Release

on:
  workflow_dispatch:

jobs:
  publish:
    name: Release, publish, and bump version
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

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

      - name: Read version from Cargo.toml
        id: version
        run: |
          VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
          echo "tag=v$VERSION" >> "$GITHUB_OUTPUT"

      - name: Check tag does not already exist
        run: |
          if git rev-parse "${{ steps.version.outputs.tag }}" >/dev/null 2>&1; then
            echo "Tag ${{ steps.version.outputs.tag }} already exists. Bump the version in Cargo.toml before releasing."
            exit 1
          fi

      - name: Run tests
        run: cargo test

      - name: Extract changelog for this release
        id: changelog
        run: |
          VERSION="${{ steps.version.outputs.version }}"
          NOTES=$(awk "/^## \[$VERSION\]/{found=1; next} found && /^## \[/{exit} found{print}" CHANGELOG.md)
          {
            echo "notes<<EOF"
            echo "$NOTES"
            echo "EOF"
          } >> "$GITHUB_OUTPUT"

      - name: Create and push tag
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag "${{ steps.version.outputs.tag }}"
          git push origin "${{ steps.version.outputs.tag }}"

      - name: Create GitHub release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ steps.version.outputs.tag }}
          body: ${{ steps.changelog.outputs.notes }}

      - name: Publish to crates.io
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Bump to next patch version
        run: |
          VERSION="${{ steps.version.outputs.version }}"
          MAJOR=$(echo "$VERSION" | cut -d. -f1)
          MINOR=$(echo "$VERSION" | cut -d. -f2)
          PATCH=$(echo "$VERSION" | cut -d. -f3)
          NEXT="${MAJOR}.${MINOR}.$((PATCH + 1))"
          sed -i "s/^version = \"$VERSION\"/version = \"$NEXT\"/" Cargo.toml
          cargo metadata --no-deps --format-version 1 > /dev/null  # refreshes Cargo.lock
          git add Cargo.toml Cargo.lock
          git commit -m "Bump version to $NEXT"
          git push origin HEAD:main