diff --git a/API.md b/API.md index 97c1333..6be3a65 100644 --- a/API.md +++ b/API.md @@ -3321,6 +3321,7 @@ new TaskBuilder(scope: Construct, id: string) | **Name** | **Description** | | --- | --- | | buildTask | Builds the `Task`. | +| referencingTask | Sets the taskRef field of the 'Task'. | | specifyRunAfter | Allows you to specify the names of which task(s), if any, the 'Task' should run after in a pipeline. | | withAnnotation | Adds an annotation to the `Task` `metadata` with the provided key and value. | | withDescription | Sets the `description` of the `Task` being built. | @@ -3341,6 +3342,23 @@ public buildTask(): void Builds the `Task`. +##### `referencingTask` + +```typescript +public referencingTask(taskRef: string): TaskBuilder +``` + +Sets the taskRef field of the 'Task'. + +Use only for tasks within pipelines: +overrides logicalID as the name of the 'Task' in its individual yaml. + +###### `taskRef`Required + +- *Type:* string + +--- + ##### `specifyRunAfter` ```typescript @@ -3510,6 +3528,7 @@ Adds the specified workspace to the `Task`. | --- | --- | --- | | logicalID | string | *No description.* | | name | string | Gets the name of the `Task` in the context of a pipeline. | +| taskRef | string | Gets the taskRef field of the `Task` for use within a pipeline. | | description | string | Gets the `description` of the `Task`. | | parameters | ParameterBuilder[] | *No description.* | | runAfter | string[] | Gets the list of task names for the runAfter value of the `Task`. | @@ -3541,6 +3560,20 @@ If not set, the 'Task' id is used. --- +##### `taskRef`Required + +```typescript +public readonly taskRef: string; +``` + +- *Type:* string + +Gets the taskRef field of the `Task` for use within a pipeline. + +If not set, the 'Task' id is used. + +--- + ##### `description`Optional ```typescript diff --git a/src/builders.ts b/src/builders.ts index 166e188..72abcf8 100644 --- a/src/builders.ts +++ b/src/builders.ts @@ -653,6 +653,7 @@ export class TaskBuilder { private _steps?: TaskStepBuilder[]; private _name?: string; private _description?: string; + private _taskref?: string; // These were initially arrays, but converted them to maps so that if // multiple values are added that the last one will win. private _workspaces = new Map; @@ -828,6 +829,24 @@ export class TaskBuilder { return this._runafter; } + /** + * Sets the taskRef field of the 'Task'. Use only for tasks within pipelines: + * overrides logicalID as the name of the 'Task' in its individual yaml. + * @param taskRef + */ + public referencingTask(taskRef: string): TaskBuilder { + this._taskref = taskRef; + return this; + } + + /** + * Gets the taskRef field of the `Task` for use within a pipeline. + * If not set, the 'Task' id is used. + */ + public get taskRef(): string { + return this._taskref || this._id; + } + /** * Builds the `Task`. */ @@ -861,7 +880,7 @@ export class TaskBuilder { const props: TaskProps = { metadata: { - name: this.logicalID, + name: this.taskRef, labels: this._labels, annotations: this._annotations, }, @@ -1084,13 +1103,13 @@ export class PipelineBuilder { if (opts.includeDependencies) { // Build the task if the user has asked for the dependencies to be // built along with the pipeline, but only if we haven't already - // built the task yet. + // built the taskRef yet. if (!taskList.find(it => { - return it == t.logicalID; + return it == t.taskRef; })) { t.buildTask(); } - taskList.push(t.logicalID); + taskList.push(t.taskRef); } }); @@ -1114,7 +1133,7 @@ function createOrderedPipelineTask(t: TaskBuilder, after: string[], params: Task return { name: t.name, taskRef: { - name: t.logicalID, + name: t.taskRef, }, runAfter: after, params: params, @@ -1124,7 +1143,7 @@ function createOrderedPipelineTask(t: TaskBuilder, after: string[], params: Task return { name: t.name, taskRef: { - name: t.logicalID, + name: t.taskRef, }, params: params, workspaces: ws, diff --git a/test/pipelinebuilder.test.ts b/test/pipelinebuilder.test.ts index c969499..e64761b 100644 --- a/test/pipelinebuilder.test.ts +++ b/test/pipelinebuilder.test.ts @@ -290,12 +290,14 @@ class MyTestChartWithSimilarTasks extends Chart { .withValue(fromPipelineParam(pipelineParam)); const myTask = new TaskBuilder(this, 'fetch-source') + .referencingTask('fetch-source') .withName('git-clone') .withWorkspace(myWorkspace) .withStringParam(urlParam) ; - const myTask2 = new TaskBuilder(this, 'fetch-source') + const myTask2 = new TaskBuilder(this, 'fetch-source-2') + .referencingTask('fetch-source') .withName('git-clone-2') .withWorkspace(myWorkspace) .withStringParam(urlParam); @@ -317,7 +319,8 @@ class MyTestChartWithRunAfter extends Chart { .withName('fetch-source') .specifyRunAfter([]); - const secondTask = new TaskBuilder(this, 'git-clone') + const secondTask = new TaskBuilder(this, 'git-clone-2') + .referencingTask('git-clone') .withName('fetch-again'); const thirdTask = new TaskBuilder(this, 'print-readme')