name: Test
on:
  push:
  pull_request:
  workflow_dispatch:
  merge_group:
jobs:
  lint-markdown:
    uses: PurpleBooth/common-pipelines/.github/workflows/markdown-check.yml@main
  rust-checks:
    uses: PurpleBooth/common-pipelines/.github/workflows/rust-check.yml@main
  specdown:
    uses: PurpleBooth/common-pipelines/.github/workflows/specdown-check.yml@main
  docker-build:
    uses: PurpleBooth/common-pipelines/.github/workflows/bake-build.yaml@main

  release:
    if: github.ref == 'refs/heads/main'
    needs:
    - docker-build
    - specdown
    - lint-markdown
    - rust-checks
    outputs:
      current_version: ${{ steps.current_version.outputs.current_version }}
      previous_version: ${{ steps.previous_version.outputs.previous_version }}
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@main
      with:
        fetch-depth: 0
    - uses: cargo-bins/cargo-binstall@main
      name: Install cargo binstall
    - name: Get tags
      run: git fetch --tags origin
    - shell: bash
      run: cargo binstall --no-confirm cargo-edit
    - shell: bash
      run: cargo binstall --no-confirm --locked cocogitto
    - id: previous_version
      run: |
        cog get-version | tee .version
        echo "previous_version=$( cat .version )"  >> "$GITHUB_OUTPUT"
        rm .version
      shell: bash
    - name: Semver release
      id: release
      run: |
        git config --global user.name "cog-bot"
        git config --global user.email "cog@example.com"
        cog bump --auto
    - id: current_version
      run: |
        cog get-version | tee .version
        echo "current_version=$( cat .version )"  >> "$GITHUB_OUTPUT"
        rm .version
      shell: bash

  docker-push:
    if: needs.release.outputs.current_version != needs.release.outputs.previous_version
    needs:
    - release
    uses: PurpleBooth/common-pipelines/.github/workflows/bake-push.yaml@main
    with:
      current_version: ${{ needs.release.outputs.current_version }}

  release-binary:
    if: needs.release.outputs.current_version != needs.release.outputs.previous_version
    needs:
    - release
    uses: PurpleBooth/common-pipelines/.github/workflows/release-rust-binary.yml@main
    with:
      current_version: ${{ needs.release.outputs.current_version }}
      previous_version: ${{ needs.release.outputs.previous_version }}
    secrets:
      cargo_token: ${{ secrets.CARGO_TOKEN }}
      committer_token: ${{ secrets.COMMITTER_TOKEN }}
      gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
      gpg_passphrase: ${{ secrets.GPG_PASSPHRASE }}
