-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathupdate-pipeline-action.ts
136 lines (123 loc) · 4.68 KB
/
update-pipeline-action.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import * as codebuild from '@aws-cdk/aws-codebuild';
import * as codepipeline from '@aws-cdk/aws-codepipeline';
import * as cpactions from '@aws-cdk/aws-codepipeline-actions';
import * as events from '@aws-cdk/aws-events';
import * as iam from '@aws-cdk/aws-iam';
import { Construct } from 'constructs';
import { embeddedAsmPath } from '../private/construct-internals';
// v2 - keep this import as a separate section to reduce merge conflict when forward merging with the v2 branch.
// eslint-disable-next-line
import { Construct as CoreConstruct } from '@aws-cdk/core';
/**
* Props for the UpdatePipelineAction
*/
export interface UpdatePipelineActionProps {
/**
* The CodePipeline artifact that holds the Cloud Assembly.
*/
readonly cloudAssemblyInput: codepipeline.Artifact;
/**
* Name of the pipeline stack
*/
readonly pipelineStackName: string;
/**
* Version of CDK CLI to 'npm install'.
*
* @default - Latest version
*/
readonly cdkCliVersion?: string;
/**
* Name of the CodeBuild project
*
* @default - Automatically generated
*/
readonly projectName?: string;
}
/**
* Action to self-mutate the pipeline
*
* Creates a CodeBuild project which will use the CDK CLI
* to deploy the pipeline stack.
*
* You do not need to instantiate this action -- it will automatically
* be added by the pipeline.
*/
export class UpdatePipelineAction extends CoreConstruct implements codepipeline.IAction {
private readonly action: codepipeline.IAction;
constructor(scope: Construct, id: string, props: UpdatePipelineActionProps) {
super(scope, id);
const installSuffix = props.cdkCliVersion ? `@${props.cdkCliVersion}` : '';
const selfMutationProject = new codebuild.PipelineProject(this, 'SelfMutation', {
projectName: props.projectName,
environment: { buildImage: codebuild.LinuxBuildImage.STANDARD_4_0 },
buildSpec: codebuild.BuildSpec.fromObject({
version: '0.2',
phases: {
install: {
commands: `npm install -g aws-cdk${installSuffix}`,
},
build: {
commands: [
// Cloud Assembly is in *current* directory.
`cdk -a ${embeddedAsmPath(scope)} deploy ${props.pipelineStackName} --require-approval=never --verbose`,
],
},
},
}),
});
// allow the self-mutating project permissions to assume the bootstrap Action role
selfMutationProject.addToRolePolicy(new iam.PolicyStatement({
actions: ['sts:AssumeRole'],
resources: ['arn:*:iam::*:role/*-deploy-role-*', 'arn:*:iam::*:role/*-publishing-role-*'],
}));
selfMutationProject.addToRolePolicy(new iam.PolicyStatement({
actions: ['cloudformation:DescribeStacks'],
resources: ['*'], // this is needed to check the status of the bootstrap stack when doing `cdk deploy`
}));
// S3 checks for the presence of the ListBucket permission
selfMutationProject.addToRolePolicy(new iam.PolicyStatement({
actions: ['s3:ListBucket'],
resources: ['*'],
}));
this.action = new cpactions.CodeBuildAction({
actionName: 'SelfMutate',
input: props.cloudAssemblyInput,
project: selfMutationProject,
// Add this purely so that the pipeline will selfupdate if the CLI version changes
environmentVariables: props.cdkCliVersion ? {
CDK_CLI_VERSION: { value: props.cdkCliVersion },
} : undefined,
});
}
/**
* Exists to implement IAction
*/
public bind(scope: CoreConstruct, stage: codepipeline.IStage, options: codepipeline.ActionBindOptions):
codepipeline.ActionConfig {
return this.action.bind(scope, stage, options);
}
/**
* Exists to implement IAction
*/
public onStateChange(name: string, target?: events.IRuleTarget, options?: events.RuleProps): events.Rule {
return this.action.onStateChange(name, target, options);
}
/**
* Exists to implement IAction
*/
public get actionProperties(): codepipeline.ActionProperties {
// FIXME: I have had to make this class a Construct, because:
//
// - It needs access to the Construct tree, because it is going to add a `PipelineProject`.
// - I would have liked to have done that in bind(), however,
// - `actionProperties` (this method) is called BEFORE bind() is called, and by that point I
// don't have the "inner" Action yet to forward the call to.
//
// I've therefore had to construct the inner CodeBuildAction in the constructor, which requires making this
// Action a Construct.
//
// Combined with how non-intuitive it is to make the "StackDeployAction", I feel there is something
// wrong with the Action abstraction here.
return this.action.actionProperties;
}
}