/
update-pipeline-action.ts
186 lines (168 loc) · 6.3 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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 { Stack } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { dockerCredentialsInstallCommands, DockerCredential, DockerCredentialUsage } from '../../docker-credentials';
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
*
* @deprecated This class is part of the old API. Use the API based on the `CodePipeline` class instead
*/
export interface UpdatePipelineActionProps {
/**
* The CodePipeline artifact that holds the Cloud Assembly.
*/
readonly cloudAssemblyInput: codepipeline.Artifact;
/**
* Name of the pipeline stack
*
* @deprecated - Use `pipelineStackHierarchicalId` instead.
* @default - none
*/
readonly pipelineStackName?: string;
/**
* Hierarchical id of the pipeline stack
*/
readonly pipelineStackHierarchicalId: 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;
/**
* Whether the build step should run in privileged mode.
*
* @default - false
*/
readonly privileged?: boolean
/**
* Docker registries and associated credentials necessary during the pipeline
* self-update stage.
*
* @default []
*/
readonly dockerCredentials?: DockerCredential[];
/**
* Custom BuildSpec that is merged with generated one
*
* @default - none
*/
readonly buildSpec?: codebuild.BuildSpec;
}
/**
* 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.
*
* @deprecated This class is part of the old API. Use the API based on the `CodePipeline` class instead
*/
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 stackIdentifier = props.pipelineStackHierarchicalId ?? props.pipelineStackName;
const buildSpec = codebuild.BuildSpec.fromObject({
version: '0.2',
phases: {
install: {
commands: [
`npm install -g aws-cdk${installSuffix}`,
...dockerCredentialsInstallCommands(DockerCredentialUsage.SELF_UPDATE, props.dockerCredentials),
],
},
build: {
commands: [
// Cloud Assembly is in *current* directory.
`cdk -a ${embeddedAsmPath(scope)} deploy ${stackIdentifier} --require-approval=never --verbose`,
],
},
},
});
const selfMutationProject = new codebuild.PipelineProject(this, 'SelfMutation', {
projectName: props.projectName,
environment: {
buildImage: codebuild.LinuxBuildImage.STANDARD_5_0,
privileged: props.privileged ?? false,
},
buildSpec: props.buildSpec ? codebuild.mergeBuildSpecs(props.buildSpec, buildSpec) : buildSpec,
});
// allow the self-mutating project permissions to assume the bootstrap Action role
selfMutationProject.addToRolePolicy(new iam.PolicyStatement({
actions: ['sts:AssumeRole'],
resources: [`arn:*:iam::${Stack.of(this).account}:role/*`],
conditions: {
'ForAnyValue:StringEquals': {
'iam:ResourceTag/aws-cdk:bootstrap-role': ['image-publishing', 'file-publishing', 'deploy'],
},
},
}));
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: ['*'],
}));
(props.dockerCredentials ?? []).forEach(reg => reg.grantRead(selfMutationProject, DockerCredentialUsage.SELF_UPDATE));
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;
}
}