Publish Conformance Rules to Nx Cloud

Nx Cloud Enterprise allows you to publish your organization's Nx Conformance rules to your Nx Cloud Organization, and consume them in any of your other Nx Workspaces without having to deal with the complexity and friction of dealing with a private NPM registry or similar. Authentication is handled automatically through your Nx Cloud connection and rules are downloaded and applied based on your preferences configured in the Nx Cloud UI.

Let's create a custom rule which we can then publish to Nx Cloud. We will first create a new library project to contain our rule (and any others we might create in the future):

nx generate @nx/js:library cloud-conformance-rules

The Nx Cloud distribution mechanism expects each rule to be created in a named subdirectory in the src/ directory of our new project, and each rule directory to contain an index.ts and a schema.json file. You can read more about creating a conformance rule in the dedicated guide. For this recipe, we'll generate a default rule to use in the publishing process.

nx g @nx/powerpack-conformance:create-rule --name=test-cloud-rule --directory=cloud-conformance-rules/src/test-cloud-rule --category=reliability --description="A test cloud rule" --reporter=non-project-files-reporter

We now have a valid implementation of a rule and we are ready to build it and publish it to Nx Cloud. The @nx/powerpack-conformance plugin provides a dedicated executor called bundle-rules for creating appropriate build artifacts for this purpose, so we will wire that executor up to a new build target in our cloud-conformance-rules project's project.json file:

cloud-conformance-rules/project.json
1{ 2 // ...any existing project.json content 3 "targets": { 4 // ...any existing targets 5 "build": { 6 "executor": "@nx/powerpack-conformance:bundle-rules", 7 "outputs": ["{options.outputPath}"], 8 "options": { 9 "outputPath": "{projectRoot}/dist" 10 } 11 } 12 } 13} 14

We can now run nx build cloud-conformance-rules to build our rule and create the build artifacts in the cloud-conformance-rules/dist directory (or wherever you prefer to configure that outputPath location). If we take a look at the output path location we will see the same structure of one named subdirectory per rule, now containing the (bundled) index.js and schema.json files.

Our final step is to publish the rule artifacts to Nx Cloud. We achieve this by running the publish-conformance-rules command on the nx-cloud CLI, passing the output path location as the first positional argument:

nx-cloud publish-conformance-rules cloud-conformance-rules/dist

Subsequent calls to this command will overwrite the previously published rule artifacts for that rule, including implementation and schema changes. Effectively, the rules are always "at HEAD" and do not therefore have explicit versioning. If you need to support different versions of various setups, you should write the rule implementation to handle it at runtime. This approach helps reduce a lot of complexity and friction when managing Nx Conformance configurations across your organization.

Because publishing the rules is a relatively common operation, you can also wire up a target in your cloud-conformance-rules project to wrap the CLI command. Therefore, including our build target from before, our project.json file now looks like this:

cloud-conformance-rules/project.json
1{ 2 // ...any existing project.json content 3 "targets": { 4 "build": { 5 "executor": "@nx/powerpack-conformance:bundle-rules", 6 "outputs": ["{options.outputPath}"], 7 "options": { 8 "outputPath": "{projectRoot}/dist" 9 } 10 }, 11 "publish": { 12 "dependsOn": ["build"], 13 "command": "npx nx-cloud publish-conformance-rules {projectRoot}/dist" 14 } 15 } 16} 17

We can now run nx publish cloud-conformance-rules to both build and publish our rule (and any future rules in this project) to Nx Cloud.