Skip to content

Commit

Permalink
feat(core): ci-workflow adds workflow file to sharedGlobal inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Jul 16, 2024
1 parent 34da542 commit 6a67c39
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
16 changes: 16 additions & 0 deletions packages/workspace/src/generators/ci-workflow/ci-workflow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,20 @@ describe('CI Workflow generator', () => {
expect(tree.read('bitbucket-pipelines.yml', 'utf-8')).toMatchSnapshot();
});
});

it('should add workflow files to namedInputs.sharedGlobals', async () => {
await ciWorkflowGenerator(tree, { ci: 'azure', name: 'CI' });
await ciWorkflowGenerator(tree, { ci: 'bitbucket-pipelines', name: 'CI' });
await ciWorkflowGenerator(tree, { ci: 'circleci', name: 'CI' });
await ciWorkflowGenerator(tree, { ci: 'github', name: 'CI' });
await ciWorkflowGenerator(tree, { ci: 'gitlab', name: 'CI' });

expect(readJson(tree, 'nx.json').namedInputs.sharedGlobals).toEqual([
'{workspaceRoot}/azure-pipelines.yml',
'{workspaceRoot}/bitbucket-pipelines.yml',
'{workspaceRoot}/.circleci/config.yml',
'{workspaceRoot}/.github/workflows/ci.yml',
'{workspaceRoot}/.gitlab-ci.yml',
]);
});
});
44 changes: 34 additions & 10 deletions packages/workspace/src/generators/ci-workflow/ci-workflow.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
Tree,
names,
detectPackageManager,
formatFiles,
generateFiles,
getPackageManagerCommand,
readJson,
names,
NxJsonConfiguration,
formatFiles,
readJson,
Tree,
writeJson,
detectPackageManager,
} from '@nx/devkit';
import { deduceDefaultBase } from '../../utilities/default-base';
import { join } from 'path';
Expand All @@ -20,14 +20,19 @@ export interface Schema {

export async function ciWorkflowGenerator(tree: Tree, schema: Schema) {
const ci = schema.ci;

const options = normalizeOptions(schema, tree);
const nxJson: NxJsonConfiguration = readJson(tree, 'nx.json');

if (ci === 'bitbucket-pipelines' && defaultBranchNeedsOriginPrefix(nxJson)) {
writeJson(tree, 'nx.json', appendOriginPrefix(nxJson));
appendOriginPrefix(nxJson);
}

const options = normalizeOptions(schema, tree);
generateFiles(tree, join(__dirname, 'files', ci), '', options);

addWorkflowFileToSharedGlobals(nxJson, schema.ci, options.workflowFileName);

writeJson(tree, 'nx.json', nxJson);

await formatFiles(tree);
}

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

function appendOriginPrefix(nxJson: NxJsonConfiguration): NxJsonConfiguration {
function appendOriginPrefix(nxJson: NxJsonConfiguration): void {
if (nxJson?.affected?.defaultBase) {
nxJson.affected.defaultBase = `origin/${nxJson.affected.defaultBase}`;
}
if (nxJson.defaultBase || !nxJson.affected) {
nxJson.defaultBase = `origin/${nxJson.defaultBase ?? deduceDefaultBase()}`;
}
return nxJson;
}

const ciWorkflowInputs: Record<Schema['ci'], string> = {
azure: 'azure-pipelines.yml',
'bitbucket-pipelines': 'bitbucket-pipelines.yml',
circleci: '.circleci/config.yml',
github: '.github/workflows/',
gitlab: '.gitlab-ci.yml',
};

function addWorkflowFileToSharedGlobals(
nxJson: NxJsonConfiguration,
ci: Schema['ci'],
workflowFileName: string
): void {
let input = `{workspaceRoot}/${ciWorkflowInputs[ci]}`;
if (ci === 'github') input += `${workflowFileName}.yml`;
nxJson.namedInputs ??= {};
nxJson.namedInputs.sharedGlobals ??= [];
nxJson.namedInputs.sharedGlobals.push(input);
}

0 comments on commit 6a67c39

Please sign in to comment.