@flit/cdk-pipeline 1.0.9 → 1.0.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flit/cdk-pipeline",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "license": "Apache-2.0",
5
5
  "description": "A highly customizable and extensible CI/CD pipeline intended as alternative to CDK's native CodePipeline",
6
6
  "homepage": "https://p-mercury.github.io/jumper-de/modules/_flit_cdk_pipeline",
@@ -15,7 +15,8 @@
15
15
  ],
16
16
  "author": {
17
17
  "name": "Luis Vierroth",
18
- "email": "luis@jumper.de"
18
+ "email": "luis@jumper.de",
19
+ "url": "https://github.com/p-mercury"
19
20
  },
20
21
  "repository": {
21
22
  "type": "git",
@@ -25,10 +26,12 @@
25
26
  "main": "./lib/index.js",
26
27
  "types": "./lib/index.d.ts",
27
28
  "typedoc": {
28
- "entryPoint": "./src/index.ts"
29
+ "entryPoint": "./src/index.ts",
30
+ "readmeFile": "./TYPEDOC.md"
29
31
  },
30
32
  "files": [
31
33
  "lib",
34
+ "src",
32
35
  ".jsii",
33
36
  "README.md"
34
37
  ],
@@ -41,9 +44,9 @@
41
44
  },
42
45
  "dependencies": {},
43
46
  "devDependencies": {
44
- "@types/node": "18.15.10",
47
+ "@types/node": "18.15.11",
45
48
  "path": "^0.12.7",
46
- "aws-cdk": "2.70.0",
49
+ "aws-cdk": "2.72.0",
47
50
  "cdk-aws-lambda-powertools-layer": "3.3.0",
48
51
  "jest": "29.5.0",
49
52
  "jsii": "5.0.1",
@@ -51,12 +54,12 @@
51
54
  "ts-jest": "29.0.5",
52
55
  "ts-node": "10.9.1",
53
56
  "typescript": "5.0.2",
54
- "aws-cdk-lib": "2.70.0",
55
- "constructs": "10.1.293"
57
+ "aws-cdk-lib": "2.72.0",
58
+ "constructs": "10.1.296"
56
59
  },
57
60
  "peerDependencies": {
58
- "aws-cdk-lib": "2.70.0",
59
- "constructs": "10.1.293"
61
+ "aws-cdk-lib": "2.72.0",
62
+ "constructs": "10.1.296"
60
63
  },
61
64
  "bundledDependencies": [
62
65
  "path"
@@ -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
+ }
@@ -0,0 +1,62 @@
1
+ import { IAction } from "aws-cdk-lib/aws-codepipeline";
2
+ import {
3
+ CodeCommitSourceAction,
4
+ CodeCommitTrigger,
5
+ } from "aws-cdk-lib/aws-codepipeline-actions";
6
+ import { IRepository } from "aws-cdk-lib/aws-codecommit";
7
+
8
+ import { Pipeline } from "./pipeline";
9
+ import { Artifact } from "./artifact";
10
+ import { SegmentConstructed } from "./segment";
11
+ import { SourceSegment, SourceSegmentProps } from "./source-segment";
12
+
13
+ export interface CodeCommitSourceSegmentProps extends SourceSegmentProps {
14
+ readonly repository: IRepository;
15
+ readonly branch?: string;
16
+ readonly trigger?: CodeCommitTrigger;
17
+ readonly variablesNamespace?: string;
18
+ }
19
+
20
+ /**
21
+ * @category Segments
22
+ */
23
+ export class CodeCommitSourceSegment extends SourceSegment {
24
+ private props: CodeCommitSourceSegmentProps;
25
+ constructor(props: CodeCommitSourceSegmentProps) {
26
+ super(props);
27
+ this.props = props;
28
+ }
29
+ construct(scope: Pipeline): SegmentConstructed {
30
+ return new CodeCommitSourceSegmentConstructed(
31
+ scope,
32
+ this.props.repository.repositoryName,
33
+ {
34
+ ...this.props,
35
+ actionName: this.props.repository.repositoryName,
36
+ }
37
+ );
38
+ }
39
+ }
40
+
41
+ export interface CodeCommitSourceSegmentConstructedProps {
42
+ readonly output: Artifact;
43
+ readonly actionName: string;
44
+ readonly repository: IRepository;
45
+ readonly branch?: string;
46
+ readonly trigger?: CodeCommitTrigger;
47
+ readonly variablesNamespace?: string;
48
+ }
49
+
50
+ export class CodeCommitSourceSegmentConstructed extends SegmentConstructed {
51
+ readonly name: string;
52
+ readonly actions: IAction[];
53
+ constructor(
54
+ scope: Pipeline,
55
+ id: string,
56
+ props: CodeCommitSourceSegmentConstructedProps
57
+ ) {
58
+ super(scope, id);
59
+ this.name = "Source";
60
+ this.actions = [new CodeCommitSourceAction(props)];
61
+ }
62
+ }
@@ -0,0 +1,64 @@
1
+ import { IAction } from "aws-cdk-lib/aws-codepipeline";
2
+ import {
3
+ GitHubSourceAction,
4
+ GitHubTrigger,
5
+ } from "aws-cdk-lib/aws-codepipeline-actions";
6
+ import { SecretValue } from "aws-cdk-lib";
7
+
8
+ import { Pipeline } from "./pipeline";
9
+ import { Artifact } from "./artifact";
10
+ import { SegmentConstructed } from "./segment";
11
+ import { SourceSegment, SourceSegmentProps } from "./source-segment";
12
+
13
+ export interface GitHubSourceSegmentProps extends SourceSegmentProps {
14
+ readonly oauthToken: SecretValue;
15
+ readonly owner: string;
16
+ readonly repository: string;
17
+ readonly branch?: string;
18
+ readonly trigger?: GitHubTrigger;
19
+ readonly variablesNamespace?: string;
20
+ }
21
+
22
+ /**
23
+ * @category Segments
24
+ */
25
+ export class GitHubSourceSegment extends SourceSegment {
26
+ private props: GitHubSourceSegmentProps;
27
+ constructor(props: GitHubSourceSegmentProps) {
28
+ super(props);
29
+ this.props = props;
30
+ }
31
+ construct(scope: Pipeline): SegmentConstructed {
32
+ return new GitHubSourceSegmentConstructed(scope, this.props.repository, {
33
+ ...this.props,
34
+ actionName: this.props.repository,
35
+ repo: this.props.repository,
36
+ });
37
+ }
38
+ }
39
+
40
+ export interface GitHubSourceSegmentConstructedProps {
41
+ readonly output: Artifact;
42
+ readonly actionName: string;
43
+ readonly oauthToken: SecretValue;
44
+ readonly owner: string;
45
+ readonly repo: string;
46
+ readonly branch?: string;
47
+ readonly runOrder?: number;
48
+ readonly trigger?: GitHubTrigger;
49
+ readonly variablesNamespace?: string;
50
+ }
51
+
52
+ export class GitHubSourceSegmentConstructed extends SegmentConstructed {
53
+ readonly name: string;
54
+ readonly actions: IAction[];
55
+ constructor(
56
+ scope: Pipeline,
57
+ id: string,
58
+ props: GitHubSourceSegmentConstructedProps
59
+ ) {
60
+ super(scope, id);
61
+ this.name = "Source";
62
+ this.actions = [new GitHubSourceAction(props)];
63
+ }
64
+ }
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ export * from "./artifact";
2
+ export * from "./code-commit-source-segment";
3
+ export * from "./git-hub-source-segment";
4
+ export * from "./pipeline-segment";
5
+ export * from "./pipeline";
6
+ export * from "./publish-assets-action";
7
+ export * from "./s3-source-segment";
8
+ export * from "./segment";
9
+ export * from "./source-segment";
10
+ 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
+ }
@@ -0,0 +1,60 @@
1
+ import { IAction } from "aws-cdk-lib/aws-codepipeline";
2
+ import {
3
+ S3SourceAction,
4
+ S3Trigger,
5
+ } from "aws-cdk-lib/aws-codepipeline-actions";
6
+ import { SecretValue } from "aws-cdk-lib";
7
+ import { IBucket } from "aws-cdk-lib/aws-s3";
8
+
9
+ import { Pipeline } from "./pipeline";
10
+ import { Artifact } from "./artifact";
11
+ import { SegmentConstructed } from "./segment";
12
+ import { SourceSegment, SourceSegmentProps } from "./source-segment";
13
+
14
+ export interface S3SourceSegmentProps extends SourceSegmentProps {
15
+ readonly oauthToken: SecretValue;
16
+ readonly bucket: IBucket;
17
+ readonly bucketKey: string;
18
+ readonly trigger?: S3Trigger;
19
+ readonly variablesNamespace?: string;
20
+ }
21
+
22
+ /**
23
+ * @category Segments
24
+ */
25
+ export class S3SourceSegment extends SourceSegment {
26
+ private props: S3SourceSegmentProps;
27
+ constructor(props: S3SourceSegmentProps) {
28
+ super(props);
29
+ this.props = props;
30
+ }
31
+ construct(scope: Pipeline): SegmentConstructed {
32
+ return new S3SourceSegmentConstructed(scope, this.props.bucket.bucketName, {
33
+ ...this.props,
34
+ actionName: this.props.bucket.bucketName,
35
+ });
36
+ }
37
+ }
38
+
39
+ export interface S3SourceSegmentConstructedProps {
40
+ readonly output: Artifact;
41
+ readonly actionName: string;
42
+ readonly bucket: IBucket;
43
+ readonly bucketKey: string;
44
+ readonly trigger?: S3Trigger;
45
+ readonly variablesNamespace?: string;
46
+ }
47
+
48
+ export class S3SourceSegmentConstructed extends SegmentConstructed {
49
+ readonly name: string;
50
+ readonly actions: IAction[];
51
+ constructor(
52
+ scope: Pipeline,
53
+ id: string,
54
+ props: S3SourceSegmentConstructedProps
55
+ ) {
56
+ super(scope, id);
57
+ this.name = "Source";
58
+ this.actions = [new S3SourceAction(props)];
59
+ }
60
+ }
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
+ }
@@ -0,0 +1,10 @@
1
+ import { Artifact } from "./artifact";
2
+ import { Segment } from "./segment";
3
+
4
+ export interface SourceSegmentProps {
5
+ readonly output: Artifact;
6
+ }
7
+
8
+ export abstract class SourceSegment extends Segment {
9
+ readonly isSource: boolean = true;
10
+ }