Skip to content

Commit

Permalink
Add Components stack to CI Pipeline (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
pziemkowski authored Jul 20, 2020
1 parent ff2ed28 commit 8893fcf
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 1 deletion.
3 changes: 3 additions & 0 deletions infra/cdk/lib/stacks/env/ci/ciBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ export class BackendCiConfig extends ServiceCiConfig {
const apiDeployProject = this.createApiDeployProject(props);
props.deployStage.addAction(this.createDeployAction('api', {
project: apiDeployProject,
runOrder: 2,
}, props));

const adminPanelDeployProject = this.createAdminPanelDeployProject(props);
props.deployStage.addAction(this.createDeployAction('admin', {
project: adminPanelDeployProject,
runOrder: 2,
}, props));

const migrationsDeployProject = this.createMigrationsDeployProject(props);
props.deployStage.addAction(this.createDeployAction('migrations', {
project: migrationsDeployProject,
runOrder: 2,
}, props));
}

Expand Down
78 changes: 78 additions & 0 deletions infra/cdk/lib/stacks/env/ci/ciComponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {Construct, Stack} from "@aws-cdk/core";
import {BuildSpec, Cache, LocalCacheMode, Project} from "@aws-cdk/aws-codebuild";
import {CodeBuildAction, CodeBuildActionProps} from "@aws-cdk/aws-codepipeline-actions";
import {Artifact, IStage} from "@aws-cdk/aws-codepipeline";
import {Effect, PolicyStatement} from "@aws-cdk/aws-iam";

import {EnvConstructProps} from "../../../types";
import {ServiceCiConfig} from "../../../patterns/serviceCiConfig";

interface ComponentsCiConfigProps extends EnvConstructProps {
inputArtifact: Artifact;
buildStage: IStage;
deployStage: IStage;
}

export class ComponentsCiConfig extends ServiceCiConfig {
constructor(scope: Construct, id: string, props: ComponentsCiConfigProps) {
super(scope, id, {envSettings: props.envSettings});

props.deployStage.addAction(this.createDeployAction({
project: this.createDeployProject(props),
}, props));
}

private createDeployAction(actionProps: Partial<CodeBuildActionProps>, props: ComponentsCiConfigProps) {
return new CodeBuildAction(<CodeBuildActionProps>{
...actionProps,
project: actionProps.project,
actionName: `${props.envSettings.projectEnvName}-deploy-components`,
input: props.inputArtifact,
runOrder: 1,
});
}

private createDeployProject(props: ComponentsCiConfigProps) {
const stack = Stack.of(this);
const project = new Project(this, "ComponentsDeployProject", {
projectName: `${props.envSettings.projectEnvName}-deploy-components`,
buildSpec: BuildSpec.fromObject({
version: '0.2',
phases: {
pre_build: {commands: ['make -C infra/cdk install']},
build: {commands: ['make -C infra/cdk deploy-components']}
},
cache: {
paths: [...this.defaultCachePaths],
},
}),
environmentVariables: {...this.defaultEnvVariables},
cache: Cache.local(LocalCacheMode.CUSTOM),
});

project.addToRolePolicy(new PolicyStatement({
effect: Effect.ALLOW,
actions: [
'cloudformation:*',
],
resources: [
`arn:aws:cloudformation:${stack.region}:${stack.account}:stack/CDKToolkit/*`,
`arn:aws:cloudformation:${stack.region}:${stack.account}:stack/${props.envSettings.projectEnvName}-ComponentsStack/*`,
],
}));

project.addToRolePolicy(new PolicyStatement({
effect: Effect.ALLOW,
actions: [
'iam:*',
'logs:*',
's3:*',
'sqs:*',
'events:*',
],
resources: ['*'],
}));

return project;
}
}
8 changes: 8 additions & 0 deletions infra/cdk/lib/stacks/env/ci/ciPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {BackendCiConfig} from "./ciBackend";
import {WebappCiConfig} from "./ciWebApp";
import {ServerlessCiConfig} from "./ciServerless";
import {UploadVersionCiConfig} from "./ciUploadVersion";
import {ComponentsCiConfig} from "./ciComponents";


export interface CiPipelineProps extends EnvConstructProps {
Expand Down Expand Up @@ -38,6 +39,13 @@ export class CiPipeline extends Construct {
const buildStage = this.selectStage(this.buildStageName, pipeline);
const deployStage = this.selectStage(this.deployStageName, pipeline);

new ComponentsCiConfig(this, "ComponentsConfig", {
buildStage,
deployStage,
envSettings: props.envSettings,
inputArtifact: sourceOutputArtifact,
});

new BackendCiConfig(this, "BackendConfig", {
buildStage,
deployStage,
Expand Down
1 change: 1 addition & 0 deletions infra/cdk/lib/stacks/env/ci/ciServerless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class ServerlessCiConfig extends ServiceCiConfig {
props.deployStage.addAction(this.createDeployAction({
project: deployProject,
input: buildArtifact,
runOrder: 2,
}, props));
}

Expand Down
2 changes: 1 addition & 1 deletion infra/cdk/lib/stacks/env/ci/ciUploadVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ export class UploadVersionCiConfig extends ServiceCiConfig {
props.deployStage.addAction(this.createDeployAction({
project: deployProject,
input: props.inputArtifact,
runOrder: 3,
}, props));
}

private createDeployAction(actionProps: Partial<CodeBuildActionProps>, props: UploadVersionCiConfigProps) {
return new CodeBuildAction(<CodeBuildActionProps>{
...actionProps,
actionName: `${props.envSettings.projectEnvName}-upload-version`,
runOrder: 2,
});
}

Expand Down
1 change: 1 addition & 0 deletions infra/cdk/lib/stacks/env/ci/ciWebApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class WebappCiConfig extends ServiceCiConfig {
props.deployStage.addAction(this.createDeployAction({
project: deployProject,
input: buildArtifact,
runOrder: 2,
}, props));
}

Expand Down

0 comments on commit 8893fcf

Please sign in to comment.