name: Publish Package
on:
  workflow_dispatch:
    inputs:
      workspace_path:
        description: 'The workspace to publish'
        required: true
        default: 'packages/shared/common'
        type: choice
        options:
          - packages/shared/common
          - packages/shared/sdk-server
          - packages/shared/sdk-server-edge
          - packages/sdk/cloudflare
          - packages/sdk/server-node
          - packages/sdk/vercel
      prerelease:
        description: 'Is this a prerelease. If so, then the latest tag will not be updated in npm.'
        type: boolean
        required: true
      dry_run:
        description: 'Is this a dry run. If so no package will be published.'
        type: boolean
        required: true

jobs:
  build-publish:
    runs-on: ubuntu-latest
    # Needed to get tokens during publishing.
    permissions:
      id-token: write
      contents: read
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 16.x
          registry-url: 'https://registry.npmjs.org'
      - name: 'Set WORKSPACE_NAME variable'
        run: |
          WORKSPACE_NAME=$(./scripts/package-name.sh ${{ inputs.workspace_path }})
          echo "WORKSPACE_NAME=$WORKSPACE_NAME" >> $GITHUB_ENV
      - id: build-and-test
        # Build using the same steps from CI.
        name: Build and Test
        uses: ./actions/ci
        with:
          workspace_name: ${{ env.WORKSPACE_NAME }}
          workspace_path: ${{ inputs.workspace_path }}
      - uses: ./actions/release-secrets
        name: 'Get NPM token'
        with:
          aws_assume_role: ${{ vars.AWS_ROLE_ARN }}
          ssm_parameter_pairs: '/production/common/releasing/npm/token = NODE_AUTH_TOKEN'
      - name: Setup .yarnrc.yml
        shell: bash
        run: |
          yarn config set npmScopes.launchdarkly.npmRegistryServer "https://registry.npmjs.org"
          yarn config set npmScopes.launchdarkly.npmAlwaysAuth true
          yarn config set npmScopes.launchdarkly.npmAuthToken $NODE_AUTH_TOKEN
      - id: publish
        name: Publish Package
        uses: ./actions/publish
        with:
          workspace_name: ${{ env.WORKSPACE_NAME }}
          prerelease: ${{ inputs.prerelease }}
          dry_run: ${{ inputs.dry_run }}
