[NX Release]: Full CI/CD automation with independent fixed versioned nx workspace libraries and apps using typescript project references #31319
electrocnic
started this conversation in
Show and Tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I dug a little bit around with available tools and practices, and for now it looks like the following usecase scenario is not fully supported (neither by nx release, nor npm, nor changesets, nor other tools or package managers?) Please correct me if I am wrong, I am happy to receive input here, this should be a bi-directional "blog" if you want.
I will work on something to make the workflow in the title possible and will share my progress and code here.
Problem Description
Initial Situation:
package.json
in every lib (individual versioning)package.json
from lib A containsB: 0.1.2
as dependency.node_modules
, which does not change if we change the source of the same library.In more Code Snippet examples from
package-lock.json
, and workspace tree, it looks like this:libA
package.json
(simplified):libB
package.json
(simplified):Available released artifacts on the self hosted artifactory:
The above
package.json
examples are using the fixed versions that they depend on, not the latest versions that are available, so be not confused.Locally they would still reference the latest source code directly in the workspace root
package-lock.json
:To achieve the fixed version reproducible build, I would need to (and will work on a tool which will also use the nx release api) "switch" this npm mode from "link" to a url pattern.
For example, external npm dependencies are resolved like this:
... and so would my own published nx libs look like too.
Another Problem Detail
Using Vite with NX with typescript leads to the following import structure in the sources:
and whenever a dependency in a
package.json
to a local lib contains a package name that can be found in the local libraries'package.json
s (see above, I have libA and libB in my workspace filetree, and both contain apackage.json
with the identifier@myorg
in it), runningnpm i
will resolve all packages that match this@myorg
name by local file references.Therefore, it is not possible to simply add
@myorg
to the.npmrc
or to delete the local file links from thepackage-lock.json
, asnpm i
would simply generate this again as it would find all the libs in the folders again.Renaming all the imports in all sources would be too error prone and cumbersome, even if automated. Also an absolute anti-pattern for ci-cd to change sources on-the-fly. It is already bad enough that I will have to script some on-the-fly changes during ci-cd release to switch to the artifact npm sources instead of local package links.
Solution Approach (Theory)
NPM Aliases
To leave the
import
statements in the source files untouched and still be able to switch the mode from file link to artifact download, I will likely publish my libaries to a slightly different npm package name: Instead of@myorg
they will be named@myorg-published
. Therefore,@myorg
packages will always be local latest live source code libraries, and@myorg-published
packages will always be prebuilt, bundled npm artifacts that are published to the registry.An npm alias looks like this:
so
@myorg/lib-b
points to the published package with a specific version.Running
npm i
now would no longer add file links, but instead really pull@myorg-published/[email protected]
from the registry, yet the import statement still just references@myorg/lib-b
.Whenever a library should locally link a fixed artifact, this approach can locally work too, not just for the release pipeline. Then we can have some libs, that reference the latest sources in the workspace, and some that reference downloaded artifacts in
node_modules
.Only thing is for now,
nx release
does not yet bump versions in npm aliases to my knowledge (not sure if it does in the meantime), would be a nice-to have, for now I must script this synchronization on my own.Also, in order to switch back to the local reference, one has to remove the alias and revert the package reference from
@myorg-published
to@myorg
.A custom made nx tool will handle the switching before, during and after the release progress, and will incorporate the nx release api for the actual version bumps and changelog creation and git tagging and so on.
Automated Versioning
I want to have a convention that every merge to or commit on my
dev-integration
branch triggers a snapshot (prerelease) version, with no manual user intervention, and a commit or merge on the main branch triggers a normal release. I will implement a pipeline setup that will run nx release on the pipe, meaning it will commit and push on the pipe and trigger another pipeline. The second pipeline will then care about the actual build with the previously pushed library artifacts and release the app with the new version.Practical implementation
I will continuously report my progress here. I would be interested in your feedback to this whole idea and approaches and if somebody already has something easier, more robust, or with better practices?
Beta Was this translation helpful? Give feedback.
All reactions