diff --git a/packages/aws-cdk-lib/aws-codepipeline/lib/pipeline.ts b/packages/aws-cdk-lib/aws-codepipeline/lib/pipeline.ts index 7099b50f7bd97..b8a0e732b18e3 100644 --- a/packages/aws-cdk-lib/aws-codepipeline/lib/pipeline.ts +++ b/packages/aws-cdk-lib/aws-codepipeline/lib/pipeline.ts @@ -596,6 +596,13 @@ export class Pipeline extends PipelineBase { */ public addTrigger(props: TriggerProps): Trigger { const trigger = new Trigger(props); + const actionName = props.gitConfiguration?.sourceAction.actionProperties.actionName; + + // check for duplicate source actions for triggers + if (actionName !== undefined && this.triggers.find(t => t.sourceAction?.actionProperties.actionName === actionName)) { + throw new Error(`Trigger with duplicate source action '${actionName}' added to the Pipeline`); + } + this.triggers.push(trigger); return trigger; } diff --git a/packages/aws-cdk-lib/aws-codepipeline/lib/trigger.ts b/packages/aws-cdk-lib/aws-codepipeline/lib/trigger.ts index 0809d29f18b3a..8929d8f0474cb 100644 --- a/packages/aws-cdk-lib/aws-codepipeline/lib/trigger.ts +++ b/packages/aws-cdk-lib/aws-codepipeline/lib/trigger.ts @@ -93,7 +93,10 @@ export interface TriggerProps { * Trigger. */ export class Trigger { + public readonly sourceAction: IAction | undefined; + constructor(private readonly props: TriggerProps) { + this.sourceAction = props.gitConfiguration?.sourceAction; this.validate(); } diff --git a/packages/aws-cdk-lib/aws-codepipeline/test/triggers.test.ts b/packages/aws-cdk-lib/aws-codepipeline/test/triggers.test.ts index 670a31003664e..1970bd42f556c 100644 --- a/packages/aws-cdk-lib/aws-codepipeline/test/triggers.test.ts +++ b/packages/aws-cdk-lib/aws-codepipeline/test/triggers.test.ts @@ -337,6 +337,30 @@ describe('triggers', () => { }).toThrow(/provider for actionProperties in sourceAction with name 'FakeSource' must be 'CodeStarSourceConnection', got 'Fake'/); }); + test('throw if source action with duplicate action name added to the Pipeline', () => { + const pipeline = new codepipeline.Pipeline(stack, 'Pipeline', { + pipelineType: codepipeline.PipelineType.V2, + triggers: [{ + providerType: codepipeline.ProviderType.CODE_STAR_SOURCE_CONNECTION, + gitConfiguration: { + sourceAction, + pushFilter: [{ + excludedTags: ['exclude1', 'exclude2'], + includedTags: ['include1', 'include2'], + }], + }, + }], + }); + expect(() => { + pipeline.addTrigger({ + providerType: codepipeline.ProviderType.CODE_STAR_SOURCE_CONNECTION, + gitConfiguration: { + sourceAction, + }, + }); + }).toThrow(/Trigger with duplicate source action 'CodeStarConnectionsSourceAction' added to the Pipeline/); + }); + test('throw if triggers are specified when pipelineType is not set to V2', () => { const pipeline = new codepipeline.Pipeline(stack, 'Pipeline', { pipelineType: codepipeline.PipelineType.V1,