htb-cli 0.1.4

Hack The Box CLI
name: Release

on:
  workflow_dispatch:

permissions:
  contents: write

jobs:
  prepare:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.knope.outputs.version }}
      should_release: ${{ steps.knope.outputs.should_release }}
    steps:
      - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3
        id: app-token
        with:
          client-id: ${{ secrets.CLIENT_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}

      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
        with:
          fetch-depth: 0
          token: ${{ steps.app-token.outputs.token }}

      - uses: knope-dev/action@19617851f9f13ab2f27a05989c55efb18aca3675 # v2.1.2
        with:
          version: 0.23.0

      - name: Prepare release
        id: knope
        run: |
          knope release --dry-run 2>&1 | tee /tmp/knope-output.txt || true
          VERSION=$(grep -oP '(?:Bumping version to |Would add the following to Cargo\.toml: version = )\K[0-9]+\.[0-9]+\.[0-9]+' /tmp/knope-output.txt || echo "")
          if [ -n "$VERSION" ]; then
            echo "version=$VERSION" >> "$GITHUB_OUTPUT"
            echo "should_release=true" >> "$GITHUB_OUTPUT"
          else
            echo "should_release=false" >> "$GITHUB_OUTPUT"
          fi

      - name: Configure git identity
        if: steps.knope.outputs.should_release == 'true'
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"

      - name: Prepare release with knope
        if: steps.knope.outputs.should_release == 'true'
        run: knope release

      - name: Commit, tag, push, and create release
        if: steps.knope.outputs.should_release == 'true'
        run: |
          git add -A
          git commit -m "chore: prepare release v${{ steps.knope.outputs.version }}"
          git tag "v${{ steps.knope.outputs.version }}"
          git push origin main --tags
          gh release create "v${{ steps.knope.outputs.version }}" \
            --title "v${{ steps.knope.outputs.version }}" \
            --notes-file CHANGELOG.md
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token }}

  build:
    needs: prepare
    if: needs.prepare.outputs.should_release == 'true'
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            binary: htb
            archive: htb-linux-x86_64.tar.gz
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            binary: htb
            archive: htb-linux-aarch64.tar.gz
            cross: true
          - target: x86_64-apple-darwin
            os: macos-latest
            binary: htb
            archive: htb-macos-x86_64.tar.gz
          - target: aarch64-apple-darwin
            os: macos-latest
            binary: htb
            archive: htb-macos-aarch64.tar.gz
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3
        id: app-token
        with:
          client-id: ${{ secrets.CLIENT_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}

      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
        with:
          ref: v${{ needs.prepare.outputs.version }}

      - uses: dtolnay/rust-toolchain@e97e2d8cc328f1b50210efc529dca0028893a2d9 # v1
        with:
          toolchain: "1.97"
          targets: ${{ matrix.target }}

      - name: Install cross
        if: matrix.cross
        run: cargo install cross --locked

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

      - name: Package
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ../../../${{ matrix.archive }} ${{ matrix.binary }}
          cd ../../..

      - name: Upload to release
        run: |
          gh release upload v${{ needs.prepare.outputs.version }} ${{ matrix.archive }}
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token }}

  publish:
    needs: [prepare, build]
    if: needs.prepare.outputs.should_release == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
        with:
          ref: v${{ needs.prepare.outputs.version }}

      - uses: dtolnay/rust-toolchain@e97e2d8cc328f1b50210efc529dca0028893a2d9 # v1
        with:
          toolchain: "1.97"

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