name: publish-on-tag

on:
  push:
    tags:
      - 'chrome-devtools-mcp-v*'
  workflow_dispatch:
    inputs:
      npm-publish:
        description: 'Try to publish to npm'
        default: false
        type: boolean
      mcp-publish:
        description: 'Try to publish to MCP registry'
        default: true
        type: boolean

permissions:
  id-token: write # Required for OIDC
  contents: read

jobs:
  publish-to-npm:
    runs-on: ubuntu-latest
    if: ${{ (github.event_name != 'workflow_dispatch') || (inputs.npm-publish && always()) }}
    steps:
      - name: Check out repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 2

      - name: Set up Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          cache: npm
          node-version-file: '.nvmrc'
          registry-url: 'https://registry.npmjs.org'

      - name: Install dependencies
        run: npm ci

      - name: Build and bundle
        run: npm run bundle
        env:
          NODE_ENV: 'production'

      - name: Publish chrome-devtools-mcp
        run: |
          npm publish --provenance --access public

      - name: Publish chrome-devtools
        run: |
          node --input-type=module --eval '
            import * as fs from "node:fs/promises";
            const json = await fs.readFile("package.json", "utf8");
            const pkg = JSON.parse(json);
            pkg.name = "chrome-devtools";
            await fs.writeFile("package.json", JSON.stringify(pkg, null, 2) + "\n");
          '
          echo 'This is the **Chrome DevTools for agents** package. Docs: https://github.com/ChromeDevTools/chrome-devtools-mcp' > README.md
          npm publish --provenance --access public

  publish-to-mcp-registry:
    runs-on: ubuntu-latest
    needs: publish-to-npm
    if: ${{ (github.event_name != 'workflow_dispatch' && needs.publish-to-npm.result == 'success') || (inputs.mcp-publish && always()) }}
    steps:
      - name: Check out repository
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 2

      - name: Set up Node.js
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          cache: npm
          node-version-file: '.nvmrc'
          registry-url: 'https://registry.npmjs.org'

      # Ensure npm 11.5.1 or later is installed
      - name: Update npm
        run: npm install -g npm@latest

      - name: Install dependencies
        run: npm ci

      - name: Build and bundle
        run: npm run bundle
        env:
          NODE_ENV: 'production'

      - name: Install MCP Publisher
        run: |
          export OS=$(uname -s | tr '[:upper:]' '[:lower:]')_$(uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')
          curl -L "https://github.com/modelcontextprotocol/registry/releases/latest/download/mcp-publisher_${OS}.tar.gz" | tar xz mcp-publisher

      - name: Login to MCP Registry
        run: ./mcp-publisher login github-oidc

      - name: Publish to MCP Registry
        run: ./mcp-publisher publish
