name: Publish NPM

run-name: Publish NPM - ${{ github.event.inputs.package }}

on:
  workflow_dispatch:
    inputs:
      package:
        type: choice
        required: true
        description: 'Which package to release'
        options:
          - artifact
          - attest
          - cache
          - core
          - exec
          - github
          - glob
          - http-client
          - io
          - tool-cache
          

jobs:
  test:
    runs-on: macos-latest-large

    steps:
      - name: setup repo
        uses: actions/checkout@v5

      - name: verify package exists
        run: ls packages/${{ github.event.inputs.package }}

      - name: Set Node.js 24.x
        uses: actions/setup-node@v5
        with:
          node-version: 24.x

      - name: npm install
        run: npm install

      - name: bootstrap
        run: npm run bootstrap

      - name: build
        run: npm run build

      - name: test
        run: npm run test

      - name: pack
        run: npm pack
        working-directory: packages/${{ github.event.inputs.package }}

      - name: upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ github.event.inputs.package }}
          path: packages/${{ github.event.inputs.package }}/*.tgz

  publish:
    runs-on: macos-latest-large
    needs: test
    environment: npm-publish
    permissions:
      contents: read
      id-token: write
    steps:

      - name: Set Node.js 24.x
        uses: actions/setup-node@v5
        with:
          node-version: 24.x

      - name: download artifact
        uses: actions/download-artifact@v4
        with:
          name: ${{ github.event.inputs.package }}

      - name: publish
        run: npm publish --provenance *.tgz

      - name: notify slack on failure
        if: failure()
        run: |
          curl -X POST -H 'Content-type: application/json' --data '{"text":":pb__failed: Failed to publish a new version of ${{ github.event.inputs.package }}"}' $SLACK_WEBHOOK
        env:
          SLACK_WEBHOOK: ${{ secrets.SLACK }}

      - name: notify slack on success
        if: success()
        run: |
          curl -X POST -H 'Content-type: application/json' --data '{"text":":dance: Successfully published a new version of ${{ github.event.inputs.package }}"}' $SLACK_WEBHOOK
        env:
          SLACK_WEBHOOK: ${{ secrets.SLACK }}

