name: Release Job

on:
  push:
    branches:
      - main

permissions:
  id-token: write # Required for OIDC
  contents: write
  pull-requests: write
  issues: write

jobs:
  main:
    runs-on: ubuntu-latest
    environment: npm-publish
    steps:
      - uses: googleapis/release-please-action@v4
        id: release
        with:
          release-type: node

      - # these if statements ensure that a publication only occurs when a new release is created:
        if: ${{ steps.release.outputs.release_created }}
        uses: actions/checkout@v4

      - name: Tag Major/Minor Versions
        # these if statements ensure that a publication only occurs when a new release is created:
        if: ${{ steps.release.outputs.release_created }}
        run: |
          git config user.name github-actions[bot]
          git config user.email 41898282+github-actions[bot]@users.noreply.github.com
          git remote add gh-token "https://${{ secrets.GITHUB_TOKEN }}@github.com/googleapis/release-please-action.git"
          git tag -d v${{ steps.release.outputs.major }} || true
          git tag -d v${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }} || true
          git push origin :v${{ steps.release.outputs.major }} || true
          git push origin :v${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }} || true
          git tag -a v${{ steps.release.outputs.major }} -m "Release v${{ steps.release.outputs.major }}"
          git tag -a v${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }} -m "Release v${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }}"
          git push origin v${{ steps.release.outputs.major }}
          git push origin v${{ steps.release.outputs.major }}.${{ steps.release.outputs.minor }}

      - name: Publish To NPM
        # these if statements ensure that a publication only occurs when a new release is created:
        if: ${{ steps.release.outputs.release_created }}
        uses: actions/setup-node@v4
        with:
          node-version: 24
      - name: Publish To NPM
        # these if statements ensure that a publication only occurs when a new release is created:
        if: ${{ steps.release.outputs.release_created }}
        # Ensure npm 11.5.1 or later is installed
        run: npm install -g npm@latest
      - name: Publish To NPM
        # these if statements ensure that a publication only occurs when a new release is created:
        if: ${{ steps.release.outputs.release_created }}
        run: npm ci
      - name: Publish To NPM
        # these if statements ensure that a publication only occurs when a new release is created:
        if: ${{ steps.release.outputs.release_created }}
        run: npm publish
