name: Publish to npm
run-name: Publishing rabbitmq-lib ${{ github.ref_name }} to npm

on:
  workflow_dispatch:
  push:
    branches:
      - master

# npm Trusted Publishing: the package is configured on npmjs.com to trust
# this repository + workflow filename, so publishing authenticates via OIDC
# (no NPM_TOKEN required). Requires npm >= 11.5.1 (Node 24 ships it).
permissions:
  contents: read
  id-token: write

jobs:
  publish:
    # The job runs in a container so the service is reachable by its hostname
    # ("rabbitmq") - on VM runners services are only reachable via
    # localhost:mapped-port. Node 24 ships npm >= 11.5.1, required for OIDC
    # trusted publishing.
    container: node:24-bookworm
    runs-on: ubuntu-latest
    services:
      rabbitmq:
        image: rabbitmq:3.13-alpine
        ports:
          - 5672:5672
        env:
          RABBITMQ_DEFAULT_USER: rabbitmq
          RABBITMQ_DEFAULT_PASS: rabbitmq
          RABBITMQ_DEFAULT_VHOST: /
        options: >-
          --health-cmd "rabbitmq-diagnostics -q ping"
          --health-interval 10s
          --health-timeout 5s
          --health-retries 10
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '24.x'
          registry-url: 'https://registry.npmjs.org'
      - name: Installing dependencies
        run: npm ci
      - name: Testing
        run: npm test
      - name: Publishing to NPM
        run: npm publish
