Publish to npm #37
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish to npm | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| dry_run: | |
| description: > | |
| Dry run — print the version and npm dist-tag that would be used; no commit or publish. | |
| Pick the branch to publish from with the "Use workflow from" dropdown. | |
| Non-default branches publish to the npm dist-tag `beta` (not `latest`). | |
| required: false | |
| default: false | |
| type: boolean | |
| skip_versioning: | |
| description: > | |
| Skip version bump — use the version already in the repo | |
| (e.g. retry after npm publish failed but the release commit is already pushed). | |
| required: false | |
| default: false | |
| type: boolean | |
| version_override: | |
| description: > | |
| Optional. Full semver to publish (e.g. 12.6.0-beta.2). Skips conventional bump when set. | |
| Leave empty for automatic versioning. | |
| required: false | |
| default: '' | |
| type: string | |
| permissions: | |
| contents: write | |
| id-token: write | |
| jobs: | |
| ci: | |
| uses: ./.github/workflows/build.yml | |
| publish: | |
| needs: ci | |
| runs-on: ubuntu-latest | |
| # Don't try to publish from a fork of RaspberryPiFoundation/blockly. | |
| if: ${{ github.repository_owner == 'RaspberryPiFoundation' }} | |
| environment: release | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v7 | |
| with: | |
| ref: ${{ github.ref }} | |
| fetch-depth: 0 | |
| ssh-key: ${{ secrets.DEPLOY_PRIVATE_KEY }} | |
| # This uses a reverse-engineered email for the github actions bot. See | |
| # https://github.com/actions/checkout/issues/13#issuecomment-724415212 | |
| - name: Git Identity | |
| run: | | |
| git config --global user.name 'github-actions[bot]' | |
| git config --global user.email '<41898282+github-actions[bot]@users.noreply.github.com' | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v7 | |
| with: | |
| node-version: 24.x | |
| registry-url: 'https://registry.npmjs.org' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build | |
| run: npm run build | |
| - name: Determine Dist Tag | |
| id: dist | |
| env: | |
| VERSION_OVERRIDE: ${{ inputs.version_override }} | |
| DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} | |
| REF_NAME: ${{ github.ref_name }} | |
| run: | | |
| DIST_TAG=$([[ "${VERSION_OVERRIDE}" == *"-beta."* || "${REF_NAME}" != "${DEFAULT_BRANCH}" ]] && echo "beta" || echo "latest") | |
| echo "dist_tag=$DIST_TAG" >> "$GITHUB_OUTPUT" | |
| echo "NPM distribution tag: $DIST_TAG" | |
| - name: Version | |
| if: ${{ !inputs.skip_versioning }} | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| VERSION_OVERRIDE: ${{ inputs.version_override }} | |
| DRY_RUN: ${{ inputs.dry_run }} | |
| DIST_TAG: ${{ steps.dist.outputs.dist_tag }} | |
| run: | | |
| set -euo pipefail | |
| VERSION_COMMAND="npx lerna version" | |
| if [ -n "${VERSION_OVERRIDE}" ]; then | |
| VERSION_COMMAND="${VERSION_COMMAND} ${VERSION_OVERRIDE}" | |
| fi | |
| if [ "${DIST_TAG}" = "latest" ]; then | |
| VERSION=$(node -p "require('./packages/blockly/package.json').version") | |
| if [[ "${VERSION}" == *"-beta."* ]]; then | |
| VERSION_COMMAND="${VERSION_COMMAND} --conventional-graduate" | |
| fi | |
| else | |
| VERSION_COMMAND="${VERSION_COMMAND} --conventional-prerelease --preid beta" | |
| fi | |
| VERSION_COMMAND="${VERSION_COMMAND} --conventional-commits --yes" | |
| if [ "${DRY_RUN}" = "true" ]; then | |
| VERSION_COMMAND="${VERSION_COMMAND} --no-push --no-git-tag-version" | |
| echo "Dry run: would publish the following versions to npm dist-tag: ${DIST_TAG}" | |
| eval "$VERSION_COMMAND" | |
| if [ "${DIST_TAG}" = "beta" ]; then | |
| echo "GitHub release would be created as prerelease." | |
| fi | |
| else | |
| eval "$VERSION_COMMAND" | |
| fi | |
| - name: Build core package | |
| if: ${{ !inputs.dry_run }} | |
| working-directory: packages/blockly | |
| run: npm run package | |
| - name: Publish | |
| if: ${{ !inputs.dry_run }} | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| SKIP_VERSIONING: ${{ inputs.skip_versioning }} | |
| DIST_TAG: ${{ steps.dist.outputs.dist_tag }} | |
| run: | | |
| if [ "${SKIP_VERSIONING}" = "true" ]; then | |
| npx lerna publish from-package --yes --dist-tag ${DIST_TAG} --loglevel silly | |
| else | |
| npx lerna publish from-git --yes --dist-tag ${DIST_TAG} --loglevel silly | |
| fi | |
| - name: Create tarball | |
| if: ${{ !inputs.dry_run }} | |
| working-directory: packages/blockly | |
| run: npm pack ./dist | |
| - name: Create GitHub release | |
| if: ${{ !inputs.dry_run }} | |
| working-directory: packages/blockly | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| DIST_TAG: ${{ steps.dist.outputs.dist_tag }} | |
| run: | | |
| VERSION=$(node -p "require('./package.json').version") | |
| TARBALL="blockly-${VERSION}.tgz" | |
| if [ "${DIST_TAG}" == "beta" ]; then | |
| gh release create "blockly-v${VERSION}" "$TARBALL" \ | |
| --repo "$GITHUB_REPOSITORY" \ | |
| --title "blockly-v${VERSION}" \ | |
| --generate-notes \ | |
| --prerelease | |
| else | |
| gh release create "blockly-v${VERSION}" "$TARBALL" \ | |
| --repo "$GITHUB_REPOSITORY" \ | |
| --title "blockly-v${VERSION}" \ | |
| --generate-notes | |
| fi |