Skip to content

Commit 5942978

Browse files
authored
fix(pipelines): SelfMutation CodeBuild project not accessible (#24073)
The Pipelines-generated Synth CodeBuild project is already exposed so that users can tweak it or its permissions. Do the same for the SelfMutation CodeBuild project, so that certain users with very specific use cases can stretch CDK Pipelines a little beyond its design goals. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent eda8c72 commit 5942978

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ export class CodePipeline extends PipelineBase {
320320
private _pipeline?: cp.Pipeline;
321321
private artifacts = new ArtifactMap();
322322
private _synthProject?: cb.IProject;
323+
private _selfMutationProject?: cb.IProject;
323324
private readonly selfMutation: boolean;
324325
private readonly useChangeSets: boolean;
325326
private _myCxAsmRoot?: string;
@@ -365,6 +366,22 @@ export class CodePipeline extends PipelineBase {
365366
return this._synthProject;
366367
}
367368

369+
/**
370+
* The CodeBuild project that performs the SelfMutation
371+
*
372+
* Will throw an error if this is accessed before `buildPipeline()`
373+
* is called.
374+
*
375+
* May return no value if `selfMutation` was set to `false` when
376+
* the `CodePipeline` was defined.
377+
*/
378+
public get selfMutationProject(): cb.IProject | undefined {
379+
if (!this._pipeline) {
380+
throw new Error('Call pipeline.buildPipeline() before reading this property');
381+
}
382+
return this._selfMutationProject;
383+
}
384+
368385
/**
369386
* The CodePipeline pipeline that deploys the CDK app
370387
*
@@ -527,6 +544,9 @@ export class CodePipeline extends PipelineBase {
527544
if (nodeType === CodeBuildProjectType.SYNTH) {
528545
this._synthProject = result.project;
529546
}
547+
if (nodeType === CodeBuildProjectType.SELF_MUTATE) {
548+
this._selfMutationProject = result.project;
549+
}
530550
}
531551

532552
if (node.data?.type === 'step' && node.data.step.primaryOutput?.primaryOutput && !this._fallbackArtifact) {

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Stack } from '@aws-cdk/core';
88
import { Construct } from 'constructs';
99
import * as cdkp from '../../lib';
1010
import { CodePipeline } from '../../lib';
11-
import { PIPELINE_ENV, TestApp, ModernTestGitHubNpmPipeline, FileAssetApp, TwoStackApp } from '../testhelpers';
11+
import { PIPELINE_ENV, TestApp, ModernTestGitHubNpmPipeline, FileAssetApp, TwoStackApp, StageWithStackOutput } from '../testhelpers';
1212

1313
let app: TestApp;
1414

@@ -426,6 +426,34 @@ test('synths with change set approvers', () => {
426426
});
427427
});
428428

429+
test('selfMutationProject can be accessed after buildPipeline', () => {
430+
// GIVEN
431+
const pipelineStack = new cdk.Stack(app, 'PipelineStack', { env: PIPELINE_ENV });
432+
const pipeline = new ModernTestGitHubNpmPipeline(pipelineStack, 'Cdk');
433+
pipeline.addStage(new StageWithStackOutput(pipelineStack, 'Stage'));
434+
435+
// WHEN
436+
pipeline.buildPipeline();
437+
438+
// THEN
439+
expect(pipeline.selfMutationProject).toBeTruthy();
440+
});
441+
442+
test('selfMutationProject is undefined if switched off', () => {
443+
// GIVEN
444+
const pipelineStack = new cdk.Stack(app, 'PipelineStack', { env: PIPELINE_ENV });
445+
const pipeline = new ModernTestGitHubNpmPipeline(pipelineStack, 'Cdk', {
446+
selfMutation: false,
447+
});
448+
pipeline.addStage(new StageWithStackOutput(pipelineStack, 'Stage'));
449+
450+
// WHEN
451+
pipeline.buildPipeline();
452+
453+
// THEN
454+
expect(pipeline.selfMutationProject).toBeUndefined();
455+
});
456+
429457
interface ReuseCodePipelineStackProps extends cdk.StackProps {
430458
reuseCrossRegionSupportStacks?: boolean;
431459
}

0 commit comments

Comments
 (0)