name: Publish v2.x on: workflow_dispatch: inputs: releaseType: type: choice description: Release type (release for main branch, prerelease for feature branches) required: true default: prerelease options: - release - prerelease - dry-run branch: type: string description: Branch to create pre-release from (only applies to prerelease/dry-run). required: false skipLernaVersion: type: boolean description: Skip `lerna version` and E2E for release/prerelease recovery runs, then only run the publish step. Use this to recover from a partial npm publish where some packages were already released. required: false default: false jobs: authorize: name: Authorize and Verify Packages on NPM runs-on: ubuntu-latest steps: - name: Check branch protection env: RELEASE_TYPE: ${{ github.event.inputs.releaseType }} REF_NAME: ${{ github.ref_name }} run: | if [ "$RELEASE_TYPE" = "dry-run" ]; then echo "✅ Branch check skipped: dry-run mode allows any branch" echo "Current branch: $REF_NAME" exit 0 fi case "$REF_NAME" in main|hotfix/*) echo "✅ Branch check passed: workflow is running from allowed branch ($REF_NAME)" ;; *) echo "❌ This workflow can only be triggered from main or hotfix/* branches." echo "Current branch: $REF_NAME" exit 1 ;; esac - name: ${{ github.actor }} permission check to do a release run: | permission=$(gh api "repos/$GITHUB_REPOSITORY/collaborators/$GITHUB_ACTOR/permission" --jq '.permission') case "$permission" in write|admin) echo "Release permission check passed: $permission" ;; *) echo "Release requires write access; $GITHUB_ACTOR has $permission access"; exit 1 ;; esac env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # The package check reads packages/*/package.json from the ref being published # and calls `pnpm view`, so it needs the repo on disk plus Node and pnpm. # No dependency install is required. - name: Check out git repository uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1 with: ref: ${{ github.event.inputs.branch || github.ref_name }} - name: Setup Node.js and pnpm uses: ./.github/actions/setup-node-pnpm with: install: 'false' - name: Verify packages exist on npm run: pnpm deploy:publish:check-packages - name: Verify pinned custom enrichment version is already published run: | version=$(node -p "require('./packages/analytics-browser/package.json').dependencies['@amplitude/plugin-custom-enrichment-browser']") bash scripts/publish/verify-custom-enrichment.sh "$version" e2e: name: E2E Tests runs-on: ubuntu-latest needs: [authorize] container: image: mcr.microsoft.com/playwright:v1.55.1 outputs: head_sha: ${{ steps.head-sha.outputs.sha }} steps: - name: Skip E2E for recovery publish if: ${{ github.event.inputs.skipLernaVersion == 'true' }} run: echo "Skipping E2E because skipLernaVersion is true" - name: Check out git repository if: ${{ github.event.inputs.skipLernaVersion != 'true' }} uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1 with: fetch-depth: 0 ref: ${{ github.event.inputs.branch || github.ref_name }} - name: Capture E2E HEAD SHA id: head-sha if: ${{ github.event.inputs.skipLernaVersion != 'true' }} run: | git config --global --add safe.directory "$GITHUB_WORKSPACE" echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" - name: Run E2E tests if: ${{ github.event.inputs.skipLernaVersion != 'true' }} uses: ./.github/actions/e2e-test with: amplitude-api-key: ${{ secrets.AMPLITUDE_API_KEY }} build-and-test: name: Build and Test runs-on: ubuntu-latest needs: [authorize] steps: - name: Skip build and test for recovery publish if: ${{ github.event.inputs.skipLernaVersion == 'true' }} run: echo "Skipping build and test because skipLernaVersion is true" - name: Check out git repository if: ${{ github.event.inputs.skipLernaVersion != 'true' }} uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1 with: fetch-depth: 0 ref: ${{ github.event.inputs.branch || github.ref_name }} - name: Build and Test if: ${{ github.event.inputs.skipLernaVersion != 'true' }} uses: ./.github/actions/build-and-test with: node-version: 24.x configure-git: 'false' configure-aws: 'false' e2e-react-native: name: E2E Tests - React Native (${{ matrix.platform }}, newArch=${{ matrix.new-arch }}) runs-on: ${{ matrix.runs-on }} needs: [authorize] timeout-minutes: 60 strategy: fail-fast: false matrix: include: - platform: ios new-arch: 'true' runs-on: macos-15 - platform: ios new-arch: 'false' runs-on: macos-15 - platform: android new-arch: 'true' runs-on: ubuntu-latest - platform: android new-arch: 'false' runs-on: ubuntu-latest env: LANG: en_US.UTF-8 LC_ALL: en_US.UTF-8 steps: - name: Skip E2E for recovery publish if: ${{ github.event.inputs.skipLernaVersion == 'true' }} run: echo "Skipping E2E because skipLernaVersion is true" - name: Check out git repository if: ${{ github.event.inputs.skipLernaVersion != 'true' }} uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1 with: fetch-depth: 0 ref: ${{ github.event.inputs.branch || github.ref_name }} - name: Run React Native E2E harness if: ${{ github.event.inputs.skipLernaVersion != 'true' }} uses: ./.github/actions/e2e-react-native with: platform: ${{ matrix.platform }} new-arch: ${{ matrix.new-arch }} deploy: name: Publish SDK packages runs-on: ubuntu-latest environment: npm-release needs: [authorize, build-and-test, e2e, e2e-react-native] if: ${{ github.event.inputs.releaseType == 'release' }} permissions: id-token: write # Required for OIDC contents: write strategy: matrix: node-version: [24.x] # Node 24.6+ bundles npm 11.5.1+ for OIDC; pnpm 11 needs Node 22+. steps: - name: Validate protected branch env: REF: ${{ github.event.inputs.branch || github.ref_name }} run: | case "$REF" in main) echo "✅ Branch check passed: main" ;; hotfix/*) echo "✅ Branch check passed: hotfix branch ($REF)" ;; *) echo "❌ Deploy can only run from main or hotfix/* branches." echo "Current branch: $REF" exit 1 ;; esac # Mint a short-lived GitHub App installation token. The app is on the branch # ruleset bypass list, so it can push the lerna version commit + tags directly # to the protected branch (a plain PAT/GITHUB_TOKEN is rejected by the ruleset). - name: Generate GitHub App token id: app-token uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 with: app-id: ${{ secrets.AMPLITUDE_DEV_EXP_APP_ID }} private-key: ${{ secrets.AMPLITUDE_DEV_EXP_PRIVATE_KEY }} - name: Check out git repository uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1 with: fetch-depth: 0 token: ${{ steps.app-token.outputs.token }} ref: ${{ github.event.inputs.branch || github.ref_name }} # Normal releases already ran unit tests/lint in the build-and-test job. # Recovery publishes (skipLernaVersion) skip that job, so run them here. - name: ${{ github.event.inputs.skipLernaVersion == 'true' && 'Build and Test' || 'Build' }} uses: ./.github/actions/build-and-test with: node-version: ${{ matrix.node-version }} run-tests: ${{ github.event.inputs.skipLernaVersion }} run-lint: ${{ github.event.inputs.skipLernaVersion }} - name: Verify ref has not advanced since e2e checkout if: ${{ github.event.inputs.skipLernaVersion != 'true' }} env: EXPECTED_SHA: ${{ needs.e2e.outputs.head_sha }} REF: ${{ github.event.inputs.branch || github.ref_name }} run: bash scripts/publish/check-ref-not-advanced.sh # Only create release version when using from-git (default behavior) # from-package mode uses existing package.json versions and doesn't need git tags - name: Create release version if: ${{ github.event.inputs.skipLernaVersion != 'true' }} run: | echo "Running lerna version..." # Stream output so runner shutdowns and canceled runs retain Lerna's progress. LOG_FILE=$(mktemp) trap 'rm -f "$LOG_FILE"' EXIT set +e GH_TOKEN=${{ steps.app-token.outputs.token }} pnpm deploy:version -y --no-private --create-release github 2>&1 | tee "$LOG_FILE" EXIT_CODE=${PIPESTATUS[0]} set -e if [ $EXIT_CODE -ne 0 ]; then echo "❌ Command failed with exit code: $EXIT_CODE" exit $EXIT_CODE fi # Check if the output indicates no changed packages if grep -q "No changed packages to version" "$LOG_FILE"; then echo "❌ No changed packages found to version. Failing the job." exit 1 fi echo "✅ Successfully created release version" # Custom enrichment is released independently; all SDK packages can go live together. - name: Publish SDK packages to NPM run: | GH_TOKEN=${{ steps.app-token.outputs.token }} pnpm deploy:publish env: S3_BUCKET_NAME: ${{ secrets.S3_BUCKET_NAME }} - name: Determine analytics-browser release if: ${{ github.event.inputs.releaseType == 'release' }} id: linear-version run: | version=$(node -p "require('./packages/analytics-browser/package.json').version") echo "version=$version" >> "$GITHUB_OUTPUT" if git diff --name-only HEAD~1 HEAD -- packages/analytics-browser/package.json | grep -q .; then echo "published=true" >> "$GITHUB_OUTPUT" else echo "published=false" >> "$GITHUB_OUTPUT" fi - name: Sync release to Linear if: ${{ steps.linear-version.outputs.published == 'true' }} continue-on-error: true uses: linear/linear-release-action@3b634623620454bac33390451a1dd15ba78e3dc4 # v0 with: access_key: ${{ secrets.LINEAR_ACCESS_KEY }} command: sync version: ${{ steps.linear-version.outputs.version }} prerelease: name: Publish SDK prerelease runs-on: ubuntu-latest environment: npm-release needs: [authorize, build-and-test, e2e, e2e-react-native] if: ${{ github.event.inputs.releaseType == 'prerelease' || github.event.inputs.releaseType == 'dry-run' }} permissions: id-token: write # Required for OIDC contents: write strategy: matrix: node-version: [24.x] # Node 24.6+ bundles npm 11.5.1+ for OIDC; pnpm 11 needs Node 22+. steps: - name: Determine branch to use id: determine-branch env: BRANCH: ${{ github.event.inputs.branch }} run: | if [ -n "$BRANCH" ]; then if ! git check-ref-format --branch "$BRANCH"; then echo "Invalid branch name: $BRANCH" exit 1 fi echo "branch=$BRANCH" >> "$GITHUB_OUTPUT" else echo "❌ No branch specified. Please specify a branch to create pre-release from." exit 1 fi - name: Generate GitHub App token id: app-token uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0 with: app-id: ${{ secrets.AMPLITUDE_DEV_EXP_APP_ID }} private-key: ${{ secrets.AMPLITUDE_DEV_EXP_PRIVATE_KEY }} - name: Check out git repository uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1 with: ref: ${{ steps.determine-branch.outputs.branch }} fetch-depth: 0 token: ${{ steps.app-token.outputs.token }} # Normal releases already ran unit tests/lint in the build-and-test job. # Recovery publishes (skipLernaVersion) skip that job, so run them here. - name: ${{ github.event.inputs.skipLernaVersion == 'true' && 'Build and Test' || 'Build' }} uses: ./.github/actions/build-and-test with: node-version: ${{ matrix.node-version }} run-tests: ${{ github.event.inputs.skipLernaVersion }} run-lint: ${{ github.event.inputs.skipLernaVersion }} # Keep alphanumeric characters and hyphens, remove other invalid characters # Examples: # - SR-1858 -> SR-1858 # - feature/my-branch -> featuremy-branch # - fix_bug_123 -> fixbug123 # - user@company.com -> usercompanycom - name: Transform feature branch name id: prerelease-id env: BRANCH: ${{ steps.determine-branch.outputs.branch }} run: | preid=$(echo "$BRANCH" | tr -cd '[:alnum:]-') echo "PREID=$preid" >> "$GITHUB_ENV" echo "preid=$preid" >> "$GITHUB_OUTPUT" # Use --no-push to prevent pushing to remote # Version example: 1.0.0 -> 1.1.0-{preid}.0 - name: Dry run pre-release version if: ${{ github.event.inputs.releaseType == 'dry-run' }} env: GH_TOKEN: ${{ steps.app-token.outputs.token }} PREID: ${{ env.PREID }} run: | pnpm deploy:version:dry-run -y --preid "$PREID" - name: Pre-release version if: ${{ github.event.inputs.releaseType == 'prerelease' && github.event.inputs.skipLernaVersion != 'true' }} env: GH_TOKEN: ${{ steps.app-token.outputs.token }} PREID: ${{ env.PREID }} BRANCH: ${{ steps.determine-branch.outputs.branch }} run: | pnpm deploy:version -y --no-private --conventional-prerelease --preid "$PREID" --allow-branch "$BRANCH" --create-release github - name: Publish SDK prerelease packages to NPM if: ${{ github.event.inputs.releaseType == 'prerelease' }} env: GH_TOKEN: ${{ steps.app-token.outputs.token }} PREID: ${{ env.PREID }} S3_BUCKET_NAME: ${{ secrets.S3_BUCKET_NAME }} run: | pnpm deploy:publish --ignore-scripts --tag "$PREID" - name: Dry run publish release to NPM if: ${{ github.event.inputs.releaseType == 'dry-run' }} env: DRY_RUN: true run: | pnpm deploy:publish:dry-run