question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GitHub Workflow: publishing changed packages to NPM registry

See original GitHub issue

Now that we’re using a monorepo & Lerna (https://github.com/colyseus/colyseus/pull/419) to manage internal Colyseus’ packages, we need to establish a way to auto-publish updated packages on NPM.

As we are using independent mode, each package can evolve on its own versioning numbers. Every 0.14.x version should be compatible with each other. Once we introduce a breaking change - the core and all its internal packages are going to be bumped to the next version (e.g. 0.15.x)


Lerna built-in versioning and publishing tools seems to be centered around a “fixed” version number for every package, not really friendly with the “independent” mode.

  • Because of that, I’m currently having to manually run npm publish for each package that has been changed
    • (a GitHub Workflow could handle this)
  • I manually update the "version" number of package.json for that package, too.
    • (This step is fine, it gives us control)

As you can see, the colyseus bundle package uses ^ for specifying the version number of its dependencies. So if an internal package gets updated, (e.g. "@colyseus/ws-transport" is bumped to 0.14.99) - it is optional for consumers if they want to upgrade it or not. https://github.com/colyseus/colyseus/blob/a404b0fe9113392ed4c1cae7918ccd79aedc0350/bundles/colyseus/package.json#L34-L37


This thread has some useful information on approaches to take for “independent” versioning and CI: https://github.com/lerna/lerna/issues/1286

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
endelcommented, Aug 26, 2021

That’s looking really great @lpsandaruwan 👏 👏

1reaction
lpsandaruwancommented, Aug 27, 2021

Hi @endel

I created a script for capturing version changed packages and publish them for this.

const { execSync } = require('child_process');
const getPackages = require('get-monorepo-packages');
const npmview = require('npmview');
const semver = require('semver');


function publishVersionChangedPackagesList(root_dir) {
    // Get package list in a directory.
    const packages = getPackages(root_dir);
    for (index in packages) {
        const package = packages[index];
        // Get published package info and latest version.
        npmview(package["package"]["name"], function (err, version, moduleInfo) {
            if (err) {
                console.log(err);
                throw err;
            } else {
                // Check whether the directory packge is greater than the published.
                if (semver.gt(package["package"]["version"], version)) {
                    // Set NPM tokens.
                    execSync(`npm config set //registry.npmjs.org/:_authToken ${process.env.NPM_TOKEN}`,
                        { stdio: 'inherit' })
                    // Execute NPM commands
                    execSync(`cd ${package["location"]} && npm publish`, { stdio: 'inherit' });
                }
            }
        })
    }
}

publishVersionChangedPackagesList("./");

Also a Github workflow to run this script.


name: Node.js CI

on:
  push:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest
    env:
      NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

    strategy:
      matrix:
        node-version: [12.x]

    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm install get-monorepo-packages
      - run: npm install npmview
      - run: npm install semver
      - run: node ./.github/script.js

Rough work is in https://github.com/lpsandaruwan/workflow-for-colyseus-test if you want to refer. Will integrate the workflow to the Colyseus repository and open a pull request describing what variables need to be added. Please advice if it requires changes. Thanks. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Publishing Node.js packages - GitHub Docs
Introduction. This guide shows you how to create a workflow that publishes Node.js packages to the GitHub Packages and npm registries after continuous ......
Read more >
Automate npm publishing with GitHub Actions, proper ...
Use GitHub Actions to automate Node.js package publishing with Keep a Changelog format and release notes.
Read more >
Automatically Publish to npm using GitHub Actions by sergiodxa
Run the npm publish command, the --access public force the package to be public even if the name is namespaced, this command is...
Read more >
Publish private npm package with github actions and packages
npmjs is the default registry for node packages, and although it is possible to host private packages there, it costs! Using github packages...
Read more >
Publish to NPM using GitHub Actions - DEV Community ‍ ‍
First of all you need to create a new NPM token that will be used to publish packages to NPM. From NPM dashboard...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found