Skip to content

Commit

Permalink
fix(pipelines): SelfMutation CodeBuild project not accessible (#24073)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
rix0rrr committed Feb 9, 2023
1 parent eda8c72 commit 5942978
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/@aws-cdk/pipelines/lib/codepipeline/codepipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ export class CodePipeline extends PipelineBase {
private _pipeline?: cp.Pipeline;
private artifacts = new ArtifactMap();
private _synthProject?: cb.IProject;
private _selfMutationProject?: cb.IProject;
private readonly selfMutation: boolean;
private readonly useChangeSets: boolean;
private _myCxAsmRoot?: string;
Expand Down Expand Up @@ -365,6 +366,22 @@ export class CodePipeline extends PipelineBase {
return this._synthProject;
}

/**
* The CodeBuild project that performs the SelfMutation
*
* Will throw an error if this is accessed before `buildPipeline()`
* is called.
*
* May return no value if `selfMutation` was set to `false` when
* the `CodePipeline` was defined.
*/
public get selfMutationProject(): cb.IProject | undefined {
if (!this._pipeline) {
throw new Error('Call pipeline.buildPipeline() before reading this property');
}
return this._selfMutationProject;
}

/**
* The CodePipeline pipeline that deploys the CDK app
*
Expand Down Expand Up @@ -527,6 +544,9 @@ export class CodePipeline extends PipelineBase {
if (nodeType === CodeBuildProjectType.SYNTH) {
this._synthProject = result.project;
}
if (nodeType === CodeBuildProjectType.SELF_MUTATE) {
this._selfMutationProject = result.project;
}
}

if (node.data?.type === 'step' && node.data.step.primaryOutput?.primaryOutput && !this._fallbackArtifact) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import * as cdkp from '../../lib';
import { CodePipeline } from '../../lib';
import { PIPELINE_ENV, TestApp, ModernTestGitHubNpmPipeline, FileAssetApp, TwoStackApp } from '../testhelpers';
import { PIPELINE_ENV, TestApp, ModernTestGitHubNpmPipeline, FileAssetApp, TwoStackApp, StageWithStackOutput } from '../testhelpers';

let app: TestApp;

Expand Down Expand Up @@ -426,6 +426,34 @@ test('synths with change set approvers', () => {
});
});

test('selfMutationProject can be accessed after buildPipeline', () => {
// GIVEN
const pipelineStack = new cdk.Stack(app, 'PipelineStack', { env: PIPELINE_ENV });
const pipeline = new ModernTestGitHubNpmPipeline(pipelineStack, 'Cdk');
pipeline.addStage(new StageWithStackOutput(pipelineStack, 'Stage'));

// WHEN
pipeline.buildPipeline();

// THEN
expect(pipeline.selfMutationProject).toBeTruthy();
});

test('selfMutationProject is undefined if switched off', () => {
// GIVEN
const pipelineStack = new cdk.Stack(app, 'PipelineStack', { env: PIPELINE_ENV });
const pipeline = new ModernTestGitHubNpmPipeline(pipelineStack, 'Cdk', {
selfMutation: false,
});
pipeline.addStage(new StageWithStackOutput(pipelineStack, 'Stage'));

// WHEN
pipeline.buildPipeline();

// THEN
expect(pipeline.selfMutationProject).toBeUndefined();
});

interface ReuseCodePipelineStackProps extends cdk.StackProps {
reuseCrossRegionSupportStacks?: boolean;
}
Expand Down

0 comments on commit 5942978

Please sign in to comment.