name: Release

on:
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always

jobs:
  release:
    if: github.repository == 'blue-build/cli'
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: write
      packages: write

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/cache@v4
        with:
          path: |
            target/
            ~/.cargo/bin/
            ~/.cargo/git/db/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
          key: ${{ runner.os }}-cargo-build-stable-${{ hashFiles('**/Cargo.toml') }}

      - name: Install just
        run: sudo snap install --edge --classic just

      # Cargo returns an exit code of 1 if already installed
      - name: Install cargo-release
        continue-on-error: true
        run: cargo install cargo-release --force

      - name: Git setup
        run: |
          git config user.name github-actions
          git config user.email github-actions@github.com

      - name: Setup release
        run: just cargo-release --execute

      - name: Create PR
        uses: peter-evans/create-pull-request@v5
        with:
          delete-branch: true
          base: "main"
          title: "Preparing Next Release"
          body: |
            Preparing next release
            This PR has been auto-generated
