Skip to content

Commit

Permalink
feat(core): improve the commented dte message on ci-workflows (#21524)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored Feb 2, 2024
1 parent 433d5f2 commit 5a818e3
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"`;
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ function setNxCloud(tree: Tree) {
return {
...json,
nxCloudAccessToken: 'xxxx-xxx-xxxx',
nxCloudUrl: 'https://my.nx.app',
};
});
}
21 changes: 14 additions & 7 deletions packages/workspace/src/generators/ci-workflow/ci-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand All @@ -46,23 +47,29 @@ 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,
packageManager,
packageManagerInstall,
packageManagerPrefix,
mainBranch: deduceDefaultBase(),
nxCloudHost,
tmpl: '',
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'){ %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'){ %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'){ %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'){ %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'){ %>
Expand Down

0 comments on commit 5a818e3

Please sign in to comment.