1
0

publish_to_npm.yml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. name: Publish to NPM
  2. # This workflow handles all publishing to npm for releases and pre-release nightly
  3. # builds. We must use a single file rather than separate workflow files because
  4. # Trusted Publishing currently supports only a single workflow file.
  5. on:
  6. release:
  7. types: [published]
  8. schedule:
  9. - cron: '0 2 * * *' # Nightly at 02:00 UTC
  10. workflow_dispatch:
  11. inputs:
  12. publish_type:
  13. description: 'Type of publish'
  14. required: true
  15. type: choice
  16. options:
  17. - release
  18. - master-nightly
  19. - minor-nightly
  20. jobs:
  21. # Determine which publish type(s) to run based on trigger
  22. setup:
  23. runs-on: ubuntu-latest
  24. outputs:
  25. matrix: ${{ steps.set-matrix.outputs.matrix }}
  26. steps:
  27. - name: Determine publish matrix
  28. id: set-matrix
  29. run: |
  30. if [ "${{ github.event_name }}" == "release" ]; then
  31. echo 'matrix=["release"]' >> $GITHUB_OUTPUT
  32. elif [ "${{ github.event_name }}" == "schedule" ]; then
  33. echo 'matrix=["master-nightly", "minor-nightly"]' >> $GITHUB_OUTPUT
  34. else
  35. echo 'matrix=["${{ inputs.publish_type }}"]' >> $GITHUB_OUTPUT
  36. fi
  37. publish:
  38. needs: setup
  39. runs-on: ubuntu-latest
  40. permissions:
  41. contents: read
  42. id-token: write
  43. strategy:
  44. fail-fast: false
  45. matrix:
  46. type: ${{ fromJson(needs.setup.outputs.matrix) }}
  47. steps:
  48. - name: Checkout
  49. uses: actions/checkout@v4
  50. with:
  51. ref: ${{ matrix.type == 'minor-nightly' && 'minor' || matrix.type == 'master-nightly' && 'master' || '' }}
  52. fetch-depth: ${{ matrix.type == 'release' && 1 || 0 }}
  53. # For nightly builds: check if there were commits in the last 24 hours
  54. - name: Check for new commits
  55. id: commit_check
  56. if: matrix.type != 'release'
  57. run: |
  58. COMMITS=$(git rev-list --count --since="24 hours" HEAD)
  59. echo "Commits found: $COMMITS"
  60. if [ "$COMMITS" -eq 0 ]; then
  61. echo "should_publish=false" >> $GITHUB_OUTPUT
  62. else
  63. echo "should_publish=true" >> $GITHUB_OUTPUT
  64. fi
  65. - name: Setup Node.js
  66. if: matrix.type == 'release' || steps.commit_check.outputs.should_publish == 'true'
  67. uses: actions/setup-node@v6
  68. with:
  69. node-version: '24.x'
  70. registry-url: 'https://registry.npmjs.org'
  71. - name: Update npm
  72. if: matrix.type == 'release' || steps.commit_check.outputs.should_publish == 'true'
  73. run: npm install -g npm@latest
  74. - name: Install dependencies
  75. if: matrix.type == 'release' || steps.commit_check.outputs.should_publish == 'true'
  76. run: npm install --no-save
  77. - name: Build
  78. if: matrix.type == 'release' || steps.commit_check.outputs.should_publish == 'true'
  79. run: npm run build
  80. # Nightly builds: configure git and bump version
  81. - name: Configure Git
  82. if: matrix.type != 'release' && steps.commit_check.outputs.should_publish == 'true'
  83. run: |
  84. git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
  85. git config --global user.name "github-actions[bot]"
  86. - name: Get current date
  87. if: matrix.type != 'release' && steps.commit_check.outputs.should_publish == 'true'
  88. id: date
  89. run: echo "date=$(date +'%Y%m%d%H%M')" >> $GITHUB_OUTPUT
  90. - name: Version packages (master-nightly)
  91. if: matrix.type == 'master-nightly' && steps.commit_check.outputs.should_publish == 'true'
  92. run: |
  93. CURRENT_VERSION=$(node -p "require('./lerna.json').version")
  94. IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION"
  95. PATCH=$((VERSION_PARTS[2] + 1))
  96. NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.${PATCH}-master-${{ steps.date.outputs.date }}"
  97. npx lerna version $NEW_VERSION --no-git-tag-version --yes --no-push --force-publish
  98. - name: Version packages (minor-nightly)
  99. if: matrix.type == 'minor-nightly' && steps.commit_check.outputs.should_publish == 'true'
  100. run: |
  101. CURRENT_VERSION=$(node -p "require('./lerna.json').version")
  102. IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION"
  103. MINOR=$((VERSION_PARTS[1] + 1))
  104. NEW_VERSION="${VERSION_PARTS[0]}.${MINOR}.0-minor-${{ steps.date.outputs.date }}"
  105. npx lerna version $NEW_VERSION --no-git-tag-version --yes --no-push --force-publish
  106. - name: Commit version changes
  107. if: matrix.type != 'release' && steps.commit_check.outputs.should_publish == 'true'
  108. run: |
  109. git add .
  110. git commit -m "chore: Bump version for ${{ matrix.type }} pre-release"
  111. # Publish to npm with appropriate dist-tag
  112. # Note: we do not use `lerna publish` because at the time of writing, it fails to publish
  113. # using the Trusted Publishing workflow for some reason.
  114. - name: Publish to NPM (release)
  115. if: matrix.type == 'release'
  116. run: npx lerna exec --no-private -- npm publish --access public
  117. - name: Publish to NPM (master-nightly)
  118. if: matrix.type == 'master-nightly' && steps.commit_check.outputs.should_publish == 'true'
  119. run: npx lerna exec --no-private -- npm publish --access public --tag master
  120. - name: Publish to NPM (minor-nightly)
  121. if: matrix.type == 'minor-nightly' && steps.commit_check.outputs.should_publish == 'true'
  122. run: npx lerna exec --no-private -- npm publish --dd --access public --tag minor
  123. - name: Skip publish (no new commits)
  124. if: matrix.type != 'release' && steps.commit_check.outputs.should_publish == 'false'
  125. run: echo "No new commits in last 24 hours – skipping ${{ matrix.type }} publish."