Skip to content

Publish to NPM

Publish to NPM #21

Workflow file for this run

name: Publish to NPM
on:
workflow_dispatch:
inputs:
package:
description: 'The full module name.'
required: true
default: '@vonage/client-sdk-video-react-native'
type: choice
options:
- 'opentok-react-native'
- '@opentok/opentok-react-native'
- '@vonage/client-sdk-video-react-native'
registry:
description: 'NPM Registry to publish to.'
required: true
default: 'npmjs.com'
type: choice
options:
- 'npmjs.com'
- 'github.com'
dry_run:
description: 'Dry run mode (test without publishing)'
required: true
type: boolean
default: true
jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Validate package and registry combination
run: |
COMBINATION="${{ inputs.package }}|${{ inputs.registry }}"
case "$COMBINATION" in
"opentok-react-native|github.com")
echo "❌ Unsupported combination: 'opentok-react-native' can only be published to npmjs.com"
exit 1
;;
"@opentok/opentok-react-native|npmjs.com")
echo "❌ Unsupported combination: '@opentok/opentok-react-native' can only be published to github.com"
exit 1
;;
esac
echo "✅ Supported combination: $COMBINATION"
- name: Checkout repository
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v6
with:
node-version: '22'
registry-url: 'https://registry.npmjs.org'
- name: Apply opentok-react-native package patch
if: inputs.package == 'opentok-react-native'
run: |
git apply patch/code-changes.patch
git apply patch/flavor-changes.patch
- name: Apply @opentok/opentok-react-native package patch
if: inputs.package == '@opentok/opentok-react-native'
run: |
git apply patch/code-changes.patch
git apply patch/code-changes-opentok-github.patch
git apply patch/flavor-changes.patch
- name: Install dependencies
run: |
npm ci
npm run prepare
- name: Determine NPM tag
id: npm-tag
run: |
VERSION="${{ github.ref_name }}"
if [[ $VERSION == *"-alpha"* ]]; then
echo "tag=alpha" >> $GITHUB_OUTPUT
echo "📦 Publishing with tag: alpha"
elif [[ $VERSION == *"-beta"* ]]; then
echo "tag=beta" >> $GITHUB_OUTPUT
echo "📦 Publishing with tag: beta"
else
echo "tag=" >> $GITHUB_OUTPUT
echo "📦 Publishing without explicit tag (npm default)"
fi
- name: Verify version matches package.json
run: |
PACKAGE_VERSION=$(npm run version:local --silent)
BRANCH_VERSION="${{ github.ref_name }}"
if [ "$PACKAGE_VERSION" != "$BRANCH_VERSION" ]; then
echo "❌ Error: Version mismatch!"
echo "Package.json has version $PACKAGE_VERSION but branch name indicates $BRANCH_VERSION"
exit 1
fi
- name: Build .npmrc file
run: |
case "${{ inputs.package }}" in
"opentok-react-native")
export NPM_SCOPE_NAME=""
export NPM_MODULE_NAME="opentok-react-native"
export NPM_AUTH_TOKEN="${{ secrets.NPM_TOKEN_OPENTOK }}"
export NPM_GITHUB_AUTH_TOKEN=""
;;
"@opentok/opentok-react-native")
export NPM_SCOPE_NAME="opentok"
export NPM_MODULE_NAME="opentok-react-native"
export NPM_AUTH_TOKEN=""
export NPM_GITHUB_AUTH_TOKEN="${{ secrets.NPM_TOKEN_OPENTOK_GITHUB }}"
;;
"@vonage/client-sdk-video-react-native")
export NPM_SCOPE_NAME="vonage"
export NPM_MODULE_NAME="client-sdk-video-react-native"
export NPM_AUTH_TOKEN="${{ secrets.NPM_TOKEN_VONAGE }}"
export NPM_GITHUB_AUTH_TOKEN="${{ secrets.NPM_TOKEN_VONAGE_GITHUB }}"
;;
*)
echo "Unsupported NPM package name: ${{ inputs.package }}"
exit 1
;;
esac
echo $NPM_SCOPE_NAME $NPM_MODULE_NAME
case "${{ inputs.registry }}" in
"npmjs.com")
export NPMRC="//registry.npmjs.org/:_authToken=${NPM_AUTH_TOKEN}"
;;
"github.com")
export NPMRC="always-auth=true\nregistry=https://npm.pkg.github.com/\n//npm.pkg.github.com/:_authToken=${NPM_GITHUB_AUTH_TOKEN}"
;;
*)
echo "Unsupported NPM registry: ${{ inputs.registry }}"
exit 1
;;
esac
printf "${NPMRC}" > .npmrc
cat .npmrc
- name: Check if version already published
run: |
VERSION="${{ github.ref_name }}"
PUBLISHED_VERSION=$(npm run version:published --silent 2>/dev/null || echo "")
if [ "$PUBLISHED_VERSION" == "$VERSION" ]; then
echo "❌ Error: Version $VERSION is already published on NPM!"
exit 1
fi
- name: Dry run - Verify package
if: inputs.dry_run == true
run: |
echo "🔍 DRY RUN MODE"
TAG="${{ steps.npm-tag.outputs.tag }}"
if [ -n "$TAG" ]; then
echo "📦 Tag: $TAG"
npm publish --dry-run --tag "$TAG"
else
echo "📦 No explicit tag (using npm default)"
npm publish --dry-run
fi
- name: Publish to NPM
if: inputs.dry_run == false
run: |
TAG="${{ steps.npm-tag.outputs.tag }}"
if [ -n "$TAG" ]; then
npm publish --tag "$TAG"
else
npm publish
fi
- name: Create tag
if: inputs.dry_run == false && inputs.package == '@vonage/client-sdk-video-react-native' && inputs.registry == 'npmjs.com'
uses: negz/create-tag@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: v${{github.ref_name }}
message: New v${{github.ref_name}} tag created!
- name: Verify publication
if: inputs.dry_run == false
run: |
sleep 10
case "${{ inputs.package }}" in
"opentok-react-native")
npm view opentok-react-native@${{ github.ref_name }} version
;;
"@opentok/opentok-react-native")
npm view @opentok/opentok-react-native@${{ github.ref_name }} version
;;
"@vonage/client-sdk-video-react-native")
npm view @vonage/client-sdk-video-react-native@${{ github.ref_name }} version
;;
*)
echo "Unsupported NPM package name: ${{ inputs.package }}"
exit 1
;;
esac
- name: Summary
if: always()
run: |
echo "## Publish Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "- **Package**: ${{ inputs.package }}" >> $GITHUB_STEP_SUMMARY
echo "- **Version**: ${{ github.ref_name }}" >> $GITHUB_STEP_SUMMARY
echo "- **Dry Run**: ${{ inputs.dry_run }}" >> $GITHUB_STEP_SUMMARY