Skip to content

Commit

Permalink
Merge pull request #25 from cloud-native-toolkit/develop
Browse files Browse the repository at this point in the history
fix: added missing auto-sequence to tasks and fixed duplicate tasks
  • Loading branch information
nathanagood authored Sep 20, 2023
2 parents ad47527 + 23ccf52 commit 6fe745c
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 14 deletions.
49 changes: 38 additions & 11 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,12 @@ export class PipelineBuilder {
const pipelineParams = new Map<string, PipelineParam>();
const pipelineWorkspaces = new Map<string, PipelineWorkspace>();
const pipelineTasks: PipelineTask[] = new Array<PipelineTask>();
// For making a list to make sure that tasks aren't duplicated when doing
// the build. Not that it really hurts anything, but it makes the multidoc
// YAML file bigger and more complex than it needs to be.
const taskList: string[] = new Array<string>();

this._tasks?.forEach(t => {
this._tasks?.forEach((t, i) => {

const taskParams: TaskParam[] = new Array<TaskParam>();
const taskWorkspaces: PipelineTaskWorkspace[] = new Array<TaskWorkspace>();
Expand Down Expand Up @@ -702,19 +706,20 @@ export class PipelineBuilder {
});
});

pipelineTasks.push({
name: t.logicalID,
taskRef: {
name: t.name,
},
params: taskParams,
workspaces: taskWorkspaces,
});
const pt = createOrderedPipelineTask(t, ((i > 0) ? this._tasks![i - 1].logicalID : ''), taskParams, taskWorkspaces);

pipelineTasks.push(pt);

if (opts.includeDependencies) {
// Build the task if the user has asked for the dependencies to be
// built along with the pipeline.
t.buildTask();
// built along with the pipeline, but only if we haven't already
// built the task yet.
if (!taskList.find(it => {
return it == t.name;
})) {
t.buildTask();
}
taskList.push(t.name!);
}
});

Expand All @@ -732,3 +737,25 @@ export class PipelineBuilder {
});
}
}

function createOrderedPipelineTask(t: TaskBuilder, after: string, params: TaskParam[], ws: TaskWorkspace[]): PipelineTask {
if (after) {
return {
name: t.logicalID,
taskRef: {
name: t.name,
},
runAfter: [after],
params: params,
workspaces: ws,
};
}
return {
name: t.logicalID,
taskRef: {
name: t.name,
},
params: params,
workspaces: ws,
};
}
95 changes: 95 additions & 0 deletions test/__snapshots__/pipelinebuilder.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 36 additions & 3 deletions test/pipelinebuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Chart, Testing } from 'cdk8s';
import { ChartProps } from 'cdk8s/lib/chart';
import { Construct } from 'constructs';
// @ts-ignore
import { Pipeline, PipelineBuilder, PipelineTaskBuilder, PipelineTaskDef, TaskRef, TaskBuilder, WorkspaceBuilder, ParameterBuilder } from '../src';
import { ParameterBuilder, Pipeline, PipelineBuilder, PipelineTaskBuilder, PipelineTaskDef, TaskBuilder, TaskRef, WorkspaceBuilder } from '../src';

class MyTestChart extends Chart {
constructor(scope: Construct, id: string, props?: ChartProps) {
Expand Down Expand Up @@ -51,7 +51,7 @@ class MySecondTestChart extends Chart {
}
}

class MyTestChartWithDups extends Chart {
class MyTestChartWithDuplicateParams extends Chart {
constructor(scope: Construct, id: string, props?: ChartProps) {
super(scope, id, props);

Expand Down Expand Up @@ -107,6 +107,32 @@ class MyTestChartWithStaticOverride extends Chart {
}
}

class MyTestChartWithDuplicateTasks extends Chart {
constructor(scope: Construct, id: string, props?: ChartProps) {
super(scope, id, props);

const myWorkspace = new WorkspaceBuilder('output')
.withDescription('The files cloned by the task')
.withName('shared-data');

const urlParam = new ParameterBuilder('url')
.withPiplineParameter('repo-url', '');

const myTask = new TaskBuilder(this, 'git-clone')
.withName('fetch-source')
.withWorkspace(myWorkspace)
.withStringParam(urlParam)
;

new PipelineBuilder(this, 'my-pipeline')
.withName('clone-build-push')
.withDescription('This pipeline closes a repository, builds a Docker image, etc.')
.withTask(myTask)
.withTask(myTask)
.buildPipeline({ includeDependencies: true });
}
}

describe('PipelineBuilderTest', () => {
test('PipelineBuilder', () => {
const app = Testing.app();
Expand All @@ -124,7 +150,7 @@ describe('PipelineBuilderTest', () => {

test('PipelineBuilderWithDuplicates', () => {
const app = Testing.app();
const chart = new MyTestChartWithDups(app, 'test-chart');
const chart = new MyTestChartWithDuplicateParams(app, 'test-chart');
const results = Testing.synth(chart);
expect(results).toMatchSnapshot();
});
Expand All @@ -135,4 +161,11 @@ describe('PipelineBuilderTest', () => {
const results = Testing.synth(chart);
expect(results).toMatchSnapshot();
});

test('PipelineBuilderWithDuplicateTasks', () => {
const app = Testing.app();
const chart = new MyTestChartWithDuplicateTasks(app, 'test-chart');
const results = Testing.synth(chart);
expect(results).toMatchSnapshot();
});
});

0 comments on commit 6fe745c

Please sign in to comment.