name: Publish to crates.io

on:
  push:
    tags:
      - "v[0-9]+.[0-9]+.[0-9]+"

env:
  CARGO_TERM_COLOR: always

jobs:
  publish:
    name: Publish ckb crate
    runs-on: ubuntu-latest
    if: ${{ github.repository_owner == 'nervosnetwork' }}
    permissions:
      contents: read
      id-token: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

      - name: Verify tag matches Cargo.toml version
        run: |
          CARGO_VERSION=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[] | select(.name == "ckb") | .version')
          TAG_VERSION="${GITHUB_REF_NAME#v}"
          if [ "$CARGO_VERSION" != "$TAG_VERSION" ]; then
            echo "Error: Tag version ($TAG_VERSION) does not match Cargo.toml version ($CARGO_VERSION)"
            exit 1
          fi
          echo "Version check passed: $CARGO_VERSION"

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