diff --git a/packages/gradle/src/generators/ci-workflow/__snapshots__/generator.spec.ts.snap b/packages/gradle/src/generators/ci-workflow/__snapshots__/generator.spec.ts.snap index 86dad706a30d9..b8fe02b4e96fe 100644 --- a/packages/gradle/src/generators/ci-workflow/__snapshots__/generator.spec.ts.snap +++ b/packages/gradle/src/generators/ci-workflow/__snapshots__/generator.spec.ts.snap @@ -17,9 +17,10 @@ jobs: steps: - checkout - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build" + # 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 + - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build" - nx/set-shas: main-branch-name: 'main' @@ -56,9 +57,10 @@ jobs: with: fetch-depth: 0 - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build" + # 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 + - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build" - name: Set up JDK 17 for x64 uses: actions/setup-java@v4 diff --git a/packages/gradle/src/generators/ci-workflow/files/circleci/.circleci/config.yml.template b/packages/gradle/src/generators/ci-workflow/files/circleci/.circleci/config.yml.template index 8c4f695c7791d..97be5eed7427a 100644 --- a/packages/gradle/src/generators/ci-workflow/files/circleci/.circleci/config.yml.template +++ b/packages/gradle/src/generators/ci-workflow/files/circleci/.circleci/config.yml.template @@ -14,10 +14,13 @@ jobs: steps: - checkout - # Connect your workspace on <%= nxCloudHost %> and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested + # 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 + <% if (connectedToCloud) { %>- run: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build" + <% } else { %># Connect your workspace by running "nx connect" and uncomment this # - run: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build" - + <% } %> - nx/set-shas: main-branch-name: '<%= mainBranch %>' diff --git a/packages/gradle/src/generators/ci-workflow/files/github/.github/workflows/__workflowFileName__.yml.template b/packages/gradle/src/generators/ci-workflow/files/github/.github/workflows/__workflowFileName__.yml.template index 938ba9624a08d..12f828c21eb60 100644 --- a/packages/gradle/src/generators/ci-workflow/files/github/.github/workflows/__workflowFileName__.yml.template +++ b/packages/gradle/src/generators/ci-workflow/files/github/.github/workflows/__workflowFileName__.yml.template @@ -18,9 +18,13 @@ jobs: with: fetch-depth: 0 - # Connect your workspace on <%= nxCloudHost %> and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested + # 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 + <% if (connectedToCloud) { %>- run: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build" + <% } else { %># Connect your workspace by running "nx connect" and uncomment this # - run: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-jvm" --stop-agents-after="build" + <% } %> - name: Set up JDK 17 for x64 uses: actions/setup-java@v4 diff --git a/packages/gradle/src/generators/ci-workflow/generator.spec.ts b/packages/gradle/src/generators/ci-workflow/generator.spec.ts index e3d25c070f1f0..4568bfb1f101d 100644 --- a/packages/gradle/src/generators/ci-workflow/generator.spec.ts +++ b/packages/gradle/src/generators/ci-workflow/generator.spec.ts @@ -1,5 +1,5 @@ import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; -import { Tree } from '@nx/devkit'; +import { readNxJson, Tree, updateNxJson } from '@nx/devkit'; import { ciWorkflowGenerator } from './generator'; @@ -8,6 +8,9 @@ describe('ci-workflow generator', () => { beforeEach(() => { tree = createTreeWithEmptyWorkspace(); + const nxJson = readNxJson(tree); + nxJson.nxCloudAccessToken = 'test'; + updateNxJson(tree, nxJson); }); describe.each([ diff --git a/packages/gradle/src/generators/ci-workflow/generator.ts b/packages/gradle/src/generators/ci-workflow/generator.ts index f548efb227a4c..c330564cfe602 100644 --- a/packages/gradle/src/generators/ci-workflow/generator.ts +++ b/packages/gradle/src/generators/ci-workflow/generator.ts @@ -3,11 +3,9 @@ import { names, generateFiles, getPackageManagerCommand, - NxJsonConfiguration, formatFiles, detectPackageManager, readNxJson, - readJson, } from '@nx/devkit'; import { join } from 'path'; import { getNxCloudUrl, isNxCloudUsed } from 'nx/src/utils/nx-cloud-utils'; @@ -47,6 +45,7 @@ interface Substitutes { packageManagerPrefix: string; commands: string[]; nxCloudHost: string; + connectedToCloud: boolean; } function getTemplateData(tree: Tree, options: Schema): Substitutes { @@ -59,7 +58,7 @@ function getTemplateData(tree: Tree, options: Schema): Substitutes { let nxCloudHost: string = 'nx.app'; try { - const nxCloudUrl = getNxCloudUrl(readJson(tree, 'nx.json')); + const nxCloudUrl = getNxCloudUrl(readNxJson(tree)); nxCloudHost = new URL(nxCloudUrl).host; } catch {} @@ -67,6 +66,8 @@ function getTemplateData(tree: Tree, options: Schema): Substitutes { const commands = options.commands ?? getCiCommands(options.ci, mainBranch); + const connectedToCloud = isNxCloudUsed(readNxJson(tree)); + return { workflowName, workflowFileName, @@ -75,6 +76,7 @@ function getTemplateData(tree: Tree, options: Schema): Substitutes { commands, mainBranch, nxCloudHost, + connectedToCloud, }; } diff --git a/packages/nx/src/utils/nx-cloud-utils.ts b/packages/nx/src/utils/nx-cloud-utils.ts index dc2407021eb3d..aac5225af9f1b 100644 --- a/packages/nx/src/utils/nx-cloud-utils.ts +++ b/packages/nx/src/utils/nx-cloud-utils.ts @@ -1,4 +1,4 @@ -import { NxJsonConfiguration, readNxJson } from '../config/nx-json'; +import { NxJsonConfiguration } from '../config/nx-json'; export function isNxCloudUsed(nxJson: NxJsonConfiguration): boolean { return ( diff --git a/packages/workspace/src/generators/ci-workflow/__snapshots__/ci-workflow.spec.ts.snap b/packages/workspace/src/generators/ci-workflow/__snapshots__/ci-workflow.spec.ts.snap index 552b31e974b87..57942a6787bbf 100644 --- a/packages/workspace/src/generators/ci-workflow/__snapshots__/ci-workflow.spec.ts.snap +++ b/packages/workspace/src/generators/ci-workflow/__snapshots__/ci-workflow.spec.ts.snap @@ -44,9 +44,10 @@ jobs: env: AZURE_DEVOPS_EXT_PAT: $(System.AccessToken) - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested - # - script: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci" + # 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 + - script: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci" - script: npm ci - script: git branch --track main origin/main @@ -73,9 +74,10 @@ pipelines: script: - export NX_BRANCH=$BITBUCKET_PR_ID - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested - # - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci" + # 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 + - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci" - npm ci @@ -89,9 +91,10 @@ pipelines: name: 'Build and test affected apps on "main" branch changes' script: - export NX_BRANCH=$BITBUCKET_BRANCH - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested - # - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci" + # 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 + - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci" - npm ci @@ -114,9 +117,10 @@ jobs: steps: - checkout - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested - # - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci" + # 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 + - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci" - run: npm ci - nx/set-shas: @@ -157,9 +161,10 @@ jobs: with: fetch-depth: 0 - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested - # - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci" + # 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 + - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci" # Cache node_modules - uses: actions/setup-node@v3 @@ -198,9 +203,10 @@ jobs: with: fetch-depth: 0 - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested - # - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci" + # 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 + - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci" # Cache node_modules - uses: actions/setup-node@v3 @@ -265,9 +271,10 @@ jobs: - script: npm install --prefix=$HOME/.local -g Bun displayName: Install Bun - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - script: bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - script: bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - script: bun install --no-cache - script: git branch --track main origin/main @@ -295,9 +302,10 @@ pipelines: - npm install --prefix=$HOME/.local -g bun - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - bun install --no-cache @@ -310,9 +318,10 @@ pipelines: name: 'Build and test affected apps on "main" branch changes' script: - export NX_BRANCH=$BITBUCKET_BRANCH - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - npm install --prefix=$HOME/.local -g bun @@ -341,9 +350,10 @@ jobs: name: Install Bun command: npm install --prefix=$HOME/.local -g bun - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - run: bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - run: bun install --no-cache - nx/set-shas: @@ -387,9 +397,10 @@ jobs: with: bun-version: latest - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - run: bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - run: bun install --no-cache - uses: nrwl/nx-set-shas@v4 @@ -425,9 +436,10 @@ jobs: with: bun-version: latest - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - run: bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - run: bun install --no-cache - uses: nrwl/nx-set-shas@v4 @@ -452,9 +464,10 @@ CI: script: - npm install --prefix=$HOME/.local -g bun - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - bun install --no-cache - NX_HEAD=$CI_COMMIT_SHA @@ -510,9 +523,10 @@ jobs: env: AZURE_DEVOPS_EXT_PAT: $(System.AccessToken) - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - script: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - script: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - script: npm ci - script: git branch --track main origin/main @@ -538,9 +552,10 @@ pipelines: script: - export NX_BRANCH=$BITBUCKET_PR_ID - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - npm ci @@ -553,9 +568,10 @@ pipelines: name: 'Build and test affected apps on "main" branch changes' script: - export NX_BRANCH=$BITBUCKET_BRANCH - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - npm ci @@ -578,9 +594,10 @@ jobs: steps: - checkout - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - run: npm ci - nx/set-shas: @@ -620,9 +637,10 @@ jobs: with: fetch-depth: 0 - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" # Cache node_modules - uses: actions/setup-node@v3 @@ -660,9 +678,10 @@ jobs: with: fetch-depth: 0 - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" # Cache node_modules - uses: actions/setup-node@v3 @@ -691,9 +710,10 @@ CI: - main - merge_requests script: - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - npm ci - NX_HEAD=$CI_COMMIT_SHA @@ -752,9 +772,10 @@ jobs: - script: npm install --prefix=$HOME/.local -g pnpm@8 displayName: Install PNPM - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - script: pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - script: pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - script: pnpm install --frozen-lockfile - script: git branch --track main origin/main @@ -782,9 +803,10 @@ pipelines: - npm install --prefix=$HOME/.local -g pnpm@8 - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - pnpm install --frozen-lockfile @@ -797,9 +819,10 @@ pipelines: name: 'Build and test affected apps on "main" branch changes' script: - export NX_BRANCH=$BITBUCKET_BRANCH - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - npm install --prefix=$HOME/.local -g pnpm@8 @@ -828,9 +851,10 @@ jobs: name: Install PNPM command: npm install --prefix=$HOME/.local -g pnpm@8 - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - run: pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - run: pnpm install --frozen-lockfile - nx/set-shas: @@ -874,9 +898,10 @@ jobs: with: version: 8 - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - run: pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" # Cache node_modules - uses: actions/setup-node@v3 @@ -918,9 +943,10 @@ jobs: with: version: 8 - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - run: pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" # Cache node_modules - uses: actions/setup-node@v3 @@ -951,9 +977,10 @@ CI: script: - npm install --prefix=$HOME/.local -g pnpm@8 - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - pnpm install --frozen-lockfile - NX_HEAD=$CI_COMMIT_SHA @@ -1009,9 +1036,10 @@ jobs: env: AZURE_DEVOPS_EXT_PAT: $(System.AccessToken) - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - script: yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - script: yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - script: yarn install --frozen-lockfile - script: git branch --track main origin/main @@ -1037,9 +1065,10 @@ pipelines: script: - export NX_BRANCH=$BITBUCKET_PR_ID - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - yarn install --frozen-lockfile @@ -1052,9 +1081,10 @@ pipelines: name: 'Build and test affected apps on "main" branch changes' script: - export NX_BRANCH=$BITBUCKET_BRANCH - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - yarn install --frozen-lockfile @@ -1077,9 +1107,10 @@ jobs: steps: - checkout - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - run: yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - run: yarn install --frozen-lockfile - nx/set-shas: @@ -1119,9 +1150,10 @@ jobs: with: fetch-depth: 0 - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - run: yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" # Cache node_modules - uses: actions/setup-node@v3 @@ -1159,9 +1191,10 @@ jobs: with: fetch-depth: 0 - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - run: yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - run: yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" # Cache node_modules - uses: actions/setup-node@v3 @@ -1190,9 +1223,10 @@ CI: - main - merge_requests script: - # Connect your workspace on nx.app and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "build" targets have been requested - # - yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + # 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 + - yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - yarn install --frozen-lockfile - NX_HEAD=$CI_COMMIT_SHA diff --git a/packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts b/packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts index 7673cdb33418a..8d8b25acc243a 100644 --- a/packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts +++ b/packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts @@ -4,6 +4,7 @@ import { readNxJson, Tree, updateJson, + updateNxJson, writeJson, } from '@nx/devkit'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; @@ -34,6 +35,9 @@ describe('CI Workflow generator', () => { beforeEach(() => { tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); + const nxJson = readNxJson(tree); + nxJson.nxCloudAccessToken = 'test'; + updateNxJson(tree, nxJson); }); afterEach(() => { diff --git a/packages/workspace/src/generators/ci-workflow/ci-workflow.ts b/packages/workspace/src/generators/ci-workflow/ci-workflow.ts index b8a1fb59abb63..bc2c8c8d955c1 100644 --- a/packages/workspace/src/generators/ci-workflow/ci-workflow.ts +++ b/packages/workspace/src/generators/ci-workflow/ci-workflow.ts @@ -11,7 +11,7 @@ import { } from '@nx/devkit'; import { deduceDefaultBase } from '../../utilities/default-base'; import { join } from 'path'; -import { getNxCloudUrl } from 'nx/src/utils/nx-cloud-utils'; +import { getNxCloudUrl, isNxCloudUsed } from 'nx/src/utils/nx-cloud-utils'; export interface Schema { name: string; @@ -42,6 +42,7 @@ interface Substitutes { nxCloudHost: string; hasE2E: boolean; tmpl: ''; + connectedToCloud: boolean; } function normalizeOptions(options: Schema, tree: Tree): Substitutes { @@ -70,6 +71,8 @@ function normalizeOptions(options: Schema, tree: Tree): Substitutes { const hasE2E = allDependencies['@nx/cypress'] || allDependencies['@nx/playwright']; + const connectedToCloud = isNxCloudUsed(readJson(tree, 'nx.json')); + return { workflowName, workflowFileName, @@ -81,6 +84,7 @@ function normalizeOptions(options: Schema, tree: Tree): Substitutes { hasE2E, nxCloudHost, tmpl: '', + connectedToCloud, }; } diff --git a/packages/workspace/src/generators/ci-workflow/files/azure/azure-pipelines.yml__tmpl__ b/packages/workspace/src/generators/ci-workflow/files/azure/azure-pipelines.yml__tmpl__ index 1e6a205fe8f79..009f88706c156 100644 --- a/packages/workspace/src/generators/ci-workflow/files/azure/azure-pipelines.yml__tmpl__ +++ b/packages/workspace/src/generators/ci-workflow/files/azure/azure-pipelines.yml__tmpl__ @@ -51,10 +51,14 @@ jobs: displayName: Install Bun <% } %> - # Connect your workspace on <%= nxCloudHost %> and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" targets have been requested + + # 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 + <% if (connectedToCloud) { %>- script: <%= packageManagerPreInstallPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" + <% } else { %># Connect your workspace by running "nx connect" and uncomment this # - script: <%= packageManagerPreInstallPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" - + <% } %> - script: <%= packageManagerInstall %> - script: git branch --track <%= mainBranch %> origin/<%= mainBranch %> condition: eq(variables['Build.Reason'], 'PullRequest') diff --git a/packages/workspace/src/generators/ci-workflow/files/bitbucket-pipelines/bitbucket-pipelines.yml__tmpl__ b/packages/workspace/src/generators/ci-workflow/files/bitbucket-pipelines/bitbucket-pipelines.yml__tmpl__ index bc6df793a4e57..7978cdaafc23c 100644 --- a/packages/workspace/src/generators/ci-workflow/files/bitbucket-pipelines/bitbucket-pipelines.yml__tmpl__ +++ b/packages/workspace/src/generators/ci-workflow/files/bitbucket-pipelines/bitbucket-pipelines.yml__tmpl__ @@ -19,10 +19,13 @@ pipelines: - npm install --prefix=$HOME/.local -g bun <% } %> - # Connect your workspace on <%= nxCloudHost %> and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" targets have been requested + # 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 + <% if (connectedToCloud) { %>- <%= packageManagerPreInstallPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" + <% } else { %># Connect your workspace by running "nx connect" and uncomment this # - <%= packageManagerPreInstallPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" - + <% } %> - <%= packageManagerInstall %> - <%= packageManagerPrefix %> nx-cloud record -- nx format:check @@ -35,10 +38,13 @@ pipelines: name: 'Build and test affected apps on "<%= mainBranch %>" branch changes' script: - export NX_BRANCH=$BITBUCKET_BRANCH - # Connect your workspace on <%= nxCloudHost %> and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" targets have been requested + # 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 + <% if (connectedToCloud) { %>- <%= packageManagerPreInstallPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" + <% } else { %># Connect your workspace by running "nx connect" and uncomment this # - <%= packageManagerPreInstallPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" - + <% } %> <% if(packageManager == 'pnpm'){ %> - npm install --prefix=$HOME/.local -g pnpm@8 <% } %> diff --git a/packages/workspace/src/generators/ci-workflow/files/circleci/.circleci/config.yml__tmpl__ b/packages/workspace/src/generators/ci-workflow/files/circleci/.circleci/config.yml__tmpl__ index 8d99f60dfb126..da824022adf6b 100644 --- a/packages/workspace/src/generators/ci-workflow/files/circleci/.circleci/config.yml__tmpl__ +++ b/packages/workspace/src/generators/ci-workflow/files/circleci/.circleci/config.yml__tmpl__ @@ -21,10 +21,13 @@ jobs: <% } %> - # Connect your workspace on <%= nxCloudHost %> and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" targets have been requested + # 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 + <% if (connectedToCloud) { %>- run: <%= packageManagerPreInstallPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" + <% } else { %># Connect your workspace by running "nx connect" and uncomment this # - run: <%= packageManagerPreInstallPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" - + <% } %> - run: <%= packageManagerInstall %> - nx/set-shas: main-branch-name: '<%= mainBranch %>' diff --git a/packages/workspace/src/generators/ci-workflow/files/github/.github/workflows/__workflowFileName__.yml__tmpl__ b/packages/workspace/src/generators/ci-workflow/files/github/.github/workflows/__workflowFileName__.yml__tmpl__ index be73dff3c3901..09b91c147ed1f 100644 --- a/packages/workspace/src/generators/ci-workflow/files/github/.github/workflows/__workflowFileName__.yml__tmpl__ +++ b/packages/workspace/src/generators/ci-workflow/files/github/.github/workflows/__workflowFileName__.yml__tmpl__ @@ -28,10 +28,13 @@ jobs: bun-version: latest <% } %> - # Connect your workspace on <%= nxCloudHost %> and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" targets have been requested + # 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 + <% if (connectedToCloud) { %>- run: <%= packageManagerPreInstallPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" + <% } else { %># Connect your workspace by running "nx connect" and uncomment this # - run: <%= packageManagerPreInstallPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" - + <% } %> <% if(packageManager != 'bun'){ %> # Cache node_modules - uses: actions/setup-node@v3 diff --git a/packages/workspace/src/generators/ci-workflow/files/gitlab/.gitlab-ci.yml__tmpl__ b/packages/workspace/src/generators/ci-workflow/files/gitlab/.gitlab-ci.yml__tmpl__ index ebd364670cd0d..e029f6a836134 100644 --- a/packages/workspace/src/generators/ci-workflow/files/gitlab/.gitlab-ci.yml__tmpl__ +++ b/packages/workspace/src/generators/ci-workflow/files/gitlab/.gitlab-ci.yml__tmpl__ @@ -15,10 +15,13 @@ variables: <% if(packageManager == 'bun'){ %> - npm install --prefix=$HOME/.local -g bun <% } %> - # Connect your workspace on <%= nxCloudHost %> and uncomment this to enable task distribution. - # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" targets have been requested + # 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 + <% if (connectedToCloud) { %>- <%= packageManagerPreInstallPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" + <% } else { %># Connect your workspace by running "nx connect" and uncomment this # - <%= packageManagerPreInstallPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="<% if(hasE2E){ %>e2e-ci<% } else { %>build<% } %>" - + <% } %> - <%= packageManagerInstall %> - NX_HEAD=$CI_COMMIT_SHA - NX_BASE=${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}