Semantic Versioning of NestJS and Angular Applications in NX Monorepo

Previous article: Accessing a NestJS and Angular Site via a Domain Name with an SSL Certificate in Kubernetes via Ingress

Connection and setup nx-semantic-release plugin for NX monorepository for automatic release creation with subsequent deployment of applications.

1. Add NX plugin for semantic versioning

For versioning we will use https://github.com/TheUnderScorer/nx-semantic-release.

Unlike https://github.com/semantic-release/semantic-releaseplugin https://github.com/TheUnderScorer/nx-semantic-release pre-runs the building of a dependency graph between libraries and applications, and then runs the release process for all related code.

After creating a release, there will be a change in the versions of applications that we check in the CI/CD configuration in order to run or exclude some steps during deployment.

Teams

npm i --save-dev @theunderscorer/nx-semantic-release

Console output

$ npm i --save-dev @theunderscorer/nx-semantic-release

removed 391 packages, changed 3 packages, and audited 2764 packages in 18s

330 packages are looking for funding
  run `npm fund` for details

52 vulnerabilities (31 moderate, 21 high)

To address issues that do not require attention, run:
  npm audit fix

To address all issues possible (including breaking changes), run:
  npm audit fix --force

Some issues need review, and may require choosing
a different dependency.

Run `npm audit` for details.

2. Add configuration for the plugin

At the moment we will not publish anything to the npm registry, so the option npm we put in false.

Create a file .nxreleaserc.json

{
  "changelog": true,
  "npm": false,
  "github": true,
  "repositoryUrl": "https://github.com/nestjs-mod/nestjs-mod-fullstack",
  "branches": ["master"]
}

3. Enable semantic versioning for our applications

Now in the deployment process we use only the root version package.json and version package.json from the NestJS application, we have to switch the root version manually when our list of dependencies changes, but let the NX plugin switch the application version.

To connect a plugin to a library or application, you need to run a special command.

Teams

npm run nx -- g @theunderscorer/nx-semantic-release:setup-project server

Console output

$ npm run nx -- g @theunderscorer/nx-semantic-release:setup-project server

> @nestjs-mod-fullstack/source@0.0.2 nx
> nx g @theunderscorer/nx-semantic-release:setup-project server


 NX  Generating @theunderscorer/nx-semantic-release:setup-project

✔ Would you want to create github releases? (Y/n) · true
✔ Would you want to create changelog file for this project? (Y/n) · true
✔ Would you want to create npm releases for this project? (Y/n) · false
✔ What tag format would you like to use for this project. Hint: you can use ${PROJECT_NAME} and ${VERSION} tokens here. · ${PROJECT_NAME}-v${VERSION}
UPDATE apps/server/project.json

4. Add additional tasks and steps to the CI/CD configuration to launch semantic versioning and create releases

We will disable automatic launch of release creation for any commit to the master and add a condition for the presence of a special label in the commit comment [release]this is necessary so that we don’t accidentally send the current code from master to the release.

Add a release creation task to .github/workflows/kubernetes.yml

name: 'Kubernetes'

on:
  push:
    branches: ['master']
env:
  REGISTRY: ghcr.io
  BASE_SERVER_IMAGE_NAME: ${{ github.repository }}-base-server
  BUILDER_IMAGE_NAME: ${{ github.repository }}-builder
  MIGRATIONS_IMAGE_NAME: ${{ github.repository }}-migrations
  SERVER_IMAGE_NAME: ${{ github.repository }}-server
  NGINX_IMAGE_NAME: ${{ github.repository }}-nginx
  E2E_TESTS_IMAGE_NAME: ${{ github.repository }}-e2e-tests
  COMPOSE_INTERACTIVE_NO_CLI: 1
  NX_DAEMON: false
  NX_PARALLEL: 1
  NX_SKIP_NX_CACHE: true
  DISABLE_SERVE_STATIC: true
jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      contents: write # to be able to publish a GitHub release
      issues: write # to be able to comment on released issues
      pull-requests: write # to be able to comment on released pull requests
      id-token: write # to enable use of OIDC for npm provenance
    steps:
      - uses: actions/checkout@v4
        if: ${{ contains(github.event.head_commit.message, '[release]') }}
      - run: npm install --prefer-offline --no-audit --progress=false
        if: ${{ contains(github.event.head_commit.message, '[release]') }}
      - run: npm run nx -- run-many --target=semantic-release --all --parallel=1
        if: ${{ contains(github.event.head_commit.message, '[release]') }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# ...

In all tasks that begin with check... add task dependency releasesince the version may change in the task release and all further tasks should receive information about this.

We are updating .github/workflows/kubernetes.yml

# ...
jobs:
  # ...
  check-base-server-image:
    runs-on: ubuntu-latest
    needs: [release]
    # ...
  check-builder-image:
    runs-on: ubuntu-latest
    needs: [release]
    # ...
  check-migrations-image:
    runs-on: ubuntu-latest
    needs: [release]
    # ...
  check-server-image:
    runs-on: ubuntu-latest
    needs: [release]
    # ...
  check-nginx-image:
    runs-on: ubuntu-latest
    needs: [release]
    # ...
  check-e2e-tests-image:
    runs-on: ubuntu-latest
    needs: [release]
    # ...

5. Commit the changes and wait for CI/CD to work successfully

Current CI/CD work result: https://github.com/nestjs-mod/nestjs-mod-fullstack/actions/runs/10879176772

Conclusion

Since the work with the dependent code graph occurs inside the plugin, we do not need to use the command nx affected.

At the moment, there is a small amount of code in the project, so there is no point in using it. affectedbut in the future as the code base grows affected will be implemented to cache and speed up the processes of building and linting code.

Plans

In the next post I'll add git hooks to pre-format code when committing, and also add frontend versioning to prevent unnecessary release creation runs…

Links

https://nestjs.com – official website of the framework
https://nestjs-mod.com – official site of additional utilities
https://fullstack.nestjs-mod.com – site from the post
https://github.com/nestjs-mod/nestjs-mod-fullstack – project from the post
https://github.com/nestjs-mod/nestjs-mod-fullstack/compare/49806d9680fd8045172597e930e69185fabe33cf..2190202deeb42cd6176123c4d574653b849ef5ed – changes

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *