@flit/cdk-pipeline 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,18 +1,184 @@
1
- # @flit/cdk
1
+ # @flit/cdk-pipeline
2
2
 
3
- This package exposes some useful CDK constructs to improve deployment workflows.
3
+ This library exposes a highly customizable and extensible L3 pipeline construct intended as an alternative to the CDK native L3 [CodePipeline](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.pipelines.CodePipeline.html) construct which has some inherent limitations in capability and extensibility.
4
+
5
+ This library provides the tools and documentation to get your own pipeline up and running and build your own custom segments.
4
6
 
5
7
  _**Warning:** This package is EXPERIMENTAL and might undergo breaking changes_
6
8
 
9
+ ## Table of contents
10
+
11
+ - [Table of contents](#table-of-contents)
12
+ - [Homepage](https://p-mercury.github.io/jumper-de/modules/_flit_cdk_pipeline)
13
+ - [Usage](#usage)
14
+ - [1. Installation](#1-installation)
15
+ - [2. Basics](#1-basics)
16
+ - [3. Multiple stacks](#3-multiple-stacks)
17
+ - [4. Passing assets](#4-passing-assets)
18
+ - [4. Build your own segment](#3-build-your-own-segment)
19
+
7
20
  ## Usage
8
21
 
9
- ### Installation
22
+ ### 1. Installation
10
23
 
11
24
  ```bash
12
- npm i -g @flit/cdk
25
+ npm i -g @flit/cdk-pipeline
26
+ ```
27
+
28
+ ### 2. Basics
29
+
30
+ The snippet bellow is a basic example of a pipeline which will run whenever a change is detected in a GitHub repository and which will update itself and deploy and update a user defined stack. This example demonstrates the three basic elements that make up a pipeline:
31
+
32
+ #### - Pipeline
33
+
34
+ The Pipeline construct will create a CloudFormation Stack which contains the pipeline and all of the required peripheral resources to make it work.
35
+
36
+ #### - Segment
37
+
38
+ A Segment is simply a pre-configured set of pipeline actions which together represent a commonly used CI/CD pattern, like for example building and deploying a stack.
39
+
40
+ To build properly, a Pipeline requires at least one SourceSegment, exactly one PipelineSegment and at least one other segment.
41
+
42
+ #### - Artifact
43
+
44
+ An Artifact represents a pipeline artifact which can be used to pass information between stages. Every asset needs to be the output of a Segment and can be consumed by any segments that need that output.
45
+
46
+ ```typescript
47
+ import { App, SecretValue, Stack } from "aws-cdk-lib";
48
+ import {
49
+ Pipeline,
50
+ GitHubSourceSegment,
51
+ PipelineSegment,
52
+ StackSegment,
53
+ Artifact,
54
+ } from "@flit/cdk-pipeline";
55
+
56
+ const APP = new App();
57
+
58
+ const sourceArtifact = new Artifact();
59
+
60
+ new Pipeline(APP, "Pipeline", {
61
+ rootDir: "./",
62
+ segments: [
63
+ new GitHubSourceSegment({
64
+ oauthToken: SecretValue.secretsManager("github-access-token"),
65
+ output: sourceArtifact,
66
+ owner: "owner-name",
67
+ repo: "repo-name",
68
+ branch: "branch-name",
69
+ }),
70
+ new PipelineSegment({
71
+ input: sourceArtifact,
72
+ command: "cdk synth Pipeline --strict --exclusively",
73
+ }),
74
+ new StackSegment({
75
+ stack: new Stack(APP, "Stack1"),
76
+ input: sourceArtifact,
77
+ command: "cdk synth Stack1 --strict --exclusively",
78
+ }),
79
+ ],
80
+ });
81
+ ```
82
+
83
+ The above code will produce the following pipeline
84
+
85
+ <div style="width: 300px; height: 340px; overflow-x: hidden; overflow-y: scroll;"><img src="media://pipeline-ouput-example.png" alt="alt text" width="100%"></div>
86
+
87
+ ### 3. Multiple stacks
88
+
89
+ To add another stack to the pipeline you simply add another StackSegment with a new stack instance and the pipeline will handle the rest.
90
+
91
+ ```typescript
92
+ import { App, SecretValue, Stack } from "aws-cdk-lib";
93
+ import {
94
+ Pipeline,
95
+ GitHubSourceSegment,
96
+ PipelineSegment,
97
+ StackSegment,
98
+ Artifact,
99
+ } from "@flit/cdk-pipeline";
100
+
101
+ const APP = new App();
102
+
103
+ const sourceArtifact = new Artifact();
104
+
105
+ new Pipeline(APP, "Pipeline", {
106
+ rootDir: "./",
107
+ segments: [
108
+ new GitHubSourceSegment({
109
+ oauthToken: SecretValue.secretsManager("github-access-token"),
110
+ output: sourceArtifact,
111
+ owner: "owner-name",
112
+ repo: "repo-name",
113
+ branch: "branch-name",
114
+ }),
115
+ new PipelineSegment({
116
+ input: sourceArtifact,
117
+ command: "cdk synth Pipeline --strict --exclusively",
118
+ }),
119
+ new StackSegment({
120
+ stack: new Stack(APP, "Stack1"),
121
+ input: sourceArtifact,
122
+ command: "cdk synth Stack1 --strict --exclusively",
123
+ }),
124
+ new StackSegment({
125
+ stack: new Stack(APP, "Stack2"),
126
+ input: sourceArtifact,
127
+ command: "cdk synth Stack2 --strict --exclusively",
128
+ }),
129
+ ],
130
+ });
131
+ ```
132
+
133
+ ### 3. Passing assets
134
+
135
+ If a segment requires the output artifact of a previous segment it you can simply add an output artifact to the previous stage and pass it as additional input to another segment
136
+
137
+ ```typescript
138
+ import { App, SecretValue, Stack } from "aws-cdk-lib";
139
+ import {
140
+ Pipeline,
141
+ GitHubSourceSegment,
142
+ PipelineSegment,
143
+ StackSegment,
144
+ Artifact,
145
+ } from "@flit/cdk-pipeline";
146
+
147
+ const APP = new App();
148
+
149
+ const sourceArtifact = new Artifact();
150
+ const stack1Artifact = new Artifact();
151
+
152
+ new Pipeline(APP, "Pipeline", {
153
+ rootDir: "./",
154
+ segments: [
155
+ new GitHubSourceSegment({
156
+ oauthToken: SecretValue.secretsManager("jumper-de-github-access-tokens"),
157
+ output: sourceArtifact,
158
+ owner: "p-mercury",
159
+ repo: "jumper-de",
160
+ branch: "main",
161
+ }),
162
+ new PipelineSegment({
163
+ input: sourceArtifact,
164
+ command: "cdk synth Pipeline --strict --exclusively",
165
+ }),
166
+ new StackSegment({
167
+ stack: new Stack(APP, "Stack1"),
168
+ input: sourceArtifact,
169
+ output: stack1Artifact,
170
+ command: "cdk synth Stack1 --strict --exclusively",
171
+ }),
172
+ new StackSegment({
173
+ stack: new Stack(APP, "Stack2"),
174
+ input: [sourceArtifact, stack1Artifact],
175
+ command: "cdk synth Stack2 --strict --exclusively",
176
+ }),
177
+ ],
178
+ });
13
179
  ```
14
180
 
15
- ### Pipeline
181
+ ### 4. Build your own segment
16
182
 
17
183
  An alternative L3 construct which fixes some inherit limitations of the CDK native CodePipeline L3 construct.
18
184
 
package/package.json CHANGED
@@ -1,14 +1,18 @@
1
1
  {
2
2
  "name": "@flit/cdk-pipeline",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "license": "Apache-2.0",
5
5
  "description": "Jumper Systems' cdk library",
6
- "homepage": "https://github.com/p-mercury/jumper-de",
6
+ "homepage": "https://p-mercury.github.io/jumper-de/modules/_flit_cdk_pipeline",
7
7
  "keywords": [
8
8
  "aws",
9
9
  "awscdk",
10
10
  "codepipeline"
11
11
  ],
12
+ "author": {
13
+ "name": "Luis Vierroth",
14
+ "email": "luis@jumper.de"
15
+ },
12
16
  "repository": {
13
17
  "type": "git",
14
18
  "url": "https://github.com/p-mercury/jumper-de.git"
@@ -16,28 +20,67 @@
16
20
  "bugs": "https://github.com/p-mercury/jumper-de/issues",
17
21
  "main": "./dist/index.js",
18
22
  "types": "./dist/index.d.ts",
19
- "files": [
20
- "dist"
21
- ],
23
+ "typedoc": {
24
+ "entryPoint": "./src/index.ts"
25
+ },
22
26
  "scripts": {
23
- "build": "npm run prepack",
24
- "prepack": "rm -rf ./dist && tsc -b"
27
+ "build": "jsii",
28
+ "prepack": "jsii"
25
29
  },
26
30
  "publishConfig": {
27
31
  "access": "public"
28
32
  },
29
- "dependencies": {
30
- "@aws-appsync/utils": "^1.1.0",
31
- "@aws-lambda-powertools/tracer": "^1.7.0",
32
- "@types/jest": "29.5.0",
33
- "@types/node": "18.15.5",
34
- "aws-cdk": "2.69.0",
35
- "aws-cdk-lib": "2.69.0",
36
- "cdk-aws-lambda-powertools-layer": "^3.3.0",
37
- "constructs": "10.1.285",
33
+ "dependencies": {},
34
+ "devDependencies": {
35
+ "@types/node": "18.15.10",
36
+ "path": "^0.12.7",
37
+ "aws-cdk": "2.70.0",
38
+ "cdk-aws-lambda-powertools-layer": "3.3.0",
38
39
  "jest": "29.5.0",
40
+ "jsii": "5.0.1",
41
+ "jsii-pacmak": "1.79.0",
39
42
  "ts-jest": "29.0.5",
40
43
  "ts-node": "10.9.1",
41
- "typescript": "5.0.2"
42
- }
44
+ "typescript": "5.0.2",
45
+ "aws-cdk-lib": "2.70.0",
46
+ "constructs": "10.1.293"
47
+ },
48
+ "peerDependencies": {
49
+ "aws-cdk-lib": "2.70.0",
50
+ "constructs": "10.1.293"
51
+ },
52
+ "bundledDependencies": [
53
+ "path"
54
+ ],
55
+ "stability": "stable",
56
+ "jsii": {
57
+ "outdir": "./dist",
58
+ "tsc": {
59
+ "rootDir": "./src",
60
+ "outDir": "./dist"
61
+ },
62
+ "targets": {
63
+ "java": {
64
+ "package": "software.amazon.awscdk.flit.pipeline",
65
+ "maven": {
66
+ "groupId": "software.amazon.awscdk",
67
+ "artifactId": "flit.pipeline"
68
+ }
69
+ },
70
+ "dotnet": {
71
+ "namespace": "Amazon.CDK.Flit.Pipeline",
72
+ "packageId": "Amazon.CDK.Flit.Pipeline",
73
+ "iconUrl": "https://raw.githubusercontent.com/aws/aws-cdk/main/logo/default-256-dark.png"
74
+ },
75
+ "python": {
76
+ "distName": "aws-cdk.flit.pipeline",
77
+ "module": "aws_cdk.flit.pipeline",
78
+ "classifiers": [
79
+ "Framework :: AWS CDK",
80
+ "Framework :: AWS CDK :: 2"
81
+ ]
82
+ }
83
+ }
84
+ },
85
+ "bundleDependencies": []
43
86
  }
@@ -0,0 +1,24 @@
1
+ import { Artifact as AwsArtifact } from "aws-cdk-lib/aws-codepipeline";
2
+
3
+ import { Segment } from "./segment";
4
+
5
+ export class Artifact extends AwsArtifact {
6
+ private producer?: Segment;
7
+ private consumers: Segment[] = [];
8
+ constructor() {
9
+ super();
10
+ }
11
+ produce(producer: Segment) {
12
+ if (this.producer) throw new Error("Artifact is already produced");
13
+ this.producer = producer;
14
+ }
15
+ consume(producer: Segment) {
16
+ this.consumers.push(producer);
17
+ }
18
+ obtainProducer(): Segment | undefined {
19
+ return this.producer;
20
+ }
21
+ obtainConsumers(): Segment[] {
22
+ return this.consumers;
23
+ }
24
+ }
@@ -1,6 +1,7 @@
1
- export * from "./pipeline";
2
1
  export * from "./artifact";
3
- export * from "./source-segment";
4
2
  export * from "./pipeline-segment";
5
- export * from "./stack-segment";
3
+ export * from "./pipeline";
6
4
  export * from "./publish-assets-action";
5
+ export * from "./segment";
6
+ export * from "./source-segment";
7
+ export * from "./stack-segment";
@@ -0,0 +1,68 @@
1
+ import { BuildEnvironmentVariable } from "aws-cdk-lib/aws-codebuild";
2
+
3
+ import { Artifact } from "./artifact";
4
+ import { Segment, SegmentConstructed } from "./segment";
5
+ import { StackSegmentConstructed } from "./stack-segment";
6
+ import { Pipeline } from "./pipeline";
7
+
8
+ export interface PipelineSegmentProps {
9
+ /**
10
+ * The input arfifact for the build stage.
11
+ */
12
+ readonly input: Artifact | Artifact[];
13
+ /**
14
+ * The command(s) to build the stack.
15
+ * @example "cdk synth StackName --strict --exclusively"
16
+ */
17
+ readonly command: string | string[];
18
+ /**
19
+ * The environmental variables for the build stage.
20
+ */
21
+ readonly environmentVariables?: { [key: string]: BuildEnvironmentVariable };
22
+ /**
23
+ * The name of the stack to apply this action to.
24
+ * @deafult The name of the given stack.
25
+ */
26
+ readonly stackName?: string;
27
+ /**
28
+ * The AWS account this Action is supposed to operate in.
29
+ */
30
+ readonly account?: string;
31
+ /**
32
+ * The AWS region the given Action resides in.
33
+ */
34
+ readonly region?: string;
35
+
36
+ /**
37
+ * The artifact to hold the stack deployment output file.
38
+ * @default no output artifact
39
+ */
40
+ readonly output?: Artifact;
41
+ /**
42
+ * Does this stage require manual approval of the change set?
43
+ * @default false
44
+ */
45
+ readonly manualApproval?: Boolean;
46
+ }
47
+
48
+ /**
49
+ * @category Segments
50
+ */
51
+ export class PipelineSegment extends Segment {
52
+ readonly isPipeline: boolean = true;
53
+ readonly props: PipelineSegmentProps;
54
+
55
+ constructor(props: PipelineSegmentProps) {
56
+ super(props);
57
+ this.props = props;
58
+ }
59
+
60
+ construct(scope: Pipeline): SegmentConstructed {
61
+ return new StackSegmentConstructed(scope, `Deploy${scope.stackName}`, {
62
+ ...this.props,
63
+ stack: scope,
64
+ input: this.inputs[0],
65
+ extraInputs: this.inputs.slice(1),
66
+ });
67
+ }
68
+ }
@@ -0,0 +1,94 @@
1
+ import { App, Stack, StackProps } from "aws-cdk-lib";
2
+ import { Construct } from "constructs";
3
+ import { Pipeline as AwsPipeline, IAction } from "aws-cdk-lib/aws-codepipeline";
4
+ import * as path from "path";
5
+
6
+ import { Artifact } from "./artifact";
7
+ import { Segment } from "./segment";
8
+ import { SourceSegment } from "./source-segment";
9
+ import { PipelineSegment } from "./pipeline-segment";
10
+
11
+ export interface PipelineProps extends StackProps {
12
+ /**
13
+ * The name of the generated pipeline.
14
+ * @default Stack ID
15
+ */
16
+ readonly pipelineName?: string;
17
+ /**
18
+ * The path to the cdk projects root directory containing the cdk.json file
19
+ * relative to the asset root
20
+ */
21
+ readonly rootDir: string;
22
+ /**
23
+ * The segments to populating the pipeline.
24
+ */
25
+ readonly segments: Segment[];
26
+ }
27
+
28
+ /**
29
+ * @category Constructs
30
+ */
31
+ export class Pipeline extends Stack {
32
+ readonly pipelineName: string;
33
+ readonly rootDir: string;
34
+ readonly buildDir: string;
35
+
36
+ constructor(scope: Construct, id: string, readonly props: PipelineProps) {
37
+ super(scope, id, props);
38
+
39
+ this.pipelineName = props.pipelineName ? props.pipelineName : id;
40
+ this.rootDir = props.rootDir;
41
+ this.buildDir = path.join(this.rootDir, (this.node.root as App).outdir);
42
+
43
+ if (!this.bundlingRequired) return;
44
+
45
+ props.segments.forEach((segment: Segment) => {
46
+ segment.inputs.forEach((artifact: Artifact) => {
47
+ if (!artifact.obtainProducer())
48
+ throw new Error("Artifact consumed but never produced.");
49
+ });
50
+ });
51
+
52
+ const sourceSegments: SourceSegment[] = props.segments.filter(
53
+ (segment: Segment) => segment.isSource
54
+ ) as SourceSegment[];
55
+
56
+ const pipelineSegment: PipelineSegment | undefined = props.segments.find(
57
+ (segment: Segment) => segment.isPipeline
58
+ ) as PipelineSegment;
59
+
60
+ const segments: Segment[] = props.segments.filter(
61
+ (segment: Segment) => !segment.isSource && !segment.isPipeline
62
+ );
63
+
64
+ new AwsPipeline(this, "Pipeline", {
65
+ pipelineName: props.pipelineName,
66
+ restartExecutionOnUpdate: true,
67
+ stages: [
68
+ {
69
+ stageName: "Source",
70
+ actions: [
71
+ ...sourceSegments.reduce(
72
+ (actions: IAction[], segment: SourceSegment) => [
73
+ ...actions,
74
+ ...segment.construct(this).actions,
75
+ ],
76
+ []
77
+ ),
78
+ ],
79
+ },
80
+ {
81
+ stageName: "Pipeline",
82
+ actions: [...pipelineSegment.construct(this).actions],
83
+ },
84
+ ...segments.map((segment: Segment) => {
85
+ const build = segment.construct(this);
86
+ return {
87
+ stageName: build.name,
88
+ actions: build.actions,
89
+ };
90
+ }),
91
+ ],
92
+ });
93
+ }
94
+ }
@@ -0,0 +1,126 @@
1
+ import { Stack } from "aws-cdk-lib";
2
+ import { Construct } from "constructs";
3
+ import {
4
+ ActionBindOptions,
5
+ ActionConfig,
6
+ ActionProperties,
7
+ Artifact,
8
+ IAction,
9
+ IStage,
10
+ } from "aws-cdk-lib/aws-codepipeline";
11
+ import { BuildSpec, LinuxBuildImage, Project } from "aws-cdk-lib/aws-codebuild";
12
+ import { CodeBuildAction } from "aws-cdk-lib/aws-codepipeline-actions";
13
+ import {
14
+ AccountPrincipal,
15
+ CompositePrincipal,
16
+ PolicyDocument,
17
+ PolicyStatement,
18
+ Role,
19
+ ServicePrincipal,
20
+ } from "aws-cdk-lib/aws-iam";
21
+ import { IRuleTarget, RuleProps, Rule } from "aws-cdk-lib/aws-events";
22
+ import * as path from "path";
23
+
24
+ export interface PublishAssetsActionProps {
25
+ readonly actionName: string;
26
+ readonly input: Artifact;
27
+ readonly manifestPath: string;
28
+ readonly runOrder?: number;
29
+ }
30
+
31
+ export class PublishAssetsAction extends Construct implements IAction {
32
+ public readonly actionProperties: ActionProperties;
33
+
34
+ bound(
35
+ scope: Construct,
36
+ stage: IStage,
37
+ options: ActionBindOptions
38
+ ): ActionConfig {
39
+ throw new Error(`Method not implemented.${!scope && !stage! && options}`);
40
+ }
41
+
42
+ bind(
43
+ scope: Construct,
44
+ stage: IStage,
45
+ options: ActionBindOptions
46
+ ): ActionConfig {
47
+ throw new Error(`Method not implemented.${!scope && !stage! && options}`);
48
+ }
49
+
50
+ onStateChange(
51
+ name: string,
52
+ target?: IRuleTarget | undefined,
53
+ options?: RuleProps | undefined
54
+ ): Rule {
55
+ throw new Error(`Method not implemented.${!name && !target! && options}`);
56
+ }
57
+
58
+ constructor(scope: Construct, id: string, props: PublishAssetsActionProps) {
59
+ super(scope, id);
60
+ const codeBuild = new CodeBuildAction({
61
+ ...props,
62
+ input: props.input,
63
+ project: new Project(this, id, {
64
+ environment: {
65
+ buildImage: LinuxBuildImage.AMAZON_LINUX_2_4,
66
+ },
67
+ role: new Role(this, "UpdatePipelineCodeCuildRole", {
68
+ assumedBy: new CompositePrincipal(
69
+ new ServicePrincipal("codebuild.amazonaws.com"),
70
+ new AccountPrincipal(Stack.of(this).account)
71
+ ),
72
+ inlinePolicies: {
73
+ selfMutation: new PolicyDocument({
74
+ statements: [
75
+ new PolicyStatement({
76
+ actions: ["sts:AssumeRole"],
77
+ resources: [`arn:*:iam::${Stack.of(this).account}:role/*`],
78
+ conditions: {
79
+ "ForAnyValue:StringEquals": {
80
+ "iam:ResourceTag/aws-cdk:bootstrap-role": [
81
+ "image-publishing",
82
+ "file-publishing",
83
+ "deploy",
84
+ ],
85
+ },
86
+ },
87
+ }),
88
+ ],
89
+ }),
90
+ },
91
+ }),
92
+ buildSpec: BuildSpec.fromObject({
93
+ version: "0.2",
94
+ phases: {
95
+ install: {
96
+ "runtime-versions": {
97
+ nodejs: 18,
98
+ },
99
+ commands: [
100
+ "npm install -g npm@latest",
101
+ "npm i -g @flit/publish-cdk-assets@latest",
102
+ ],
103
+ },
104
+ build: {
105
+ commands: `pca ${
106
+ props.manifestPath ? path.normalize(props.manifestPath) : "."
107
+ }`,
108
+ },
109
+ },
110
+ }),
111
+ }),
112
+ });
113
+
114
+ this.actionProperties = codeBuild.actionProperties;
115
+ this.bind = (
116
+ scope: Construct,
117
+ stage: IStage,
118
+ options: ActionBindOptions
119
+ ): ActionConfig => codeBuild.bind(scope, stage, options);
120
+ this.onStateChange = (
121
+ name: string,
122
+ target?: IRuleTarget,
123
+ options?: RuleProps
124
+ ): Rule => codeBuild.onStateChange(name, target, options);
125
+ }
126
+ }
package/src/segment.ts ADDED
@@ -0,0 +1,45 @@
1
+ import { Construct } from "constructs";
2
+ import { Stack } from "aws-cdk-lib";
3
+ import { IAction } from "aws-cdk-lib/aws-codepipeline";
4
+
5
+ import { Artifact } from "./artifact";
6
+ import { Pipeline } from "./pipeline";
7
+
8
+ export interface SegmentProps {
9
+ readonly input?: Artifact | Artifact[];
10
+ readonly output?: Artifact | Artifact[];
11
+ }
12
+
13
+ export abstract class Segment {
14
+ readonly isSource: boolean = false;
15
+ readonly isPipeline: boolean = false;
16
+ readonly dependencies?: Stack[];
17
+ readonly inputs: Artifact[] = [];
18
+ readonly outputs: Artifact[] = [];
19
+ constructor(props: SegmentProps) {
20
+ if (props.input) {
21
+ this.inputs = (
22
+ props.input.constructor.name === "Array" ? props.input : [props.input]
23
+ ) as Artifact[];
24
+ this.inputs.forEach((artifact: Artifact) => {
25
+ artifact.consume(this);
26
+ }, this);
27
+ }
28
+ if (props.output) {
29
+ this.outputs = (
30
+ props.output.constructor.name === "Array"
31
+ ? props.output
32
+ : [props.output]
33
+ ) as Artifact[];
34
+ this.outputs.forEach((artifact: Artifact) => {
35
+ artifact.produce(this);
36
+ }, this);
37
+ }
38
+ }
39
+ abstract construct(scope: Pipeline): SegmentConstructed;
40
+ }
41
+
42
+ export abstract class SegmentConstructed extends Construct {
43
+ readonly name: string = "";
44
+ readonly actions: IAction[] = [];
45
+ }