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.
E.g.
1cloud-conformance-rules/
2├── src/
3│ ├── test-cloud-rule/
4│ │ ├── index.ts // Our rule implementation
5│ │ └── schema.json // The schema definition for the options supported by our rule
6
Our simple rule implementation in test-cloud-rule/index.ts
, that will currently not report any violations, might look like this:
1import { createConformanceRule } from '@nx/powerpack-conformance';
2
3export default createConformanceRule<object>({
4 name: 'test-cloud-rule',
5 category: 'reliability',
6 description: 'A test cloud rule',
7 reporter: 'non-project-files-reporter',
8 implementation: async () => {
9 return {
10 severity: 'low',
11 details: {
12 violations: [],
13 },
14 };
15 },
16});
17
And because we do not yet have any options that we want to support for our rule, our schema.json
file will looks like this (using the JSON Schema format):
1{
2 "$schema": "http://json-schema.org/draft-07/schema#",
3 "type": "object",
4 "properties": {},
5 "additionalProperties": false
6}
7
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:
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:
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.