This repository was archived by the owner on Feb 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCodePipeline.py
More file actions
110 lines (96 loc) · 4.19 KB
/
Copy pathCodePipeline.py
File metadata and controls
110 lines (96 loc) · 4.19 KB
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from aws_cdk import (
aws_s3,
aws_codepipeline,
aws_ecr,
aws_ssm,
aws_codecommit,
aws_codebuild,
aws_codepipeline_actions
)
import aws_cdk as core
from constructs import Construct
class CodePipelineStack(core.Stack):
def __init__(self, scope: Construct, id: str, namespace, ** kwargs) -> None:
super().__init__(scope, id, **kwargs)
# get repository name
repository_name = aws_ssm.StringParameter.value_for_string_parameter(
self, f"{namespace}-repository")
# get repository reference
code_repository = aws_codecommit.Repository.from_repository_name(
scope=self, id=f"codecommit-repository-{id}", repository_name=repository_name)
# get codebuild project name
buildproject_name = aws_ssm.StringParameter.value_for_string_parameter(
self, f"{namespace}-codebuild")
# get build project reference
build_project = aws_codebuild.PipelineProject.from_project_name(
scope=self, id=f"codebuild-project-{id}", project_name=buildproject_name)
# get codebuild project name
source_bucket_name = aws_ssm.StringParameter.value_for_string_parameter(
self, f"{namespace}-sourcebucket")
# get build project reference
source_bucket = aws_s3.Bucket.from_bucket_name(
scope=self, id=f"artifacts-bucket-{id}", bucket_name=source_bucket_name)
# define the s3 artifact
artifacts_bucket = aws_codepipeline.Artifact(artifact_name='source')
# define the pipeline
pipeline = aws_codepipeline.Pipeline(
self, "CodePipeline",
pipeline_name=f"{namespace}",
artifact_bucket=source_bucket,
stages=[
aws_codepipeline.StageProps(
stage_name='Source',
actions=[
aws_codepipeline_actions.CodeCommitSourceAction(
repository=code_repository,
branch='main',
action_name='CodeCommitSourceRetrieval',
output=artifacts_bucket,
),
]
),
aws_codepipeline.StageProps(
stage_name='Build',
actions=[
aws_codepipeline_actions.CodeBuildAction(
action_name='DockerBuildImages',
input=artifacts_bucket,
project=build_project,
)
]
)
]
)
source_bucket.grant_read_write(pipeline.role)
# pipeline param to get the
pipeline_param = aws_ssm.StringParameter(
self, "CodePipelineName",
parameter_name=f"{namespace}-pipeline",
string_value=pipeline.pipeline_name,
description='Code Pipeline Name'
)
# cfn output
core.CfnOutput(
self, "PipelineName",
description="CodePipeline Name",
value=pipeline.pipeline_name
)
# # pass objects to another stack
# @property
# def outputs(self):
# return self.output_props