Skip to content

Commit 9cd639b

Browse files
authored
fix(pipelines): cannot configure actionName for all sources (#24027)
Add the ability for GitHub, CodeStar Connections, CodeCommit source types to configure the Action Name that is added to the pipeline. This is necessary to add the same GitHub repository to the same pipeline twice with a different branch each time. By default, it would pick the same default name for each, which would then conflict. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent be4f971 commit 9cd639b

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

packages/@aws-cdk/pipelines/lib/codepipeline/codepipeline-source.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ export interface GitHubSourceOptions {
236236
*/
237237
readonly trigger?: GitHubTrigger;
238238

239+
/**
240+
* The action name used for this source in the CodePipeline
241+
*
242+
* @default - The repository string
243+
*/
244+
readonly actionName?: string;
239245
}
240246

241247
/**
@@ -262,7 +268,7 @@ class GitHubSource extends CodePipelineSource {
262268
protected getAction(output: Artifact, actionName: string, runOrder: number, variablesNamespace?: string) {
263269
return new cp_actions.GitHubSourceAction({
264270
output,
265-
actionName,
271+
actionName: this.props.actionName ?? actionName,
266272
runOrder,
267273
oauthToken: this.authentication,
268274
owner: this.owner,
@@ -379,7 +385,6 @@ export interface ConnectionSourceOptions {
379385
*/
380386
readonly connectionArn: string;
381387

382-
383388
// long URL in @see
384389
/**
385390
* If this is set, the next CodeBuild job clones the repository (instead of CodePipeline downloading the files).
@@ -403,6 +408,13 @@ export interface ConnectionSourceOptions {
403408
* @see https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CodestarConnectionSource.html
404409
*/
405410
readonly triggerOnPush?: boolean;
411+
412+
/**
413+
* The action name used for this source in the CodePipeline
414+
*
415+
* @default - The repository string
416+
*/
417+
readonly actionName?: string;
406418
}
407419

408420
class CodeStarConnectionSource extends CodePipelineSource {
@@ -424,7 +436,7 @@ class CodeStarConnectionSource extends CodePipelineSource {
424436
protected getAction(output: Artifact, actionName: string, runOrder: number, variablesNamespace?: string) {
425437
return new cp_actions.CodeStarConnectionsSourceAction({
426438
output,
427-
actionName,
439+
actionName: this.props.actionName ?? actionName,
428440
runOrder,
429441
connectionArn: this.props.connectionArn,
430442
owner: this.owner,
@@ -468,6 +480,13 @@ export interface CodeCommitSourceOptions {
468480
* @see https://docs.aws.amazon.com/codepipeline/latest/userguide/action-reference-CodeCommit.html
469481
*/
470482
readonly codeBuildCloneOutput?: boolean;
483+
484+
/**
485+
* The action name used for this source in the CodePipeline
486+
*
487+
* @default - The repository name
488+
*/
489+
readonly actionName?: string;
471490
}
472491

473492
class CodeCommitSource extends CodePipelineSource {
@@ -483,7 +502,7 @@ class CodeCommitSource extends CodePipelineSource {
483502
return new cp_actions.CodeCommitSourceAction({
484503
output,
485504
// Guaranteed to be okay as action name
486-
actionName: this.repository.repositoryName,
505+
actionName: this.props.actionName ?? this.repository.repositoryName,
487506
runOrder,
488507
branch: this.branch,
489508
trigger: this.props.trigger,

packages/@aws-cdk/pipelines/test/codepipeline/codepipeline-sources.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,54 @@ test('pass role to s3 codepipeline source', () => {
288288
}]),
289289
});
290290
});
291+
292+
type SourceFactory = (stack: Stack) => cdkp.CodePipelineSource;
293+
294+
test.each([
295+
['CodeCommit', (stack) => {
296+
const repo = new ccommit.Repository(stack, 'Repo', {
297+
repositoryName: 'MyRepo',
298+
});
299+
return cdkp.CodePipelineSource.codeCommit(repo, 'main', {
300+
actionName: 'ConfiguredName',
301+
});
302+
}],
303+
['S3', (stack) => {
304+
const bucket = new s3.Bucket(stack, 'Bucket');
305+
return cdkp.CodePipelineSource.s3(bucket, 'thefile.zip', {
306+
actionName: 'ConfiguredName',
307+
});
308+
}],
309+
['ECR', (stack) => {
310+
const repository = new ecr.Repository(stack, 'Repository', { repositoryName: 'namespace/repo' });
311+
return cdkp.CodePipelineSource.ecr(repository, {
312+
actionName: 'ConfiguredName',
313+
});
314+
}],
315+
['GitHub', () => {
316+
return cdkp.CodePipelineSource.gitHub('owner/repo', 'main', {
317+
actionName: 'ConfiguredName',
318+
});
319+
}],
320+
['CodeStar', () => {
321+
return cdkp.CodePipelineSource.connection('owner/repo', 'main', {
322+
connectionArn: 'arn:aws:codestar-connections:us-west-2:123456789012:connection/39e4c34d-e13a-4e94-a886',
323+
actionName: 'ConfiguredName',
324+
});
325+
}],
326+
] as Array<[string, SourceFactory]>)('can configure actionName for %s', (_name: string, fac: SourceFactory) => {
327+
new ModernTestGitHubNpmPipeline(pipelineStack, 'Pipeline', {
328+
input: fac(pipelineStack),
329+
});
330+
331+
Template.fromStack(pipelineStack).hasResourceProperties('AWS::CodePipeline::Pipeline', {
332+
Stages: Match.arrayWith([{
333+
Name: 'Source',
334+
Actions: [
335+
Match.objectLike({
336+
Name: 'ConfiguredName',
337+
}),
338+
],
339+
}]),
340+
});
341+
});

0 commit comments

Comments
 (0)