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

Guy Senpai
3 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.

Part 2: Configure Github Actions workflows

In this part, we will configure a Github Actions workflow for our previously created workspace. The Github Actions workflow will automate the tests, build and publish of our libraries.

Content

Create a Github personal access token

To be able to publish your package to Github packages, your Github action needs a personal access token (PAT).

Login into your Github account, click the profile icon and select Settings. In the left sidebar, click on Developer settings and select Personal access tokens. Then click on Generate new token, enter your Github password, enter a note for the token, set expiration, check the scopes repo, write:packages, delete:packages and admin:repo_hook and click Generate token. Copy the token, as we’re going to need it for the next step.

Create a Github repository secret

Navigate to your Github repository page, click Settings and then Secrets > Actions. Click on New repository secret, fill in NPM_TOKEN as the Name, paste the PAT created on the previous step inside the Value field and click on Add secret.

That’s it, now the NPM_TOKEN can be used as an environment variable inside our Github CI action.

Create the Github CI action

Before creating the Github CI action, ensure the workflows permissions on your Github repository is read/write by click on Settings in your Github repository page. Click on Actions > General and then check Read and write permissions on Workflows permissions section.

Let’s create the Github CI actions that will run on every time you push a commit, create or close a pull request to your master branch.

create the .github/workflows/ci.yml file and edit it like below:

This Github action config file is generic because it’s not depend of a package manager. You can use it with npm, yarn or pnpm. If you want to know more about Github Actions workflow configuration check this link Workflow syntax for Github Actions.

As you can see, we have one job in this file call main which is the main job of this workflow. We have many steps in this job which do:

  • Checkout to the pull request if you create a pull request
  • Checkout to the default branch if you push on master
  • Set the base and head SHAs required for nx affected commands in CI
  • Detect your package manager
  • Set your node, yarn, npm versions using Volta
  • Install PNPM package manager if you use it
  • Print node and package manager versions useful for node_modules cache
  • Use the node_modules cache following the package manager
  • Install your workspace dependencies following the package manager
  • run the projects targets like lint, test, build through affected commands.
  • Release the all projects in workspace if you push on master or close the opened pull request. You can only release the updated projects by remove the --all option in the command npx affected --target release.

You have now a Github Actions workflow ready. You can see your workflow runs in your Github repository page on Actions tab.

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

--

--