Nx monorepo : publish your libraries to Github Packages with Github Actions & Semantic release — Part 3/3

Guy Senpai
4 min readMay 17, 2022

--

Hi everyone! 👋🏾

In this series of posts, we will see together how we can use Github Actions and semantic release to publish libraries on Github packages from Nx monorepo .

If you find an error, please leave a note or a comment.

  1. Install Nx monorepo and configure Husky Git hooks
  2. Configure Github Actions workflows
  3. Create publishable libraries and configure semantic release

Part 3: Creating publishable libraries & configuring semantic release

In this part, we will create publishable libraries and configure semantic release for our libraries to release and publish them.

Contents

Create a publishable library

In a Nx monorepo you have many way to create a publishable library. You can create an Angular publishable library, a React publishable library, a Node publishable library or a Ts/Js publishable library. Here we will create a Ts publishable library.

Install @nrwl/js plugin if not installed and run the command below:

nx generate @nrwl/js:library ts-library --compiler=swc --importPath=@my-org/ts-library --publishable

The value @my-org in the importPath option most often corresponds to your Github/NPM username or organisation. It represents the scope of your library.

After publishable library created, you must updated package.json` file in your library folder like below:

package.json

As you can see, we have replace version: 0.0.1 to version: 0.0.0-semantic-release . We also add repository and publishConfig. Semantic release use it to know the current version of your package, the repository where to release and the npm registry where to publish packages.

Install and configure semantic-release in the workspace

Semantic release is an automated version management and package publishing. It is used to update version and publish automatically a single package. There is actually no support for Nx monorepo.

But there is a fork of semantic-release call semantic-release-plus that supports monorepo like Nx. We will use semantic-release-plus in this step.

Install the packages below with your package manager

semantic-release-plus semantic-release-npm-github-publish

After packages installed, create a release.config.base.js at the workspace root and edit it like below

release.config.base.js

With this configuration, Semantic release will easily publish to NPM, Github or Github Packages Registry.

Configure semantic-release in the library

Create a release.config.js in the library root directory and edit like below:

release.config.js

The library release config extends the workspace root release config and overwrite the plugins` property to adapt it to the library.

Now add a release target in your library project.json. Run the command below to add the new target in your ts-library :

nx generate @nrwl/workspace:run-commands release --command='npx semantic-release-plus --extends ./packages/ts-library/release.config.js' --project=ts-library

Edit the release target in your library project.json` by adding "parallel": false after command property like below:

release target in project.json

Your library is now ready to be published. Commit your changes and push to your repository. You can go to the Actions tab in your Github repository page to see if your workflow run success. After success Go to the Code tab in your Github repository page you will see a new release and new package added.

You can now create your libraries and publish them to Github Packages. Thank you for your reading. You can find full code in https://github.com/guysenpai/nx-workspace-core.

If this post was helpful to you, clap 👏🏾, thanks!

--

--