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 320446ca304a0..c81cdb5c10e37 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 @@ -24,7 +24,8 @@ jobs: pool: vmImage: 'ubuntu-latest' steps: - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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" - script: npm ci @@ -49,7 +50,8 @@ pipelines: script: - export NX_BRANCH=$BITBUCKET_PR_ID - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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" - npm ci @@ -72,7 +74,8 @@ jobs: steps: - checkout - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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" - run: npm ci @@ -112,7 +115,8 @@ jobs: with: fetch-depth: 0 - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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" # Cache node_modules @@ -149,7 +153,8 @@ jobs: with: fetch-depth: 0 - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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" # Cache node_modules @@ -177,7 +182,8 @@ CI: - main - merge_requests script: - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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" - npm ci @@ -189,7 +195,7 @@ CI: " `; -exports[`CI Workflow generator with npm should throw error is nx cloud is not set 1`] = `"This workspace is not connected to Nx Cloud."`; +exports[`CI Workflow generator with npm should throw error is nx cloud is not set 1`] = `"nx-cloud runner not found in nx.json"`; exports[`CI Workflow generator with pnpm should generate azure CI config 1`] = ` "name: CI @@ -215,7 +221,8 @@ jobs: pool: vmImage: 'ubuntu-latest' steps: - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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 exec nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - script: npm install --prefix=$HOME/.local -g pnpm@8 @@ -242,7 +249,8 @@ pipelines: script: - export NX_BRANCH=$BITBUCKET_PR_ID - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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 exec nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - npm install --prefix=$HOME/.local -g pnpm@8 @@ -267,7 +275,8 @@ jobs: steps: - checkout - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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 exec nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - run: @@ -310,7 +319,8 @@ jobs: with: fetch-depth: 0 - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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 exec nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - uses: pnpm/action-setup@v2 @@ -350,7 +360,8 @@ jobs: with: fetch-depth: 0 - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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 exec nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - uses: pnpm/action-setup@v2 @@ -381,7 +392,8 @@ CI: - main - merge_requests script: - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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 exec nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" - npm install --prefix=$HOME/.local -g pnpm@8 @@ -395,7 +407,7 @@ CI: " `; -exports[`CI Workflow generator with pnpm should throw error is nx cloud is not set 1`] = `"This workspace is not connected to Nx Cloud."`; +exports[`CI Workflow generator with pnpm should throw error is nx cloud is not set 1`] = `"nx-cloud runner not found in nx.json"`; exports[`CI Workflow generator with yarn should generate azure CI config 1`] = ` "name: CI @@ -421,7 +433,8 @@ jobs: pool: vmImage: 'ubuntu-latest' steps: - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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" - script: yarn install --frozen-lockfile @@ -446,7 +459,8 @@ pipelines: script: - export NX_BRANCH=$BITBUCKET_PR_ID - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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" - yarn install --frozen-lockfile @@ -469,7 +483,8 @@ jobs: steps: - checkout - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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" - run: yarn install --frozen-lockfile @@ -509,7 +524,8 @@ jobs: with: fetch-depth: 0 - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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" # Cache node_modules @@ -546,7 +562,8 @@ jobs: with: fetch-depth: 0 - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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" # Cache node_modules @@ -574,7 +591,8 @@ CI: - main - merge_requests script: - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # Connect your workspace on my.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" - yarn install --frozen-lockfile @@ -586,4 +604,4 @@ CI: " `; -exports[`CI Workflow generator with yarn should throw error is nx cloud is not set 1`] = `"This workspace is not connected to Nx Cloud."`; +exports[`CI Workflow generator with yarn should throw error is nx cloud is not set 1`] = `"nx-cloud runner not found in nx.json"`; 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 566050221062f..df3fa2d11095b 100644 --- a/packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts +++ b/packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts @@ -138,6 +138,7 @@ function setNxCloud(tree: Tree) { return { ...json, nxCloudAccessToken: 'xxxx-xxx-xxxx', + nxCloudUrl: 'https://my.nx.app', }; }); } diff --git a/packages/workspace/src/generators/ci-workflow/ci-workflow.ts b/packages/workspace/src/generators/ci-workflow/ci-workflow.ts index b036d5e42752e..17bdb2e55bbc7 100644 --- a/packages/workspace/src/generators/ci-workflow/ci-workflow.ts +++ b/packages/workspace/src/generators/ci-workflow/ci-workflow.ts @@ -11,17 +11,18 @@ import { } from '@nx/devkit'; import { deduceDefaultBase } from '../../utilities/default-base'; import { join } from 'path'; +import { getNxCloudUrl } from 'nx/src/utils/nx-cloud-utils'; export interface Schema { name: string; ci: 'github' | 'azure' | 'circleci' | 'bitbucket-pipelines' | 'gitlab'; } -export async function ciWorkflowGenerator(host: Tree, schema: Schema) { +export async function ciWorkflowGenerator(tree: Tree, schema: Schema) { const ci = schema.ci; - const options = normalizeOptions(schema); + const options = normalizeOptions(schema, tree); - const nxJson: NxJsonConfiguration = readJson(host, 'nx.json'); + const nxJson: NxJsonConfiguration = readJson(tree, 'nx.json'); const nxCloudUsed = nxJson.nxCloudAccessToken ?? Object.values(nxJson.tasksRunnerOptions ?? {}).find( @@ -32,11 +33,11 @@ export async function ciWorkflowGenerator(host: Tree, schema: Schema) { } if (ci === 'bitbucket-pipelines' && defaultBranchNeedsOriginPrefix(nxJson)) { - writeJson(host, 'nx.json', appendOriginPrefix(nxJson)); + writeJson(tree, 'nx.json', appendOriginPrefix(nxJson)); } - generateFiles(host, join(__dirname, 'files', ci), '', options); - await formatFiles(host); + generateFiles(tree, join(__dirname, 'files', ci), '', options); + await formatFiles(tree); } interface Substitutes { @@ -46,16 +47,21 @@ interface Substitutes { packageManager: string; packageManagerInstall: string; packageManagerPrefix: string; + nxCloudHost: string; tmpl: ''; } -function normalizeOptions(options: Schema): Substitutes { +function normalizeOptions(options: Schema, tree: Tree): Substitutes { const { name: workflowName, fileName: workflowFileName } = names( options.name ); const packageManager = detectPackageManager(); const { exec: packageManagerPrefix, ciInstall: packageManagerInstall } = getPackageManagerCommand(packageManager); + + const nxCloudUrl = getNxCloudUrl(readJson(tree, 'nx.json')); + const nxCloudHost = new URL(nxCloudUrl).host; + return { workflowName, workflowFileName, @@ -63,6 +69,7 @@ function normalizeOptions(options: Schema): Substitutes { packageManagerInstall, packageManagerPrefix, mainBranch: deduceDefaultBase(), + nxCloudHost, tmpl: '', }; } 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 cb273b71acfec..41a319d94f54b 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__ @@ -21,7 +21,8 @@ jobs: pool: vmImage: 'ubuntu-latest' steps: - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # 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 # - script: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" <% if(packageManager == 'pnpm'){ %> 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 a8f245c626b96..7a414d793aa9f 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__ @@ -12,7 +12,8 @@ pipelines: script: - export NX_BRANCH=$BITBUCKET_PR_ID - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # 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 # - <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" <% if(packageManager == 'pnpm'){ %> 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 84177b1334b33..79ed84a82000d 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__ @@ -10,7 +10,8 @@ jobs: steps: - checkout - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # 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 # - run: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" <% if(packageManager == 'pnpm'){ %> 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 ff533ed094dfb..7cc14596a512c 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__ @@ -18,7 +18,8 @@ jobs: with: fetch-depth: 0 - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # 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 # - run: <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" <% if(packageManager == 'pnpm'){ %> 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 edb3787f27146..17e61a6ba7fcc 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__ @@ -9,7 +9,8 @@ variables: - main - merge_requests script: - # Uncomment to enable task distribution. --stop-agents-after is optional, but allows idle agents to shut down once the "build" targets have been requested + # 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 # - <%= packageManagerPrefix %> nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" <% if(packageManager == 'pnpm'){ %>