name: Rust Automatic Release and Publish

on:
  pull_request:
    branches:
      - main
      - support/v*
    types: [closed]

jobs:
  call-create-release-workflow:
    if: github.event.pull_request.merged == true
    uses: rk-work/esp_bno055/.github/workflows/shared-release.yml@main
    with:
      # changelog-config-path: ./.github/.github_changelog_generator
      pre-release-tag-regex: ^v[0-9]+\.[0-9]+\.[0-9]+-(?<pre_release>\w+)\.\d+$
      main-release-tag-regex: ^v[0-9]+\.[0-9]+\.[0-9]+$
      create-github-release: true
    secrets:
      GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
      GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }}

  release:
    runs-on: ubuntu-latest
    needs: call-create-release-workflow
    if: ${{ needs.call-create-release-workflow.outputs.is-release }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Set up ESP-IDF
        uses: espressif/setup-esp-idf@v1
        with:
          version: 'latest'
      - name: Publish to crates.io
        run: cargo publish --token ${{ secrets.CARGO_REGISTRY_TOKEN }}
      - name: Generate documentation
        run: cargo doc --target xtensa-esp32-none-elf
