Skip to content

Commit

Permalink
feat(pipelines): temporarily disable self-mutation (aws#10466)
Browse files Browse the repository at this point in the history
The self-mutation feature of the `CdkPipeline` might at times get in the way of the pipeline development workflow. Each change to the pipeline must be pushed to git, otherwise, after the pipeline was updated using `cdk deploy`, it will automatically revert to the state found in git.

To make the development more convenient, the self-mutation feature can be turned off temporarily, by passing `selfMutating: false` property, example:

```ts
const pipeline = new CdkPipeline(this, 'Pipeline', {
  selfMutating: false,
  ...
});
```

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
tomas-mazak committed Oct 19, 2020
1 parent 2f3a167 commit 8ffabb4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
18 changes: 18 additions & 0 deletions packages/@aws-cdk/pipelines/README.md
Expand Up @@ -544,6 +544,24 @@ class MyPipelineStack extends Stack {
}
```

### Developing the pipeline

The self-mutation feature of the `CdkPipeline` might at times get in the way
of the pipeline development workflow. Each change to the pipeline must be pushed
to git, otherwise, after the pipeline was updated using `cdk deploy`, it will
automatically revert to the state found in git.

To make the development more convenient, the self-mutation feature can be turned
off temporarily, by passing `selfMutating: false` property, example:

```ts
const pipeline = new CdkPipeline(this, 'Pipeline', {
selfMutating: false,
...
});
```


## CDK Environment Bootstrapping

An *environment* is an *(account, region)* pair where you want to deploy a
Expand Down
34 changes: 25 additions & 9 deletions packages/@aws-cdk/pipelines/lib/pipeline.ts
Expand Up @@ -105,6 +105,20 @@ export interface CdkPipelineProps {
* @default - All private subnets.
*/
readonly subnetSelection?: ec2.SubnetSelection;

/**
* Whether the pipeline will update itself
*
* This needs to be set to `true` to allow the pipeline to reconfigure
* itself when assets or stages are being added to it, and `true` is the
* recommended setting.
*
* You can temporarily set this to `false` while you are iterating
* on the pipeline itself and prefer to deploy changes using `cdk deploy`.
*
* @default true
*/
readonly selfMutating?: boolean;
}

/**
Expand Down Expand Up @@ -178,15 +192,17 @@ export class CdkPipeline extends CoreConstruct {
});
}

this._pipeline.addStage({
stageName: 'UpdatePipeline',
actions: [new UpdatePipelineAction(this, 'UpdatePipeline', {
cloudAssemblyInput: this._cloudAssemblyArtifact,
pipelineStackName: pipelineStack.stackName,
cdkCliVersion: props.cdkCliVersion,
projectName: maybeSuffix(props.pipelineName, '-selfupdate'),
})],
});
if (props.selfMutating ?? true) {
this._pipeline.addStage({
stageName: 'UpdatePipeline',
actions: [new UpdatePipelineAction(this, 'UpdatePipeline', {
cloudAssemblyInput: this._cloudAssemblyArtifact,
pipelineStackName: pipelineStack.stackName,
cdkCliVersion: props.cdkCliVersion,
projectName: maybeSuffix(props.pipelineName, '-selfupdate'),
})],
});
}

this._assets = new AssetPublishing(this, 'Assets', {
cloudAssemblyInput: this._cloudAssemblyArtifact,
Expand Down
28 changes: 27 additions & 1 deletion packages/@aws-cdk/pipelines/test/pipeline.test.ts
@@ -1,6 +1,15 @@
import * as fs from 'fs';
import * as path from 'path';
import { anything, arrayWith, Capture, deepObjectLike, encodedJson, objectLike, stringLike } from '@aws-cdk/assert';
import {
anything,
arrayWith,
Capture,
deepObjectLike,
encodedJson,
notMatching,
objectLike,
stringLike,
} from '@aws-cdk/assert';
import '@aws-cdk/assert/jest';
import * as cp from '@aws-cdk/aws-codepipeline';
import * as cpa from '@aws-cdk/aws-codepipeline-actions';
Expand Down Expand Up @@ -332,6 +341,23 @@ test('selfmutation stage correctly identifies nested assembly of pipeline stack'
});
});

test('selfmutation feature can be turned off', () => {
const stack = new Stack();
const cloudAssemblyArtifact = new cp.Artifact();
// WHEN
new TestGitHubNpmPipeline(stack, 'Cdk', {
cloudAssemblyArtifact,
selfMutating: false,
});
// THEN
expect(stack).toHaveResourceLike('AWS::CodePipeline::Pipeline', {
Stages: notMatching(arrayWith({
Name: 'UpdatePipeline',
Actions: anything(),
})),
});
});

test('overridden stack names are respected', () => {
// WHEN
pipeline.addApplicationStage(new OneStackAppWithCustomName(app, 'App1'));
Expand Down

0 comments on commit 8ffabb4

Please sign in to comment.