From aa7c5dc3eaa2659cc2fd46a91056211d6769de48 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Fri, 12 Jul 2024 10:25:55 -0700 Subject: [PATCH] chore(core): add unit tests for not connected to nx cloud (#26885) ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes # --- .../__snapshots__/generator.spec.ts.snap | 88 +- .../circleci/.circleci/config.yml.template | 2 +- .../__workflowFileName__.yml.template | 2 +- .../generators/ci-workflow/generator.spec.ts | 46 +- .../src/generators/ci-workflow/generator.ts | 26 +- .../__snapshots__/ci-workflow.spec.ts.snap | 1600 ++++++++++++++--- .../ci-workflow/ci-workflow.spec.ts | 165 +- .../files/azure/azure-pipelines.yml__tmpl__ | 1 + .../bitbucket-pipelines.yml__tmpl__ | 2 + .../circleci/.circleci/config.yml__tmpl__ | 1 + .../__workflowFileName__.yml__tmpl__ | 1 + .../files/gitlab/.gitlab-ci.yml__tmpl__ | 1 + 12 files changed, 1597 insertions(+), 338 deletions(-) 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 b8fe02b4e96fe..59a2bdca44b1d 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 @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`ci-workflow generator circleci pipeline should match snapshot 1`] = ` +exports[`ci-workflow generator connected to nxCloud circleci pipeline should match snapshot 1`] = ` "version: 2.1 orbs: @@ -25,6 +25,7 @@ jobs: - nx/set-shas: main-branch-name: 'main' + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: ./nx affected --base=$NX_BASE --head=$NX_HEAD -t test build workflows: @@ -36,7 +37,7 @@ workflows: " `; -exports[`ci-workflow generator github pipeline should match snapshot 1`] = ` +exports[`ci-workflow generator connected to nxCloud github pipeline should match snapshot 1`] = ` "name: CI on: @@ -74,6 +75,89 @@ jobs: - 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: ./nx affected -t test build +" +`; + +exports[`ci-workflow generator not connected to nxCloud circleci pipeline should match snapshot 1`] = ` +"version: 2.1 + +orbs: + nx: nrwl/nx@1.6.2 + +jobs: + main: + environment: + # Configure the JVM and Gradle to avoid OOM errors + _JAVA_OPTIONS: '-Xmx3g' + GRADLE_OPTS: '-Dorg.gradle.daemon=false -Dorg.gradle.workers.max=2' + docker: + - image: cimg/openjdk:17.0-node + steps: + - checkout + + # 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="5 linux-medium-jvm" --stop-agents-after="build" + + - nx/set-shas: + main-branch-name: 'main' + + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - run: ./nx affected --base=$NX_BASE --head=$NX_HEAD -t test build + +workflows: + version: 2 + + ci: + jobs: + - main +" +`; + +exports[`ci-workflow generator not connected to nxCloud github pipeline should match snapshot 1`] = ` +"name: CI + +on: + push: + branches: + - main + pull_request: + +permissions: + actions: read + contents: read + +jobs: + main: + runs-on: ubuntu-latest + steps: + - 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="5 linux-medium-jvm" --stop-agents-after="build" + + - name: Set up JDK 17 for x64 + uses: actions/setup-java@v4 + with: + 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: ./nx affected -t test build " `; 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 97be5eed7427a..d16d32fe8c9cd 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 @@ -25,7 +25,7 @@ jobs: main-branch-name: '<%= mainBranch %>' <% for (const command of commands) { %> - - run: <%= command %><% } %> + <% if (command.command) { %>- run: <%= command.command %><% } else if (command.comment) { %><%= command.comment %><% } else {%>- run: <%= command %><% } %><% } %> workflows: version: 2 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 12f828c21eb60..83e5173865989 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 @@ -39,4 +39,4 @@ jobs: - uses: nrwl/nx-set-shas@v4 <% for (const command of commands) { %> - - run: <%= command %><% } %> + <% if (command.command) { %>- run: <%= command.command %><% } else if (command.comment) { %><%= command.comment %><% } else {%>- run: <%= command %><% } %><% } %> diff --git a/packages/gradle/src/generators/ci-workflow/generator.spec.ts b/packages/gradle/src/generators/ci-workflow/generator.spec.ts index 4568bfb1f101d..bf2c0ba09ee27 100644 --- a/packages/gradle/src/generators/ci-workflow/generator.spec.ts +++ b/packages/gradle/src/generators/ci-workflow/generator.spec.ts @@ -8,21 +8,45 @@ describe('ci-workflow generator', () => { beforeEach(() => { tree = createTreeWithEmptyWorkspace(); - const nxJson = readNxJson(tree); - nxJson.nxCloudAccessToken = 'test'; - updateNxJson(tree, nxJson); }); describe.each([ - ['github', '.github/workflows/ci.yml'], - ['circleci', '.circleci/config.yml'], - ] as const)(`%s pipeline`, (ciProvider, output) => { - it('should match snapshot', async () => { - await ciWorkflowGenerator(tree, { - name: 'CI', - ci: ciProvider, + ['connected to nxCloud', true], + ['not connected to nxCloud', false], + ] as const)(`%s`, (_, connectedToCloud) => { + let nxCloudAccessToken: string; + + beforeEach(() => { + if (connectedToCloud) { + const nxJson = readNxJson(tree); + nxJson.nxCloudAccessToken = 'test'; + updateNxJson(tree, nxJson); + } else { + nxCloudAccessToken = process.env.NX_CLOUD_ACCESS_TOKEN; + delete process.env.NX_CLOUD_ACCESS_TOKEN; + } + }); + + afterEach(() => { + if (connectedToCloud) { + const nxJson = readNxJson(tree); + delete nxJson.nxCloudAccessToken; + updateNxJson(tree, nxJson); + } else { + process.env.NX_CLOUD_ACCESS_TOKEN = nxCloudAccessToken; + } + }); + describe.each([ + ['github', '.github/workflows/ci.yml'], + ['circleci', '.circleci/config.yml'], + ] as const)(`%s pipeline`, (ciProvider, output) => { + it('should match snapshot', async () => { + await ciWorkflowGenerator(tree, { + name: 'CI', + ci: ciProvider, + }); + expect(tree.read(output, 'utf-8')).toMatchSnapshot(); }); - expect(tree.read(output, 'utf-8')).toMatchSnapshot(); }); }); }); diff --git a/packages/gradle/src/generators/ci-workflow/generator.ts b/packages/gradle/src/generators/ci-workflow/generator.ts index c330564cfe602..36febc6b81c53 100644 --- a/packages/gradle/src/generators/ci-workflow/generator.ts +++ b/packages/gradle/src/generators/ci-workflow/generator.ts @@ -11,22 +11,36 @@ import { join } from 'path'; import { getNxCloudUrl, isNxCloudUsed } from 'nx/src/utils/nx-cloud-utils'; import { deduceDefaultBase } from 'nx/src/utils/default-base'; -function getCiCommands(ci: Schema['ci'], mainBranch: string): string[] { +function getCiCommands(ci: Schema['ci']): Command[] { switch (ci) { case 'circleci': { - return [`./nx affected --base=$NX_BASE --head=$NX_HEAD -t test build`]; + return [ + { + comment: `# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected`, + }, + { + command: `./nx affected --base=$NX_BASE --head=$NX_HEAD -t test build`, + }, + ]; } default: { - return [`./nx affected -t test build`]; + return [ + { + comment: `# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected`, + }, + { command: `./nx affected -t test build` }, + ]; } } } +export type Command = { command: string } | { comment: string } | string; + export interface Schema { name: string; ci: 'github' | 'circleci'; packageManager?: null; - commands?: string[]; + commands?: Command[]; } export async function ciWorkflowGenerator(tree: Tree, schema: Schema) { @@ -43,7 +57,7 @@ interface Substitutes { workflowFileName: string; packageManager: string; packageManagerPrefix: string; - commands: string[]; + commands: Command[]; nxCloudHost: string; connectedToCloud: boolean; } @@ -64,7 +78,7 @@ function getTemplateData(tree: Tree, options: Schema): Substitutes { const mainBranch = deduceDefaultBase(); - const commands = options.commands ?? getCiCommands(options.ci, mainBranch); + const commands = options.commands ?? getCiCommands(options.ci); const connectedToCloud = isNxCloudUsed(readNxJson(tree)); 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 57942a6787bbf..a2c0a2c772e02 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 @@ -1,230 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`CI Workflow generator optional e2e should add e2e to azure CI config 1`] = ` -"name: CI - -trigger: - - main -pr: - - main - -variables: - CI: 'true' - \${{ if eq(variables['Build.Reason'], 'PullRequest') }}: - NX_BRANCH: $(System.PullRequest.PullRequestNumber) - TARGET_BRANCH: $[replace(variables['System.PullRequest.TargetBranch'],'refs/heads/','origin/')] - BASE_SHA: $(git merge-base $(TARGET_BRANCH) HEAD) - \${{ if ne(variables['Build.Reason'], 'PullRequest') }}: - NX_BRANCH: $(Build.SourceBranchName) - BASE_SHA: $(git rev-parse HEAD~1) - HEAD_SHA: $(git rev-parse HEAD) - -jobs: - - job: main - pool: - vmImage: 'ubuntu-latest' - steps: - - checkout: self - fetchDepth: 0 - # Set Azure Devops CLI default settings - - bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject) - displayName: 'Set default Azure DevOps organization and project' - # Get last successfull commit from Azure Devops CLI - - bash: | - LAST_SHA=$(az pipelines build list --branch $(Build.SourceBranchName) --definition-ids $(System.DefinitionId) --result succeeded --top 1 --query "[0].triggerInfo.\\"ci.sourceSha\\"") - if [ -z "$LAST_SHA" ] - then - echo "Last successful commit not found. Using fallback 'HEAD~1': $BASE_SHA" - else - echo "Last successful commit SHA: $LAST_SHA" - echo "##vso[task.setvariable variable=BASE_SHA]$LAST_SHA" - fi - displayName: 'Get last successful commit SHA' - condition: ne(variables['Build.Reason'], 'PullRequest') - env: - AZURE_DEVOPS_EXT_PAT: $(System.AccessToken) - - # 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 - condition: eq(variables['Build.Reason'], 'PullRequest') - - # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud - # - script: npx nx-cloud record -- echo Hello World - - script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) lint test build - - script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) --parallel 1 e2e-ci -" -`; - -exports[`CI Workflow generator optional e2e should add e2e to bitbucket pipelines config 1`] = ` -"image: node:20 - -clone: - depth: full - -pipelines: - pull-requests: - '**': - - step: - name: 'Build and test affected apps on Pull Requests' - script: - - export NX_BRANCH=$BITBUCKET_PR_ID - - # 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 - - - npx nx-cloud record -- nx format:check - - npx nx affected --base=origin/main -t lint test build - - npx nx affected --base=origin/main --parallel 1 -t e2e-ci - - branches: - main: - - step: - name: 'Build and test affected apps on "main" branch changes' - script: - - export NX_BRANCH=$BITBUCKET_BRANCH - # 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 - - # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud - # - npx nx-cloud record -- echo Hello World - - npx nx affected -t lint test build e2e-ci --base=HEAD~1 -" -`; - -exports[`CI Workflow generator optional e2e should add e2e to circleci CI config 1`] = ` -"version: 2.1 - -orbs: - nx: nrwl/nx@1.6.2 - -jobs: - main: - docker: - - image: cimg/node:lts-browsers - steps: - - checkout - - # 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: - main-branch-name: 'main' - - # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud - # - run: npx nx-cloud record -- echo Hello World - - run: npx nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build - - run: npx nx affected --base=$NX_BASE --head=$NX_HEAD --parallel 1 -t e2e-ci - -workflows: - version: 2 - - ci: - jobs: - - main -" -`; - -exports[`CI Workflow generator optional e2e should add e2e to github CI config 1`] = ` -"name: CI - -on: - push: - branches: - - main - pull_request: - -permissions: - actions: read - contents: read - -jobs: - main: - runs-on: ubuntu-latest - steps: - - 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 - - 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 - with: - node-version: 20 - cache: 'npm' - - - run: npm ci - - uses: nrwl/nx-set-shas@v4 - - # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud - # - run: npx nx-cloud record -- echo Hello World - - run: npx nx affected -t lint test build - - run: npx nx affected --parallel 1 -t e2e-ci -" -`; - -exports[`CI Workflow generator optional e2e should add e2e to github CI config with custom name 1`] = ` -"name: My custom-workflow - -on: - push: - branches: - - main - pull_request: - -permissions: - actions: read - contents: read - -jobs: - main: - runs-on: ubuntu-latest - steps: - - 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 - - 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 - with: - node-version: 20 - cache: 'npm' - - - run: npm ci - - uses: nrwl/nx-set-shas@v4 - - # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud - # - run: npx nx-cloud record -- echo Hello World - - run: npx nx affected -t lint test build - - run: npx nx affected --parallel 1 -t e2e-ci -" -`; - -exports[`CI Workflow generator with bun should generate azure CI config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with bun should generate azure CI config 1`] = ` "name: CI trigger: @@ -282,11 +58,12 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - script: bun nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - script: bun nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) lint test build " `; -exports[`CI Workflow generator with bun should generate bitbucket pipelines config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with bun should generate bitbucket pipelines config 1`] = ` "image: node:20 clone: @@ -310,6 +87,7 @@ pipelines: - bun install --no-cache - bun nx-cloud record -- nx format:check + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - bun nx affected --base=origin/main -t lint test build branches: @@ -329,11 +107,12 @@ pipelines: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - bun nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - bun nx affected -t lint test build --base=HEAD~1 " `; -exports[`CI Workflow generator with bun should generate circleci CI config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with bun should generate circleci CI config 1`] = ` "version: 2.1 orbs: @@ -361,6 +140,7 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: bun nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: bun nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build workflows: @@ -372,7 +152,7 @@ workflows: " `; -exports[`CI Workflow generator with bun should generate github CI config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with bun should generate github CI config 1`] = ` "name: CI on: @@ -407,11 +187,12 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: bun nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: bun nx affected -t lint test build " `; -exports[`CI Workflow generator with bun should generate github CI config with custom name 1`] = ` +exports[`CI Workflow generator connected to nxCloud with bun should generate github CI config with custom name 1`] = ` "name: My custom-workflow on: @@ -446,11 +227,12 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: bun nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: bun nx affected -t lint test build " `; -exports[`CI Workflow generator with bun should generate gitlab config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with bun should generate gitlab config 1`] = ` "image: node:20 variables: CI: 'true' @@ -475,11 +257,12 @@ CI: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - bun nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - bun nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build " `; -exports[`CI Workflow generator with npm should generate azure CI config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with npm should generate azure CI config 1`] = ` "name: CI trigger: @@ -534,11 +317,12 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - script: npx nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) lint test build " `; -exports[`CI Workflow generator with npm should generate bitbucket pipelines config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with npm should generate bitbucket pipelines config 1`] = ` "image: node:20 clone: @@ -560,6 +344,7 @@ pipelines: - npm ci - npx nx-cloud record -- nx format:check + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - npx nx affected --base=origin/main -t lint test build branches: @@ -577,11 +362,12 @@ pipelines: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - npx nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - npx nx affected -t lint test build --base=HEAD~1 " `; -exports[`CI Workflow generator with npm should generate circleci CI config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with npm should generate circleci CI config 1`] = ` "version: 2.1 orbs: @@ -605,6 +391,7 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: npx nx-cloud record -- echo Hello World + # 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 --base=$NX_BASE --head=$NX_HEAD -t lint test build workflows: @@ -616,7 +403,7 @@ workflows: " `; -exports[`CI Workflow generator with npm should generate github CI config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with npm should generate github CI config 1`] = ` "name: CI on: @@ -653,11 +440,12 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: npx nx-cloud record -- echo Hello World + # 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 " `; -exports[`CI Workflow generator with npm should generate github CI config with custom name 1`] = ` +exports[`CI Workflow generator connected to nxCloud with npm should generate github CI config with custom name 1`] = ` "name: My custom-workflow on: @@ -694,11 +482,12 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: npx nx-cloud record -- echo Hello World + # 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 " `; -exports[`CI Workflow generator with npm should generate gitlab config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with npm should generate gitlab config 1`] = ` "image: node:20 variables: CI: 'true' @@ -721,11 +510,12 @@ CI: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - npx nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - npx nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build " `; -exports[`CI Workflow generator with pnpm should generate azure CI config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with pnpm should generate azure CI config 1`] = ` "name: CI trigger: @@ -783,11 +573,12 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - script: pnpm exec nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - script: pnpm exec nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) lint test build " `; -exports[`CI Workflow generator with pnpm should generate bitbucket pipelines config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with pnpm should generate bitbucket pipelines config 1`] = ` "image: node:20 clone: @@ -811,6 +602,7 @@ pipelines: - pnpm install --frozen-lockfile - pnpm exec nx-cloud record -- nx format:check + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - pnpm exec nx affected --base=origin/main -t lint test build branches: @@ -830,11 +622,12 @@ pipelines: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - pnpm exec nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - pnpm exec nx affected -t lint test build --base=HEAD~1 " `; -exports[`CI Workflow generator with pnpm should generate circleci CI config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with pnpm should generate circleci CI config 1`] = ` "version: 2.1 orbs: @@ -862,6 +655,7 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: pnpm exec nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: pnpm exec nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build workflows: @@ -873,7 +667,7 @@ workflows: " `; -exports[`CI Workflow generator with pnpm should generate github CI config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with pnpm should generate github CI config 1`] = ` "name: CI on: @@ -914,11 +708,12 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: pnpm exec nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: pnpm exec nx affected -t lint test build " `; -exports[`CI Workflow generator with pnpm should generate github CI config with custom name 1`] = ` +exports[`CI Workflow generator connected to nxCloud with pnpm should generate github CI config with custom name 1`] = ` "name: My custom-workflow on: @@ -959,11 +754,12 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: pnpm exec nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: pnpm exec nx affected -t lint test build " `; -exports[`CI Workflow generator with pnpm should generate gitlab config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with pnpm should generate gitlab config 1`] = ` "image: node:20 variables: CI: 'true' @@ -988,11 +784,12 @@ CI: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - pnpm exec nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - pnpm exec nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build " `; -exports[`CI Workflow generator with yarn should generate azure CI config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with yarn should generate azure CI config 1`] = ` "name: CI trigger: @@ -1047,11 +844,12 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - script: yarn nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - script: yarn nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) lint test build " `; -exports[`CI Workflow generator with yarn should generate bitbucket pipelines config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with yarn should generate bitbucket pipelines config 1`] = ` "image: node:20 clone: @@ -1073,6 +871,7 @@ pipelines: - yarn install --frozen-lockfile - yarn nx-cloud record -- nx format:check + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - yarn nx affected --base=origin/main -t lint test build branches: @@ -1090,11 +889,12 @@ pipelines: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - yarn nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - yarn nx affected -t lint test build --base=HEAD~1 " `; -exports[`CI Workflow generator with yarn should generate circleci CI config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with yarn should generate circleci CI config 1`] = ` "version: 2.1 orbs: @@ -1118,6 +918,7 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: yarn nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: yarn nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build workflows: @@ -1129,7 +930,7 @@ workflows: " `; -exports[`CI Workflow generator with yarn should generate github CI config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with yarn should generate github CI config 1`] = ` "name: CI on: @@ -1166,11 +967,12 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: yarn nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: yarn nx affected -t lint test build " `; -exports[`CI Workflow generator with yarn should generate github CI config with custom name 1`] = ` +exports[`CI Workflow generator connected to nxCloud with yarn should generate github CI config with custom name 1`] = ` "name: My custom-workflow on: @@ -1207,11 +1009,12 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: yarn nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: yarn nx affected -t lint test build " `; -exports[`CI Workflow generator with yarn should generate gitlab config 1`] = ` +exports[`CI Workflow generator connected to nxCloud with yarn should generate gitlab config 1`] = ` "image: node:20 variables: CI: 'true' @@ -1234,6 +1037,1307 @@ CI: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - yarn nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - yarn nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build " `; + +exports[`CI Workflow generator not connected to nxCloud with bun should generate azure CI config 1`] = ` +"name: CI + +trigger: + - main +pr: + - main + +variables: + CI: 'true' + \${{ if eq(variables['Build.Reason'], 'PullRequest') }}: + NX_BRANCH: $(System.PullRequest.PullRequestNumber) + TARGET_BRANCH: $[replace(variables['System.PullRequest.TargetBranch'],'refs/heads/','origin/')] + BASE_SHA: $(git merge-base $(TARGET_BRANCH) HEAD) + \${{ if ne(variables['Build.Reason'], 'PullRequest') }}: + NX_BRANCH: $(Build.SourceBranchName) + BASE_SHA: $(git rev-parse HEAD~1) + HEAD_SHA: $(git rev-parse HEAD) + +jobs: + - job: main + pool: + vmImage: 'ubuntu-latest' + steps: + - checkout: self + fetchDepth: 0 + # Set Azure Devops CLI default settings + - bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject) + displayName: 'Set default Azure DevOps organization and project' + # Get last successfull commit from Azure Devops CLI + - bash: | + LAST_SHA=$(az pipelines build list --branch $(Build.SourceBranchName) --definition-ids $(System.DefinitionId) --result succeeded --top 1 --query "[0].triggerInfo.\\"ci.sourceSha\\"") + if [ -z "$LAST_SHA" ] + then + echo "Last successful commit not found. Using fallback 'HEAD~1': $BASE_SHA" + else + echo "Last successful commit SHA: $LAST_SHA" + echo "##vso[task.setvariable variable=BASE_SHA]$LAST_SHA" + fi + displayName: 'Get last successful commit SHA' + condition: ne(variables['Build.Reason'], 'PullRequest') + env: + AZURE_DEVOPS_EXT_PAT: $(System.AccessToken) + + - script: npm install --prefix=$HOME/.local -g Bun + displayName: Install Bun + + # 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 + # - 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 + condition: eq(variables['Build.Reason'], 'PullRequest') + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - script: bun nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - script: bun nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) lint test build +" +`; + +exports[`CI Workflow generator not connected to nxCloud with bun should generate bitbucket pipelines config 1`] = ` +"image: node:20 + +clone: + depth: full + +pipelines: + pull-requests: + '**': + - step: + name: 'Build and test affected apps on Pull Requests' + script: + - export NX_BRANCH=$BITBUCKET_PR_ID + + - npm install --prefix=$HOME/.local -g bun + + # 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 + # - bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + + - bun install --no-cache + + - bun nx-cloud record -- nx format:check + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - bun nx affected --base=origin/main -t lint test build + + branches: + main: + - step: + name: 'Build and test affected apps on "main" branch changes' + script: + - export NX_BRANCH=$BITBUCKET_BRANCH + # 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 + # - bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + + - npm install --prefix=$HOME/.local -g bun + + - bun install --no-cache + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - bun nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - bun nx affected -t lint test build --base=HEAD~1 +" +`; + +exports[`CI Workflow generator not connected to nxCloud with bun should generate circleci CI config 1`] = ` +"version: 2.1 + +orbs: + nx: nrwl/nx@1.6.2 + +jobs: + main: + docker: + - image: cimg/node:lts-browsers + steps: + - checkout + + - run: + name: Install Bun + command: npm install --prefix=$HOME/.local -g bun + + # 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: bunx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + + - run: bun install --no-cache + - nx/set-shas: + main-branch-name: 'main' + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: bun nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - run: bun nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build + +workflows: + version: 2 + + ci: + jobs: + - main +" +`; + +exports[`CI Workflow generator not connected to nxCloud with bun should generate github CI config 1`] = ` +"name: CI + +on: + push: + branches: + - main + pull_request: + +permissions: + actions: read + contents: read + +jobs: + main: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: oven-sh/setup-bun@v1 + with: + bun-version: latest + + # 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: 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 + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: bun nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - run: bun nx affected -t lint test build +" +`; + +exports[`CI Workflow generator not connected to nxCloud with bun should generate github CI config with custom name 1`] = ` +"name: My custom-workflow + +on: + push: + branches: + - main + pull_request: + +permissions: + actions: read + contents: read + +jobs: + main: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: oven-sh/setup-bun@v1 + with: + bun-version: latest + + # 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: 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 + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: bun nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - run: bun nx affected -t lint test build +" +`; + +exports[`CI Workflow generator not connected to nxCloud with bun should generate gitlab config 1`] = ` +"image: node:20 +variables: + CI: 'true' + +# Main job +CI: + interruptible: true + only: + - main + - merge_requests + script: + - npm install --prefix=$HOME/.local -g bun + + # 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 + # - 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 + - NX_BASE=\${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA} + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - bun nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - bun nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build +" +`; + +exports[`CI Workflow generator not connected to nxCloud with npm should generate azure CI config 1`] = ` +"name: CI + +trigger: + - main +pr: + - main + +variables: + CI: 'true' + \${{ if eq(variables['Build.Reason'], 'PullRequest') }}: + NX_BRANCH: $(System.PullRequest.PullRequestNumber) + TARGET_BRANCH: $[replace(variables['System.PullRequest.TargetBranch'],'refs/heads/','origin/')] + BASE_SHA: $(git merge-base $(TARGET_BRANCH) HEAD) + \${{ if ne(variables['Build.Reason'], 'PullRequest') }}: + NX_BRANCH: $(Build.SourceBranchName) + BASE_SHA: $(git rev-parse HEAD~1) + HEAD_SHA: $(git rev-parse HEAD) + +jobs: + - job: main + pool: + vmImage: 'ubuntu-latest' + steps: + - checkout: self + fetchDepth: 0 + # Set Azure Devops CLI default settings + - bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject) + displayName: 'Set default Azure DevOps organization and project' + # Get last successfull commit from Azure Devops CLI + - bash: | + LAST_SHA=$(az pipelines build list --branch $(Build.SourceBranchName) --definition-ids $(System.DefinitionId) --result succeeded --top 1 --query "[0].triggerInfo.\\"ci.sourceSha\\"") + if [ -z "$LAST_SHA" ] + then + echo "Last successful commit not found. Using fallback 'HEAD~1': $BASE_SHA" + else + echo "Last successful commit SHA: $LAST_SHA" + echo "##vso[task.setvariable variable=BASE_SHA]$LAST_SHA" + fi + displayName: 'Get last successful commit SHA' + condition: ne(variables['Build.Reason'], 'PullRequest') + env: + AZURE_DEVOPS_EXT_PAT: $(System.AccessToken) + + # 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 + # - 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 + condition: eq(variables['Build.Reason'], 'PullRequest') + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - script: npx nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) lint test build +" +`; + +exports[`CI Workflow generator not connected to nxCloud with npm should generate bitbucket pipelines config 1`] = ` +"image: node:20 + +clone: + depth: full + +pipelines: + pull-requests: + '**': + - step: + name: 'Build and test affected apps on Pull Requests' + script: + - export NX_BRANCH=$BITBUCKET_PR_ID + + # 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 + # - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + + - npm ci + + - npx nx-cloud record -- nx format:check + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - npx nx affected --base=origin/main -t lint test build + + branches: + main: + - step: + name: 'Build and test affected apps on "main" branch changes' + script: + - export NX_BRANCH=$BITBUCKET_BRANCH + # 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 + # - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + + - npm ci + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - npx nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - npx nx affected -t lint test build --base=HEAD~1 +" +`; + +exports[`CI Workflow generator not connected to nxCloud with npm should generate circleci CI config 1`] = ` +"version: 2.1 + +orbs: + nx: nrwl/nx@1.6.2 + +jobs: + main: + docker: + - image: cimg/node:lts-browsers + steps: + - checkout + + # 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="5 linux-medium-js" --stop-agents-after="build" + + - run: npm ci + - nx/set-shas: + main-branch-name: 'main' + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: npx nx-cloud record -- echo Hello World + # 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 --base=$NX_BASE --head=$NX_HEAD -t lint test build + +workflows: + version: 2 + + ci: + jobs: + - main +" +`; + +exports[`CI Workflow generator not connected to nxCloud with npm should generate github CI config 1`] = ` +"name: CI + +on: + push: + branches: + - main + pull_request: + +permissions: + actions: read + contents: read + +jobs: + main: + runs-on: ubuntu-latest + steps: + - 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="5 linux-medium-js" --stop-agents-after="build" + + # Cache node_modules + - uses: actions/setup-node@v3 + with: + node-version: 20 + cache: 'npm' + + - run: npm ci + - uses: nrwl/nx-set-shas@v4 + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: npx nx-cloud record -- echo Hello World + # 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 +" +`; + +exports[`CI Workflow generator not connected to nxCloud with npm should generate github CI config with custom name 1`] = ` +"name: My custom-workflow + +on: + push: + branches: + - main + pull_request: + +permissions: + actions: read + contents: read + +jobs: + main: + runs-on: ubuntu-latest + steps: + - 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="5 linux-medium-js" --stop-agents-after="build" + + # Cache node_modules + - uses: actions/setup-node@v3 + with: + node-version: 20 + cache: 'npm' + + - run: npm ci + - uses: nrwl/nx-set-shas@v4 + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: npx nx-cloud record -- echo Hello World + # 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 +" +`; + +exports[`CI Workflow generator not connected to nxCloud with npm should generate gitlab config 1`] = ` +"image: node:20 +variables: + CI: 'true' + +# Main job +CI: + interruptible: true + only: + - main + - merge_requests + script: + # 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 + # - npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + + - npm ci + - NX_HEAD=$CI_COMMIT_SHA + - NX_BASE=\${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA} + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - npx nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - npx nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build +" +`; + +exports[`CI Workflow generator not connected to nxCloud with pnpm should generate azure CI config 1`] = ` +"name: CI + +trigger: + - main +pr: + - main + +variables: + CI: 'true' + \${{ if eq(variables['Build.Reason'], 'PullRequest') }}: + NX_BRANCH: $(System.PullRequest.PullRequestNumber) + TARGET_BRANCH: $[replace(variables['System.PullRequest.TargetBranch'],'refs/heads/','origin/')] + BASE_SHA: $(git merge-base $(TARGET_BRANCH) HEAD) + \${{ if ne(variables['Build.Reason'], 'PullRequest') }}: + NX_BRANCH: $(Build.SourceBranchName) + BASE_SHA: $(git rev-parse HEAD~1) + HEAD_SHA: $(git rev-parse HEAD) + +jobs: + - job: main + pool: + vmImage: 'ubuntu-latest' + steps: + - checkout: self + fetchDepth: 0 + # Set Azure Devops CLI default settings + - bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject) + displayName: 'Set default Azure DevOps organization and project' + # Get last successfull commit from Azure Devops CLI + - bash: | + LAST_SHA=$(az pipelines build list --branch $(Build.SourceBranchName) --definition-ids $(System.DefinitionId) --result succeeded --top 1 --query "[0].triggerInfo.\\"ci.sourceSha\\"") + if [ -z "$LAST_SHA" ] + then + echo "Last successful commit not found. Using fallback 'HEAD~1': $BASE_SHA" + else + echo "Last successful commit SHA: $LAST_SHA" + echo "##vso[task.setvariable variable=BASE_SHA]$LAST_SHA" + fi + displayName: 'Get last successful commit SHA' + condition: ne(variables['Build.Reason'], 'PullRequest') + env: + AZURE_DEVOPS_EXT_PAT: $(System.AccessToken) + + - script: npm install --prefix=$HOME/.local -g pnpm@8 + displayName: Install PNPM + + # 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 + # - 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 + condition: eq(variables['Build.Reason'], 'PullRequest') + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - script: pnpm exec nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - script: pnpm exec nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) lint test build +" +`; + +exports[`CI Workflow generator not connected to nxCloud with pnpm should generate bitbucket pipelines config 1`] = ` +"image: node:20 + +clone: + depth: full + +pipelines: + pull-requests: + '**': + - step: + name: 'Build and test affected apps on Pull Requests' + script: + - export NX_BRANCH=$BITBUCKET_PR_ID + + - npm install --prefix=$HOME/.local -g pnpm@8 + + # 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 + # - pnpm dlx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + + - pnpm install --frozen-lockfile + + - pnpm exec nx-cloud record -- nx format:check + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - pnpm exec nx affected --base=origin/main -t lint test build + + branches: + main: + - step: + name: 'Build and test affected apps on "main" branch changes' + script: + - export NX_BRANCH=$BITBUCKET_BRANCH + # 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 + # - 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 + + - pnpm install --frozen-lockfile + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - pnpm exec nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - pnpm exec nx affected -t lint test build --base=HEAD~1 +" +`; + +exports[`CI Workflow generator not connected to nxCloud with pnpm should generate circleci CI config 1`] = ` +"version: 2.1 + +orbs: + nx: nrwl/nx@1.6.2 + +jobs: + main: + docker: + - image: cimg/node:lts-browsers + steps: + - checkout + + - run: + name: Install PNPM + command: npm install --prefix=$HOME/.local -g pnpm@8 + + # 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: 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: + main-branch-name: 'main' + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: pnpm exec nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - run: pnpm exec nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build + +workflows: + version: 2 + + ci: + jobs: + - main +" +`; + +exports[`CI Workflow generator not connected to nxCloud with pnpm should generate github CI config 1`] = ` +"name: CI + +on: + push: + branches: + - main + pull_request: + +permissions: + actions: read + contents: read + +jobs: + main: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: pnpm/action-setup@v2 + with: + version: 8 + + # 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: 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 + with: + node-version: 20 + cache: 'pnpm' + + - run: pnpm install --frozen-lockfile + - uses: nrwl/nx-set-shas@v4 + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: pnpm exec nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - run: pnpm exec nx affected -t lint test build +" +`; + +exports[`CI Workflow generator not connected to nxCloud with pnpm should generate github CI config with custom name 1`] = ` +"name: My custom-workflow + +on: + push: + branches: + - main + pull_request: + +permissions: + actions: read + contents: read + +jobs: + main: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: pnpm/action-setup@v2 + with: + version: 8 + + # 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: 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 + with: + node-version: 20 + cache: 'pnpm' + + - run: pnpm install --frozen-lockfile + - uses: nrwl/nx-set-shas@v4 + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: pnpm exec nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - run: pnpm exec nx affected -t lint test build +" +`; + +exports[`CI Workflow generator not connected to nxCloud with pnpm should generate gitlab config 1`] = ` +"image: node:20 +variables: + CI: 'true' + +# Main job +CI: + interruptible: true + only: + - main + - merge_requests + script: + - npm install --prefix=$HOME/.local -g pnpm@8 + + # 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 + # - 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 + - NX_BASE=\${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA} + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - pnpm exec nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - pnpm exec nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build +" +`; + +exports[`CI Workflow generator not connected to nxCloud with yarn should generate azure CI config 1`] = ` +"name: CI + +trigger: + - main +pr: + - main + +variables: + CI: 'true' + \${{ if eq(variables['Build.Reason'], 'PullRequest') }}: + NX_BRANCH: $(System.PullRequest.PullRequestNumber) + TARGET_BRANCH: $[replace(variables['System.PullRequest.TargetBranch'],'refs/heads/','origin/')] + BASE_SHA: $(git merge-base $(TARGET_BRANCH) HEAD) + \${{ if ne(variables['Build.Reason'], 'PullRequest') }}: + NX_BRANCH: $(Build.SourceBranchName) + BASE_SHA: $(git rev-parse HEAD~1) + HEAD_SHA: $(git rev-parse HEAD) + +jobs: + - job: main + pool: + vmImage: 'ubuntu-latest' + steps: + - checkout: self + fetchDepth: 0 + # Set Azure Devops CLI default settings + - bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject) + displayName: 'Set default Azure DevOps organization and project' + # Get last successfull commit from Azure Devops CLI + - bash: | + LAST_SHA=$(az pipelines build list --branch $(Build.SourceBranchName) --definition-ids $(System.DefinitionId) --result succeeded --top 1 --query "[0].triggerInfo.\\"ci.sourceSha\\"") + if [ -z "$LAST_SHA" ] + then + echo "Last successful commit not found. Using fallback 'HEAD~1': $BASE_SHA" + else + echo "Last successful commit SHA: $LAST_SHA" + echo "##vso[task.setvariable variable=BASE_SHA]$LAST_SHA" + fi + displayName: 'Get last successful commit SHA' + condition: ne(variables['Build.Reason'], 'PullRequest') + env: + AZURE_DEVOPS_EXT_PAT: $(System.AccessToken) + + # 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 + # - 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 + condition: eq(variables['Build.Reason'], 'PullRequest') + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - script: yarn nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - script: yarn nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) lint test build +" +`; + +exports[`CI Workflow generator not connected to nxCloud with yarn should generate bitbucket pipelines config 1`] = ` +"image: node:20 + +clone: + depth: full + +pipelines: + pull-requests: + '**': + - step: + name: 'Build and test affected apps on Pull Requests' + script: + - export NX_BRANCH=$BITBUCKET_PR_ID + + # 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 + # - yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + + - yarn install --frozen-lockfile + + - yarn nx-cloud record -- nx format:check + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - yarn nx affected --base=origin/main -t lint test build + + branches: + main: + - step: + name: 'Build and test affected apps on "main" branch changes' + script: + - export NX_BRANCH=$BITBUCKET_BRANCH + # 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 + # - yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + + - yarn install --frozen-lockfile + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - yarn nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - yarn nx affected -t lint test build --base=HEAD~1 +" +`; + +exports[`CI Workflow generator not connected to nxCloud with yarn should generate circleci CI config 1`] = ` +"version: 2.1 + +orbs: + nx: nrwl/nx@1.6.2 + +jobs: + main: + docker: + - image: cimg/node:lts-browsers + steps: + - checkout + + # 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: yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + + - run: yarn install --frozen-lockfile + - nx/set-shas: + main-branch-name: 'main' + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: yarn nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - run: yarn nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build + +workflows: + version: 2 + + ci: + jobs: + - main +" +`; + +exports[`CI Workflow generator not connected to nxCloud with yarn should generate github CI config 1`] = ` +"name: CI + +on: + push: + branches: + - main + pull_request: + +permissions: + actions: read + contents: read + +jobs: + main: + runs-on: ubuntu-latest + steps: + - 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: yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + + # Cache node_modules + - uses: actions/setup-node@v3 + with: + node-version: 20 + cache: 'yarn' + + - run: yarn install --frozen-lockfile + - uses: nrwl/nx-set-shas@v4 + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: yarn nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - run: yarn nx affected -t lint test build +" +`; + +exports[`CI Workflow generator not connected to nxCloud with yarn should generate github CI config with custom name 1`] = ` +"name: My custom-workflow + +on: + push: + branches: + - main + pull_request: + +permissions: + actions: read + contents: read + +jobs: + main: + runs-on: ubuntu-latest + steps: + - 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: yarn nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" + + # Cache node_modules + - uses: actions/setup-node@v3 + with: + node-version: 20 + cache: 'yarn' + + - run: yarn install --frozen-lockfile + - uses: nrwl/nx-set-shas@v4 + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: yarn nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - run: yarn nx affected -t lint test build +" +`; + +exports[`CI Workflow generator not connected to nxCloud with yarn should generate gitlab config 1`] = ` +"image: node:20 +variables: + CI: 'true' + +# Main job +CI: + interruptible: true + only: + - main + - merge_requests + script: + # 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 + # - 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 + - NX_BASE=\${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA} + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - yarn nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - yarn nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build +" +`; + +exports[`CI Workflow generator optional e2e should add e2e to azure CI config 1`] = ` +"name: CI + +trigger: + - main +pr: + - main + +variables: + CI: 'true' + \${{ if eq(variables['Build.Reason'], 'PullRequest') }}: + NX_BRANCH: $(System.PullRequest.PullRequestNumber) + TARGET_BRANCH: $[replace(variables['System.PullRequest.TargetBranch'],'refs/heads/','origin/')] + BASE_SHA: $(git merge-base $(TARGET_BRANCH) HEAD) + \${{ if ne(variables['Build.Reason'], 'PullRequest') }}: + NX_BRANCH: $(Build.SourceBranchName) + BASE_SHA: $(git rev-parse HEAD~1) + HEAD_SHA: $(git rev-parse HEAD) + +jobs: + - job: main + pool: + vmImage: 'ubuntu-latest' + steps: + - checkout: self + fetchDepth: 0 + # Set Azure Devops CLI default settings + - bash: az devops configure --defaults organization=$(System.TeamFoundationCollectionUri) project=$(System.TeamProject) + displayName: 'Set default Azure DevOps organization and project' + # Get last successfull commit from Azure Devops CLI + - bash: | + LAST_SHA=$(az pipelines build list --branch $(Build.SourceBranchName) --definition-ids $(System.DefinitionId) --result succeeded --top 1 --query "[0].triggerInfo.\\"ci.sourceSha\\"") + if [ -z "$LAST_SHA" ] + then + echo "Last successful commit not found. Using fallback 'HEAD~1': $BASE_SHA" + else + echo "Last successful commit SHA: $LAST_SHA" + echo "##vso[task.setvariable variable=BASE_SHA]$LAST_SHA" + fi + displayName: 'Get last successful commit SHA' + condition: ne(variables['Build.Reason'], 'PullRequest') + env: + AZURE_DEVOPS_EXT_PAT: $(System.AccessToken) + + # 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 + condition: eq(variables['Build.Reason'], 'PullRequest') + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - script: npx nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) lint test build + - script: npx nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) --parallel 1 e2e-ci +" +`; + +exports[`CI Workflow generator optional e2e should add e2e to bitbucket pipelines config 1`] = ` +"image: node:20 + +clone: + depth: full + +pipelines: + pull-requests: + '**': + - step: + name: 'Build and test affected apps on Pull Requests' + script: + - export NX_BRANCH=$BITBUCKET_PR_ID + + # 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 + + - npx nx-cloud record -- nx format:check + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - npx nx affected --base=origin/main -t lint test build + - npx nx affected --base=origin/main --parallel 1 -t e2e-ci + + branches: + main: + - step: + name: 'Build and test affected apps on "main" branch changes' + script: + - export NX_BRANCH=$BITBUCKET_BRANCH + # 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 + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - npx nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected + - npx nx affected -t lint test build e2e-ci --base=HEAD~1 +" +`; + +exports[`CI Workflow generator optional e2e should add e2e to circleci CI config 1`] = ` +"version: 2.1 + +orbs: + nx: nrwl/nx@1.6.2 + +jobs: + main: + docker: + - image: cimg/node:lts-browsers + steps: + - checkout + + # 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: + main-branch-name: 'main' + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: npx nx-cloud record -- echo Hello World + # 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 --base=$NX_BASE --head=$NX_HEAD -t lint test build + - run: npx nx affected --base=$NX_BASE --head=$NX_HEAD --parallel 1 -t e2e-ci + +workflows: + version: 2 + + ci: + jobs: + - main +" +`; + +exports[`CI Workflow generator optional e2e should add e2e to github CI config 1`] = ` +"name: CI + +on: + push: + branches: + - main + pull_request: + +permissions: + actions: read + contents: read + +jobs: + main: + runs-on: ubuntu-latest + steps: + - 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 + - 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 + with: + node-version: 20 + cache: 'npm' + + - run: npm ci + - uses: nrwl/nx-set-shas@v4 + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: npx nx-cloud record -- echo Hello World + # 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: npx nx affected --parallel 1 -t e2e-ci +" +`; + +exports[`CI Workflow generator optional e2e should add e2e to github CI config with custom name 1`] = ` +"name: My custom-workflow + +on: + push: + branches: + - main + pull_request: + +permissions: + actions: read + contents: read + +jobs: + main: + runs-on: ubuntu-latest + steps: + - 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 + - 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 + with: + node-version: 20 + cache: 'npm' + + - run: npm ci + - uses: nrwl/nx-set-shas@v4 + + # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud + # - run: npx nx-cloud record -- echo Hello World + # 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: npx nx affected --parallel 1 -t e2e-ci +" +`; 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 8d8b25acc243a..cb045a0947187 100644 --- a/packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts +++ b/packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts @@ -35,103 +35,130 @@ describe('CI Workflow generator', () => { beforeEach(() => { tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); - const nxJson = readNxJson(tree); - nxJson.nxCloudAccessToken = 'test'; - updateNxJson(tree, nxJson); }); afterEach(() => { vol.reset(); }); - ['npm', 'yarn', 'pnpm', 'bun'].forEach((packageManager: PackageManager) => { - describe(`with ${packageManager}`, () => { - beforeEach(() => { - let fileSys; - if (packageManager === 'bun') { - fileSys = { 'bun.lockb': '' }; - } else if (packageManager === 'yarn') { - fileSys = { 'yarn.lock': '' }; - } else if (packageManager === 'pnpm') { - fileSys = { 'pnpm-lock.yaml': '' }; - } else { - fileSys = { 'package-lock.json': '' }; - } - vol.fromJSON(fileSys, ''); - }); + describe.each([ + ['connected to nxCloud', true], + ['not connected to nxCloud', false], + ] as const)(`%s`, (_, connectedToCloud) => { + let nxCloudAccessToken: string; - it('should generate github CI config', async () => { - await ciWorkflowGenerator(tree, { ci: 'github', name: 'CI' }); + beforeEach(() => { + if (connectedToCloud) { + const nxJson = readNxJson(tree); + nxJson.nxCloudAccessToken = 'test'; + updateNxJson(tree, nxJson); + } else { + nxCloudAccessToken = process.env.NX_CLOUD_ACCESS_TOKEN; + delete process.env.NX_CLOUD_ACCESS_TOKEN; + } + }); - expect( - tree.read('.github/workflows/ci.yml', 'utf-8') - ).toMatchSnapshot(); - }); + afterEach(() => { + if (connectedToCloud) { + const nxJson = readNxJson(tree); + delete nxJson.nxCloudAccessToken; + updateNxJson(tree, nxJson); + } else { + process.env.NX_CLOUD_ACCESS_TOKEN = nxCloudAccessToken; + } + }); - it('should generate circleci CI config', async () => { - await ciWorkflowGenerator(tree, { ci: 'circleci', name: 'CI' }); + ['npm', 'yarn', 'pnpm', 'bun'].forEach((packageManager: PackageManager) => { + describe(`with ${packageManager}`, () => { + beforeEach(() => { + let fileSys; + if (packageManager === 'bun') { + fileSys = { 'bun.lockb': '' }; + } else if (packageManager === 'yarn') { + fileSys = { 'yarn.lock': '' }; + } else if (packageManager === 'pnpm') { + fileSys = { 'pnpm-lock.yaml': '' }; + } else { + fileSys = { 'package-lock.json': '' }; + } + vol.fromJSON(fileSys, ''); + }); - expect(tree.read('.circleci/config.yml', 'utf-8')).toMatchSnapshot(); - }); + it('should generate github CI config', async () => { + await ciWorkflowGenerator(tree, { ci: 'github', name: 'CI' }); - it('should generate azure CI config', async () => { - await ciWorkflowGenerator(tree, { ci: 'azure', name: 'CI' }); + expect( + tree.read('.github/workflows/ci.yml', 'utf-8') + ).toMatchSnapshot(); + }); - expect(tree.read('azure-pipelines.yml', 'utf-8')).toMatchSnapshot(); - }); + it('should generate circleci CI config', async () => { + await ciWorkflowGenerator(tree, { ci: 'circleci', name: 'CI' }); - it('should generate github CI config with custom name', async () => { - await ciWorkflowGenerator(tree, { - ci: 'github', - name: 'My custom-workflow', + expect(tree.read('.circleci/config.yml', 'utf-8')).toMatchSnapshot(); }); - expect( - tree.read('.github/workflows/my-custom-workflow.yml', 'utf-8') - ).toMatchSnapshot(); - }); + it('should generate azure CI config', async () => { + await ciWorkflowGenerator(tree, { ci: 'azure', name: 'CI' }); - it('should generate bitbucket pipelines config', async () => { - await ciWorkflowGenerator(tree, { - ci: 'bitbucket-pipelines', - name: 'CI', + expect(tree.read('azure-pipelines.yml', 'utf-8')).toMatchSnapshot(); }); - expect(tree.read('bitbucket-pipelines.yml', 'utf-8')).toMatchSnapshot(); - }); + it('should generate github CI config with custom name', async () => { + await ciWorkflowGenerator(tree, { + ci: 'github', + name: 'My custom-workflow', + }); + + expect( + tree.read('.github/workflows/my-custom-workflow.yml', 'utf-8') + ).toMatchSnapshot(); + }); - it('should prefix nx.json affected defaultBase with origin/ if ci is bitbucket-pipelines', async () => { - const nxJson = readJson(tree, 'nx.json'); - nxJson.affected.defaultBase = 'my-branch'; - writeJson(tree, 'nx.json', nxJson); + it('should generate bitbucket pipelines config', async () => { + await ciWorkflowGenerator(tree, { + ci: 'bitbucket-pipelines', + name: 'CI', + }); - await ciWorkflowGenerator(tree, { - ci: 'bitbucket-pipelines', - name: 'CI', + expect( + tree.read('bitbucket-pipelines.yml', 'utf-8') + ).toMatchSnapshot(); }); - expect(readJson(tree, 'nx.json').affected.defaultBase).toEqual( - 'origin/my-branch' - ); - }); + it('should prefix nx.json affected defaultBase with origin/ if ci is bitbucket-pipelines', async () => { + const nxJson = readJson(tree, 'nx.json'); + nxJson.affected.defaultBase = 'my-branch'; + writeJson(tree, 'nx.json', nxJson); - it('should prefix nx.json base with origin/ if ci is bitbucket-pipelines', async () => { - const nxJson = readNxJson(tree); - nxJson.defaultBase = 'my-branch'; - writeJson(tree, 'nx.json', nxJson); + await ciWorkflowGenerator(tree, { + ci: 'bitbucket-pipelines', + name: 'CI', + }); - await ciWorkflowGenerator(tree, { - ci: 'bitbucket-pipelines', - name: 'CI', + expect(readJson(tree, 'nx.json').affected.defaultBase).toEqual( + 'origin/my-branch' + ); }); - expect(readNxJson(tree).defaultBase).toEqual('origin/my-branch'); - }); + it('should prefix nx.json base with origin/ if ci is bitbucket-pipelines', async () => { + const nxJson = readNxJson(tree); + nxJson.defaultBase = 'my-branch'; + writeJson(tree, 'nx.json', nxJson); + + await ciWorkflowGenerator(tree, { + ci: 'bitbucket-pipelines', + name: 'CI', + }); - it('should generate gitlab config', async () => { - await ciWorkflowGenerator(tree, { ci: 'gitlab', name: 'CI' }); + expect(readNxJson(tree).defaultBase).toEqual('origin/my-branch'); + }); - expect(tree.read('.gitlab-ci.yml', 'utf-8')).toMatchSnapshot(); + it('should generate gitlab config', async () => { + await ciWorkflowGenerator(tree, { ci: 'gitlab', name: 'CI' }); + + expect(tree.read('.gitlab-ci.yml', 'utf-8')).toMatchSnapshot(); + }); }); }); }); 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 009f88706c156..09087f88fe2b5 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__ @@ -65,5 +65,6 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - script: <%= packageManagerPrefix %> nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - script: <%= packageManagerPrefix %> nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) lint test build<% if(hasE2E){ %> - script: <%= packageManagerPrefix %> nx affected --base=$(BASE_SHA) --head=$(HEAD_SHA) --parallel 1 e2e-ci<% } %> 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 7978cdaafc23c..4d91ce066fafa 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__ @@ -29,6 +29,7 @@ pipelines: - <%= packageManagerInstall %> - <%= packageManagerPrefix %> nx-cloud record -- nx format:check + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - <%= packageManagerPrefix %> nx affected --base=origin/<%= mainBranch %> -t lint test build<% if(hasE2E){ %> - <%= packageManagerPrefix %> nx affected --base=origin/<%= mainBranch %> --parallel 1 -t e2e-ci<% } %> @@ -55,4 +56,5 @@ pipelines: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - <%= packageManagerPrefix %> nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - <%= packageManagerPrefix %> nx affected -t lint test build<% if(hasE2E){ %> e2e-ci<% } %> --base=HEAD~1 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 da824022adf6b..89164ad1d3d6d 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__ @@ -34,6 +34,7 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: <%= packageManagerPrefix %> nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: <%= packageManagerPrefix %> nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build<% if(hasE2E){ %> - run: <%= packageManagerPrefix %> nx affected --base=$NX_BASE --head=$NX_HEAD --parallel 1 -t e2e-ci<% } %> 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 09b91c147ed1f..f7966d14f5c5f 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__ @@ -47,5 +47,6 @@ jobs: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - run: <%= packageManagerPrefix %> nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: <%= packageManagerPrefix %> nx affected -t lint test build<% if(hasE2E){ %> - run: <%= packageManagerPrefix %> nx affected --parallel 1 -t e2e-ci<% } %> 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 e029f6a836134..c6e336e75712e 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__ @@ -28,5 +28,6 @@ variables: # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - <%= packageManagerPrefix %> nx-cloud record -- echo Hello World + # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - <%= packageManagerPrefix %> nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build<% if(hasE2E){ %> - <%= packageManagerPrefix %> nx affected --base=$NX_BASE --head=$NX_HEAD --parallel 1 -t e2e-ci<% } %>