Skip to content

Commit 1a94296

Browse files
authored
feat(core): ci-workflow adds workflow file to sharedGlobal inputs (#26948)
When we generate the workflow file, users should also add the new workflow file to the `sharedGlobals` named input, otherwise changes may land without verifying that all tasks are successful. This is already called out in the CI tutorials, and doing it automatically improves the DX. ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent 34da542 commit 1a94296

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,20 @@ describe('CI Workflow generator', () => {
212212
expect(tree.read('bitbucket-pipelines.yml', 'utf-8')).toMatchSnapshot();
213213
});
214214
});
215+
216+
it('should add workflow files to namedInputs.sharedGlobals', async () => {
217+
await ciWorkflowGenerator(tree, { ci: 'azure', name: 'CI' });
218+
await ciWorkflowGenerator(tree, { ci: 'bitbucket-pipelines', name: 'CI' });
219+
await ciWorkflowGenerator(tree, { ci: 'circleci', name: 'CI' });
220+
await ciWorkflowGenerator(tree, { ci: 'github', name: 'CI' });
221+
await ciWorkflowGenerator(tree, { ci: 'gitlab', name: 'CI' });
222+
223+
expect(readJson(tree, 'nx.json').namedInputs.sharedGlobals).toEqual([
224+
'{workspaceRoot}/azure-pipelines.yml',
225+
'{workspaceRoot}/bitbucket-pipelines.yml',
226+
'{workspaceRoot}/.circleci/config.yml',
227+
'{workspaceRoot}/.github/workflows/ci.yml',
228+
'{workspaceRoot}/.gitlab-ci.yml',
229+
]);
230+
});
215231
});

packages/workspace/src/generators/ci-workflow/ci-workflow.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {
2-
Tree,
3-
names,
2+
detectPackageManager,
3+
formatFiles,
44
generateFiles,
55
getPackageManagerCommand,
6-
readJson,
6+
names,
77
NxJsonConfiguration,
8-
formatFiles,
8+
readJson,
9+
Tree,
910
writeJson,
10-
detectPackageManager,
1111
} from '@nx/devkit';
1212
import { deduceDefaultBase } from '../../utilities/default-base';
1313
import { join } from 'path';
@@ -20,14 +20,19 @@ export interface Schema {
2020

2121
export async function ciWorkflowGenerator(tree: Tree, schema: Schema) {
2222
const ci = schema.ci;
23-
23+
const options = normalizeOptions(schema, tree);
2424
const nxJson: NxJsonConfiguration = readJson(tree, 'nx.json');
25+
2526
if (ci === 'bitbucket-pipelines' && defaultBranchNeedsOriginPrefix(nxJson)) {
26-
writeJson(tree, 'nx.json', appendOriginPrefix(nxJson));
27+
appendOriginPrefix(nxJson);
2728
}
2829

29-
const options = normalizeOptions(schema, tree);
3030
generateFiles(tree, join(__dirname, 'files', ci), '', options);
31+
32+
addWorkflowFileToSharedGlobals(nxJson, schema.ci, options.workflowFileName);
33+
34+
writeJson(tree, 'nx.json', nxJson);
35+
3136
await formatFiles(tree);
3237
}
3338

@@ -93,12 +98,31 @@ function defaultBranchNeedsOriginPrefix(nxJson: NxJsonConfiguration): boolean {
9398
return !base?.startsWith('origin/');
9499
}
95100

96-
function appendOriginPrefix(nxJson: NxJsonConfiguration): NxJsonConfiguration {
101+
function appendOriginPrefix(nxJson: NxJsonConfiguration): void {
97102
if (nxJson?.affected?.defaultBase) {
98103
nxJson.affected.defaultBase = `origin/${nxJson.affected.defaultBase}`;
99104
}
100105
if (nxJson.defaultBase || !nxJson.affected) {
101106
nxJson.defaultBase = `origin/${nxJson.defaultBase ?? deduceDefaultBase()}`;
102107
}
103-
return nxJson;
108+
}
109+
110+
const ciWorkflowInputs: Record<Schema['ci'], string> = {
111+
azure: 'azure-pipelines.yml',
112+
'bitbucket-pipelines': 'bitbucket-pipelines.yml',
113+
circleci: '.circleci/config.yml',
114+
github: '.github/workflows/',
115+
gitlab: '.gitlab-ci.yml',
116+
};
117+
118+
function addWorkflowFileToSharedGlobals(
119+
nxJson: NxJsonConfiguration,
120+
ci: Schema['ci'],
121+
workflowFileName: string
122+
): void {
123+
let input = `{workspaceRoot}/${ciWorkflowInputs[ci]}`;
124+
if (ci === 'github') input += `${workflowFileName}.yml`;
125+
nxJson.namedInputs ??= {};
126+
nxJson.namedInputs.sharedGlobals ??= [];
127+
nxJson.namedInputs.sharedGlobals.push(input);
104128
}

0 commit comments

Comments
 (0)