Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added TaskBuilder method to specify the runAfter value for tasks in a pipeline #48

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 41 additions & 7 deletions API.md

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

51 changes: 42 additions & 9 deletions src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ export class TaskBuilder {
private _labels?: {
[key: string]: string;
};
private _runafter?: string[];

/**
* Creates a new instance of the `TaskBuilder` using the given `scope` and
Expand Down Expand Up @@ -717,9 +718,10 @@ export class TaskBuilder {

/**
* Gets the name of the `Task` in the context of a pipeline.
* If not set, the 'Task' id is used.
*/
public get name(): string | undefined {
return this._name;
public get name(): string {
return this._name || this._id;
}

/**
Expand Down Expand Up @@ -807,6 +809,25 @@ export class TaskBuilder {
return this;
}

/**
* Allows you to specify the names of which task(s), if any, the 'Task' should
* run after in a pipeline. An empty array as input indicates the 'Task' yaml
* should have no runAfter field.
* By default, the value of runAfter is set to the preceeding 'Task' in the pipeline.
* @param taskArray
*/
public specifyRunAfter(taskArray: string[]): TaskBuilder {
this._runafter = taskArray;
return this;
}

/**
* Gets the list of task names for the runAfter value of the `Task`.
*/
public get runAfter(): string[] | undefined {
return this._runafter;
}

/**
* Builds the `Task`.
*/
Expand Down Expand Up @@ -1019,7 +1040,7 @@ export class PipelineBuilder {

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

const taskName = t.name || t.logicalID;
const taskName = t.name;
if (taskNames.find(it => {
return it == taskName;
})) {
Expand All @@ -1044,7 +1065,19 @@ export class PipelineBuilder {
});
});

const pt = createOrderedPipelineTask(t, ((i > 0) ? (this._tasks![i - 1].name || this._tasks![i - 1].logicalID) : ''), taskParams, taskWorkspaces);
const after = [];
if (t.runAfter != undefined) {
t.runAfter.forEach(name => {
if (!this._tasks?.find(it => {return (it.name) == name;})) {
throw new Error(`'${name}' supplied as value for runAfter but no such task found in pipeline.`);
}
after.push(name);
});
} else if (i > 0) {
after.push(this._tasks![i - 1].name);
}

const pt = createOrderedPipelineTask(t, after, taskParams, taskWorkspaces);

pipelineTasks.push(pt);

Expand Down Expand Up @@ -1076,20 +1109,20 @@ export class PipelineBuilder {
}
}

function createOrderedPipelineTask(t: TaskBuilder, after: string, params: TaskParam[], ws: TaskWorkspace[]): PipelineTask {
if (after) {
function createOrderedPipelineTask(t: TaskBuilder, after: string[], params: TaskParam[], ws: TaskWorkspace[]): PipelineTask {
if (after.length) {
return {
name: t.name || t.logicalID,
name: t.name,
taskRef: {
name: t.logicalID,
},
runAfter: [after],
runAfter: after,
params: params,
workspaces: ws,
};
}
return {
name: t.name || t.logicalID,
name: t.name,
taskRef: {
name: t.logicalID,
},
Expand Down
81 changes: 81 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.

52 changes: 52 additions & 0 deletions test/pipelinebuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,43 @@ class MyTestChartWithSimilarTasks extends Chart {
}
}

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

const firstTask = new TaskBuilder(this, 'git-clone')
.withName('fetch-source')
.specifyRunAfter([]);

const secondTask = new TaskBuilder(this, 'git-clone')
.withName('fetch-again');

const thirdTask = new TaskBuilder(this, 'print-readme')
.withName('cat-readme')
.specifyRunAfter(['fetch-again']);

new PipelineBuilder(this, 'clone-read')
.withTask(thirdTask)
.withTask(firstTask)
.withTask(secondTask)
.buildPipeline({ includeDependencies: true });
}
}

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

const myTask = new TaskBuilder(this, 'print-readme')
.withName('cat-readme')
.specifyRunAfter(['fetch-source']);

new PipelineBuilder(this, 'clone-read')
.withTask(myTask)
.buildPipeline({ includeDependencies: true });
}
}

describe('PipelineBuilderTest', () => {
test('PipelineRunBuilder', () => {
const app = Testing.app();
Expand Down Expand Up @@ -383,4 +420,19 @@ describe('PipelineBuilderTest', () => {
const results = Testing.synth(chart);
expect(results).toMatchSnapshot();
});

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

test('PipelineBuilderWithRunAfterError', () => {
const app = Testing.app();
const f = () => {
new MyTestChartWithRunAfterError(app, 'test-chart');
};
expect(f).toThrowError('\'fetch-source\' supplied as value for runAfter but no such task found in pipeline.');
});
});
Loading