name: Publish to npm

on:
  push:
    tags:
      - "v*"

jobs:
  publish:
    name: Build and publish to npm
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write

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

      - name: Verify tag matches package.json version
        run: |
          TAG_VERSION="${GITHUB_REF_NAME#v}"
          PKG_VERSION=$(node -p "require('./package.json').version")
          if [ "$TAG_VERSION" != "$PKG_VERSION" ]; then
            echo "Tag ($TAG_VERSION) does not match package.json version ($PKG_VERSION)."
            exit 1
          fi

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "24"
          registry-url: "https://registry.npmjs.org"

      - name: Install dependencies
        run: npm ci

      - name: Build
        run: npm run build

      # Always publish under the beta dist-tag. Promote to latest manually:
      #   npm dist-tag add @agility/content-fetch@<version> latest
      - name: Publish
        run: npm publish --access public --tag beta
