From 3890edc560726d899e5b56c11a0cbb24a9a7cdad Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Wed, 24 Jul 2024 21:32:01 -0400 Subject: [PATCH] docs(core): feature Nx Cloud sections more prominently in CLI tutorials (#27077) Updated these tutorials: - https://nx-dev-git-docs-cli-tutorials-promote-cloud-nrwl.vercel.app/getting-started/tutorials/npm-workspaces-tutorial - https://nx-dev-git-docs-cli-tutorials-promote-cloud-nrwl.vercel.app/getting-started/tutorials/react-standalone-tutorial - https://nx-dev-git-docs-cli-tutorials-promote-cloud-nrwl.vercel.app/getting-started/tutorials/react-monorepo-tutorial - https://nx-dev-git-docs-cli-tutorials-promote-cloud-nrwl.vercel.app/getting-started/tutorials/angular-standalone-tutorial - https://nx-dev-git-docs-cli-tutorials-promote-cloud-nrwl.vercel.app/getting-started/tutorials/angular-monorepo-tutorial - https://nx-dev-git-docs-cli-tutorials-promote-cloud-nrwl.vercel.app/getting-started/tutorials/vue-standalone-tutorial - https://nx-dev-git-docs-cli-tutorials-promote-cloud-nrwl.vercel.app/getting-started/tutorials/gradle-tutorial - https://nx-dev-git-docs-cli-tutorials-promote-cloud-nrwl.vercel.app/recipes/adopting-nx/adding-to-monorepo - https://nx-dev-git-docs-cli-tutorials-promote-cloud-nrwl.vercel.app/recipes/adopting-nx/adding-to-existing-project --- .../migration/adding-to-existing-project.md | 14 ++-- docs/shared/migration/adding-to-monorepo.md | 16 ++-- docs/shared/tutorials/angular-monorepo.md | 15 ++-- docs/shared/tutorials/angular-standalone.md | 15 ++-- docs/shared/tutorials/gradle.md | 74 +++++++++++++------ docs/shared/tutorials/npm-workspaces.md | 72 +++++++++++------- docs/shared/tutorials/react-monorepo.md | 15 ++-- docs/shared/tutorials/react-standalone.md | 15 ++-- docs/shared/tutorials/vue-standalone.md | 15 ++-- .../src/lib/table-of-contents.tsx | 28 ++++++- .../src/lib/nodes/heading.component.tsx | 14 +++- .../src/lib/nodes/heading.schema.ts | 1 + 12 files changed, 202 insertions(+), 92 deletions(-) diff --git a/docs/shared/migration/adding-to-existing-project.md b/docs/shared/migration/adding-to-existing-project.md index 68f371c06abf2..130d4d3af5d1f 100644 --- a/docs/shared/migration/adding-to-existing-project.md +++ b/docs/shared/migration/adding-to-existing-project.md @@ -1,6 +1,6 @@ # Adding Nx to your Existing Project -Nx can be added to any type of project, not just monorepos. The main benefit is to get caching abilities for the package +Nx can be added to any type of project, not just monorepos. A large benefit of Nx is its caching feature for package scripts. Each project usually has a set of scripts in the `package.json`: ```json {% fileName="package.json" %} @@ -14,12 +14,14 @@ scripts. Each project usually has a set of scripts in the `package.json`: } ``` -You can make these scripts faster by leveraging Nx's caching capabilities. For example: +You can make these scripts faster by leveraging Nx's [caching capabilities](/features/cache-task-results). For example: - You change some spec files: in that case the `build` task can be cached and doesn't have to re-run. - You update your docs, changing a couple of markdown files: then there's no need to re-run builds, tests, linting on your CI. All you might want to do is trigger the Docusaurus build. +Additionally, Nx also [speeds up your CI ⚡](#fast-ci) with [remote caching](/ci/features/remote-cache) and [distributed task execution](/ci/features/distribute-task-execution). + ## Install Nx on a Non-Monorepo Project Run the following command: @@ -284,7 +286,7 @@ Now if you run `npm run test` or `nx test` twice, the results will be retrieved this example are as cautious as possible, so you can significantly improve the value of the cache by [customizing Nx Inputs](/recipes/running-tasks/configure-inputs) for each task. -## Set Up CI for Your Workspace +## Fast CI ⚡ {% highlightColor="green" %} This tutorial walked you through how Nx can improve the local development experience, but the biggest difference Nx makes is in CI. As repositories get bigger, making sure that the CI is fast, reliable and maintainable can get very challenging. Nx provides a solution. @@ -293,7 +295,7 @@ This tutorial walked you through how Nx can improve the local development experi - Nx Agents [efficiently distribute tasks across machines](/ci/concepts/parallelization-distribution) ensuring constant CI time regardless of the repository size. The right number of machines is allocated for each PR to ensure good performance without wasting compute. - Nx Atomizer [automatically splits](/ci/features/split-e2e-tasks) large e2e tests to distribute them across machines. Nx can also automatically [identify and rerun flaky e2e tests](/ci/features/flaky-tasks). -### Connect to Nx Cloud +### Connect to Nx Cloud {% highlightColor="green" %} Nx Cloud is a companion app for your CI system that provides remote caching, task distribution, e2e tests deflaking, better DX and more. @@ -325,7 +327,7 @@ git pull You should now have an `nxCloudAccessToken` property specified in the `nx.json` file. -### Create a CI Workflow +### Create a CI Workflow {% highlightColor="green" %} Use the following command to generate a CI workflow file. @@ -362,7 +364,7 @@ jobs: - run: npx nx affected -t lint test build ``` -### Open a Pull Request +### Open a Pull Request {% highlightColor="green" %} Commit the changes and open a new PR on GitHub. diff --git a/docs/shared/migration/adding-to-monorepo.md b/docs/shared/migration/adding-to-monorepo.md index 5644f7c5bf312..fb224dd8bc163 100644 --- a/docs/shared/migration/adding-to-monorepo.md +++ b/docs/shared/migration/adding-to-monorepo.md @@ -6,14 +6,12 @@ powering Nx underneath. As a result, Lerna gets all the modern features such as on [https://lerna.js.org/upgrade](https://lerna.js.org/upgrade). {% /callout %} -Nx has first-class support for [monorepos](/getting-started/tutorials/npm-workspaces-tutorial). As a result, if you have +Nx has first-class support for [monorepos](/getting-started/tutorials/npm-workspaces-tutorial). If you have an existing NPM/Yarn or PNPM-based monorepo setup, you can easily add Nx to get - fast [task scheduling](/features/run-tasks) -- support for [task pipelines](/concepts/task-pipeline-configuration) -- [caching](/features/cache-task-results) -- [remote caching with Nx Cloud](/ci/features/remote-cache) -- [distributed task execution with Nx Cloud](/ci/features/distribute-task-execution) +- high-performance task [caching](/features/cache-task-results) +- [fast CI ⚡](#fast-ci) with [remote caching](/ci/features/remote-cache) and [distributed task execution](/ci/features/distribute-task-execution) This is a low-impact operation because all that needs to be done is to install the `nx` package at the root level and add an `nx.json` for configuring caching and task pipelines. @@ -307,7 +305,7 @@ pnpm run -r test This allows for incrementally adopting Nx in your existing workspace. -## Set Up CI for Your Workspace +## Fast CI ⚡ {% highlightColor="green" %} This tutorial walked you through how Nx can improve the local development experience, but the biggest difference Nx makes is in CI. As repositories get bigger, making sure that the CI is fast, reliable and maintainable can get very challenging. Nx provides a solution. @@ -316,7 +314,7 @@ This tutorial walked you through how Nx can improve the local development experi - Nx Agents [efficiently distribute tasks across machines](/ci/concepts/parallelization-distribution) ensuring constant CI time regardless of the repository size. The right number of machines is allocated for each PR to ensure good performance without wasting compute. - Nx Atomizer [automatically splits](/ci/features/split-e2e-tasks) large e2e tests to distribute them across machines. Nx can also automatically [identify and rerun flaky e2e tests](/ci/features/flaky-tasks). -### Connect to Nx Cloud +### Connect to Nx Cloud {% highlightColor="green" %} Nx Cloud is a companion app for your CI system that provides remote caching, task distribution, e2e tests deflaking, better DX and more. @@ -348,7 +346,7 @@ git pull You should now have an `nxCloudAccessToken` property specified in the `nx.json` file. -### Create a CI Workflow +### Create a CI Workflow {% highlightColor="green" %} Use the following command to generate a CI workflow file. @@ -385,7 +383,7 @@ jobs: - run: npx nx affected -t lint test build ``` -### Open a Pull Request +### Open a Pull Request {% highlightColor="green" %} Commit the changes and open a new PR on GitHub. diff --git a/docs/shared/tutorials/angular-monorepo.md b/docs/shared/tutorials/angular-monorepo.md index 3d31f77da4335..893233bf8d4fa 100644 --- a/docs/shared/tutorials/angular-monorepo.md +++ b/docs/shared/tutorials/angular-monorepo.md @@ -7,12 +7,13 @@ description: In this tutorial you'll create a frontend-focused workspace with Nx In this tutorial you'll learn how to use Angular with Nx in a [monorepo (integrated) setup](/concepts/integrated-vs-package-based#integrated-repos). -What are you going to learn? +What will you learn? - how to create a new Angular application - how to run a single task (i.e. serve your app) or run multiple tasks in parallel - how to leverage code generators to scaffold components - how to modularize your codebase and impose architectural constraints for better maintainability +- [how to speed up CI with Nx Cloud ⚡](#fast-ci) {% callout type="info" title="Looking for an Angular standalone app?" %} Note, this tutorial sets up a repo with applications and libraries in their own subfolders. If you are looking for an Angular standalone app setup then check out our [Angular standalone app tutorial](/getting-started/tutorials/angular-standalone-tutorial). @@ -1238,7 +1239,11 @@ If you have the ESLint plugin installed in your IDE you should immediately see a Learn more about how to [enforce module boundaries](/features/enforce-module-boundaries). -## Set Up CI for Your Angular Monorepo +## Fast CI ⚡ {% highlightColor="green" %} + +{% callout type="check" title="Repository with Nx" %} +Make sure you have completed the previous sections of this tutorial before starting this one. If you want a clean starting point, you can check out the [reference code](https://github.com/nrwl/nx-recipes/tree/main/angular-monorepo) as a starting point. +{% /callout %} {% video-link link="https://youtu.be/ZzTP4bVJEnI?t=791" /%} @@ -1249,7 +1254,7 @@ This tutorial walked you through how Nx can improve the local development experi - Nx Agents [efficiently distribute tasks across machines](/ci/concepts/parallelization-distribution) ensuring constant CI time regardless of the repository size. The right number of machines is allocated for each PR to ensure good performance without wasting compute. - Nx Atomizer [automatically splits](/ci/features/split-e2e-tasks) large e2e tests to distribute them across machines. Nx can also automatically [identify and rerun flaky e2e tests](/ci/features/flaky-tasks). -### Connect to Nx Cloud +### Connect to Nx Cloud {% highlightColor="green" %} Nx Cloud is a companion app for your CI system that provides remote caching, task distribution, e2e tests deflaking, better DX and more. @@ -1267,7 +1272,7 @@ npx nx connect Once you click the link, follow the steps provided and make sure Nx Cloud is enabled on the main branch of your repository. -### Configure Your CI Workflow +### Configure Your CI Workflow {% highlightColor="green" %} When you chose GitHub Actions as your CI provider at the beginning of the tutorial, `create-nx-workspace` created a `.github/workflows/ci.yml` file that contains a CI pipeline that will run the `lint`, `test`, `build` and `e2e` tasks for projects that are affected by any given PR. Since we are using Nx Cloud, the pipeline will also distribute tasks across multiple machines to ensure fast and reliable CI runs. @@ -1304,7 +1309,7 @@ jobs: - run: npx nx affected -t lint test build ``` -### Open a Pull Request +### Open a Pull Request {% highlightColor="green" %} Commit the changes and open a new PR on GitHub. diff --git a/docs/shared/tutorials/angular-standalone.md b/docs/shared/tutorials/angular-standalone.md index 47e058d1c63a9..256a1e2096435 100644 --- a/docs/shared/tutorials/angular-standalone.md +++ b/docs/shared/tutorials/angular-standalone.md @@ -2,12 +2,13 @@ In this tutorial you'll learn how to use Angular with Nx in a ["standalone" (non-monorepo) setup](/concepts/integrated-vs-package-based#standalone-applications). Not to be confused with the "Angular Standalone API", a standalone project in Nx is a non-monorepo setup where you have a single application at the root level. This setup is very similar to what the Angular CLI gives you. -What are you going to learn? +What will you learn? - how to create a new standalone (single-project) Nx workspace setup for Angular - how to run a single task (i.e. serve your app) or run multiple tasks in parallel - how to leverage code generators to scaffold components - how to modularize your codebase and impose architectural constraints for better maintainability +- [how to speed up CI with Nx Cloud ⚡](#fast-ci) {% callout type="info" title="Looking for Angular monorepos?" %} Note, this tutorial sets up a repo with a single application at the root level that breaks out its code into libraries to add structure. If you are looking for an Angular monorepo setup then check out our [Angular monorepo tutorial](/getting-started/tutorials/angular-monorepo-tutorial). @@ -1041,7 +1042,11 @@ When you are ready to add another application to the repo, you'll probably want You can also go through the full [Angular monorepo tutorial](/getting-started/tutorials/angular-monorepo-tutorial) -## Set Up CI for the Angular App +## Fast CI ⚡ {% highlightColor="green" %} + +{% callout type="check" title="Repository with Nx" %} +Make sure you have completed the previous sections of this tutorial before starting this one. If you want a clean starting point, you can check out the [reference code](https://github.com/nrwl/nx-recipes/tree/main/angular-standalone) as a starting point. +{% /callout %} This tutorial walked you through how Nx can improve the local development experience, but the biggest difference Nx makes is in CI. As repositories get bigger, making sure that the CI is fast, reliable and maintainable can get very challenging. Nx provides a solution. @@ -1050,7 +1055,7 @@ This tutorial walked you through how Nx can improve the local development experi - Nx Agents [efficiently distribute tasks across machines](/ci/concepts/parallelization-distribution) ensuring constant CI time regardless of the repository size. The right number of machines is allocated for each PR to ensure good performance without wasting compute. - Nx Atomizer [automatically splits](/ci/features/split-e2e-tasks) large e2e tests to distribute them across machines. Nx can also automatically [identify and rerun flaky e2e tests](/ci/features/flaky-tasks). -### Connect to Nx Cloud +### Connect to Nx Cloud {% highlightColor="green" %} Nx Cloud is a companion app for your CI system that provides remote caching, task distribution, e2e tests deflaking, better DX and more. @@ -1068,7 +1073,7 @@ npx nx connect Once you click the link, follow the steps provided and make sure Nx Cloud is enabled on the main branch of your repository. -### Configure Your CI Workflow +### Configure Your CI Workflow {% highlightColor="green" %} When you chose GitHub Actions as your CI provider at the beginning of the tutorial, `create-nx-workspace` created a `.github/workflows/ci.yml` file that contains a CI pipeline that will run the `lint`, `test`, `build` and `e2e` tasks for projects that are affected by any given PR. Since we are using Nx Cloud, the pipeline will also distribute tasks across multiple machines to ensure fast and reliable CI runs. @@ -1105,7 +1110,7 @@ jobs: - run: npx nx affected -t lint test build ``` -### Open a Pull Request +### Open a Pull Request {% highlightColor="green" %} Commit the changes and open a new PR on GitHub. diff --git a/docs/shared/tutorials/gradle.md b/docs/shared/tutorials/gradle.md index aaf3e945d3f41..dd7d0d2428c31 100644 --- a/docs/shared/tutorials/gradle.md +++ b/docs/shared/tutorials/gradle.md @@ -5,8 +5,15 @@ description: In this tutorial you'll add Nx to an existing Gradle repo # Gradle Tutorial -In this tutorial, you'll learn how to add Nx to a repository with an existing Gradle setup. You'll see how Nx can -provide immediate value. +In this tutorial, you'll learn how to add Nx to a repository with an existing Gradle setup. + +What will you learn? + +- how to add Nx to a Gradle project +- how to run a single task (i.e. serve your app) or run multiple tasks in parallel +- how to leverage code generators to scaffold components +- how to modularize your codebase and impose architectural constraints for better maintainability +- [how to speed up CI with Nx Cloud ⚡](#fast-ci) ## Prerequisites @@ -31,11 +38,7 @@ node -v This tutorial picks up where [Spring framework](https://spring.io/)'s guide for [Multi-Module Projects](https://spring.io/guides/gs/multi-module) leaves off. -Fork [the sample repository](https://github.com/nrwl/gradle-tutorial): - -[https://github.com/nrwl/gradle-tutorial](https://github.com/nrwl/gradle-tutorial) - -And then clone it on your local machine: +Fork [the sample repository](https://github.com/nrwl/gradle-tutorial/fork), and then clone it on your local machine: ```shell git clone https://github.com//gradle-tutorial.git @@ -66,7 +69,11 @@ Root project 'gradle-tutorial' Nx is a build system with built in tooling and advanced CI capabilities. It helps you maintain and scale monorepos, both locally and on CI. We will explore the features of Nx in this tutorial by adding it to the Gradle workspace above. -To add Nx, run `npx nx@latest init`. +To add Nx, run + +```shell {% path="~/gradle-tutorial" %} +npx nx@latest init +``` This command will download the latest version of Nx and help set up your repository to take advantage of it. Nx will also detect Gradle is used in the repo so it will propose adding the `@nx/gradle` plugin to integrate Gradle with Nx. @@ -260,7 +267,11 @@ To run the `test` tasks for projects affected by this change, run: Notice that this command does not run the `test` task for the `library` project, since it could not have been affected by the code change. -## Set Up CI for Your Gradle Workspace +## Fast CI ⚡ {% highlightColor="green" %} + +{% callout type="check" title="Repository with Nx" %} +Make sure you have completed the previous sections of this tutorial before starting this one. If you want a clean starting point, you can check out the [reference code](https://github.com/nrwl/nx-recipes/tree/main/gradle) as a starting point. +{% /callout %} This tutorial walked you through how Nx can improve the local development experience, but the biggest difference Nx makes is in CI. As repositories get bigger, making sure that the CI is fast, reliable and maintainable can get very challenging. Nx provides a solution. @@ -269,7 +280,7 @@ This tutorial walked you through how Nx can improve the local development experi - Nx Agents [efficiently distribute tasks across machines](/ci/concepts/parallelization-distribution) ensuring constant CI time regardless of the repository size. The right number of machines is allocated for each PR to ensure good performance without wasting compute. - Nx Atomizer [automatically splits](/ci/features/split-e2e-tasks) large e2e tests to distribute them across machines. Nx can also automatically [identify and rerun flaky e2e tests](/ci/features/flaky-tasks). -### Connect to Nx Cloud +### Connect to Nx Cloud {% highlightColor="green" %} Nx Cloud is a companion app for your CI system that provides remote caching, task distribution, e2e tests deflaking, better DX and more. @@ -282,7 +293,7 @@ Now that we're working on the CI pipeline, it is important for your changes to b Now connect your repository to Nx Cloud with the following command: ```shell -npx nx connect +./nx connect ``` A browser window will open to register your repository in your [Nx Cloud](https://cloud.nx.app) account. The link is also printed to the terminal if the windows does not open, or you closed it before finishing the steps. The app will guide you to create a PR to enable Nx Cloud on your repository. @@ -301,7 +312,7 @@ git pull You should now have an `nxCloudAccessToken` property specified in the `nx.json` file. -### Create a CI Workflow +### Create a CI Workflow {% highlightColor="green" %} Let's create a branch to add a CI workflow. @@ -312,16 +323,26 @@ git checkout -b add-workflow And use the following command to generate a CI workflow file. ```shell -npx nx generate ci-workflow --ci=github +./nx generate ci-workflow --ci=github ``` This generator creates a `.github/workflows/ci.yml` file that contains a CI pipeline that will run the `lint`, `test`, `build` and `e2e` tasks for projects that are affected by any given PR. Since we are using Nx Cloud, the pipeline will also distribute tasks across multiple machines to ensure fast and reliable CI runs. The key lines in the CI pipeline are: -```yml {% fileName=".github/workflows/ci.yml" highlightLines=["10-14", "21-22"] %} +```yml {% fileName=".github/workflows/ci.yml" highlightLines=["21-24", "38-39"] %} name: CI -# ... + +on: + push: + branches: + - main + pull_request: + +permissions: + actions: read + contents: read + jobs: main: runs-on: ubuntu-latest @@ -329,22 +350,29 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 0 + # This enables task distribution via Nx Cloud # Run this command as early as possible, before dependencies are installed # Learn more at https://nx.dev/ci/reference/nx-cloud-cli#npx-nxcloud-startcirun - # Connect your workspace by running "nx connect" and uncomment this - - run: npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build" - - uses: actions/setup-node@v3 + - run: npx nx-cloud start-ci-run --distribute-on="3 linux-medium-jvm" --stop-agents-after="build" + + - name: Set up JDK 17 for x64 + uses: actions/setup-java@v4 with: - node-version: 20 - cache: 'npm' - - run: npm ci --legacy-peer-deps + java-version: '17' + distribution: 'temurin' + architecture: x64 + + - name: Setup Gradle + uses: gradle/gradle-build-action@v2 + - uses: nrwl/nx-set-shas@v4 + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - - run: npx nx affected -t lint test build + - run: ./nx affected -t test build ``` -### Open a Pull Request +### Open a Pull Request {% highlightColor="green" %} Commit the changes and open a new PR on GitHub. diff --git a/docs/shared/tutorials/npm-workspaces.md b/docs/shared/tutorials/npm-workspaces.md index 5663831a84ab1..5d2c6b7202233 100644 --- a/docs/shared/tutorials/npm-workspaces.md +++ b/docs/shared/tutorials/npm-workspaces.md @@ -5,12 +5,16 @@ description: In this tutorial you'll add Nx to an existing NPM workspaces repo # NPM Workspaces Tutorial -In this tutorial, you'll learn how to add Nx to a repository with an existing NPM workspaces setup. You'll see how Nx can provide immediate value with very little configuration and then you can gradually enable more features. +In this tutorial, you'll learn how to add Nx to a repository with an existing [NPM workspaces](https://docs.npmjs.com/cli/using-npm/workspaces) setup. -- Add Nx to the repository with a single command -- Configure caching for your existing tasks -- Configure a task pipeline -- Use Nx Plugins to automatically configure caching +What will you learn? + +- how to add Nx to the repository with a single command +- how to configure caching for your tasks +- how to configure a task pipeline +- how to configure projects automatically with Nx Plugins +- how to manage your releases with `nx release` +- [how to speed up CI with Nx Cloud ⚡](#fast-ci)