Last active
October 19, 2022 07:23
-
-
Save wcheek/76545f921a721967177c9131cfa184de to your computer and use it in GitHub Desktop.
CDK Pipelines aws_codepipeline Website Deployment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aws_cdk import Stack, aws_s3 | |
from aws_cdk import aws_secretsmanager as secretsmanager | |
from aws_cdk.aws_codebuild import ( | |
BuildEnvironment, | |
ComputeType, | |
LinuxBuildImage, | |
PipelineProject, | |
) | |
from aws_cdk.aws_codepipeline import Artifact, Pipeline | |
from aws_cdk.aws_codepipeline_actions import ( | |
CodeBuildAction, | |
GitHubSourceAction, | |
GitHubTrigger, | |
S3DeployAction, | |
) | |
from constructs import Construct | |
class PipelineStack(Stack): | |
def __init__( | |
self, | |
scope: Construct, | |
construct_id: str, | |
bucket: aws_s3.IBucket, | |
**kwargs | |
) -> None: | |
super().__init__(scope, construct_id, **kwargs) | |
self.bucket = bucket | |
pipeline: Pipeline = Pipeline( | |
self, id="pipeline", pipeline_name="WebsitePipeline" | |
) | |
source_artifact, github_source_action = self.create_source_action() | |
code_pipeline_project = self.create_pipeline_project() | |
code_build_artifact, code_build_action = self.create_build_action( | |
source_artifact=source_artifact, | |
code_pipeline_project=code_pipeline_project, | |
) | |
s3_deploy_action = self.create_s3_deploy_action( | |
code_build_artifact=code_build_artifact | |
) | |
pipeline.add_stage(stage_name="source", actions=[github_source_action]) | |
pipeline.add_stage(stage_name="build", actions=[code_build_action]) | |
pipeline.add_stage(stage_name="deploy", actions=[s3_deploy_action]) | |
def create_source_action(self): | |
source_artifact = Artifact(artifact_name="source_artifact") | |
github_secret = secretsmanager.Secret.from_secret_name_v2( | |
scope=self, | |
id="github_secret", | |
secret_name="Github_Personal_Access_Token", | |
) | |
github_source_action = GitHubSourceAction( | |
action_name="Github_source_action", | |
oauth_token=github_secret.secret_value_from_json( | |
key="github_access_token" | |
), | |
owner="wcheek", | |
repo="website", | |
branch="main", | |
output=source_artifact, | |
# Change this to WEBHOOK if I just want to do release changes. | |
trigger=GitHubTrigger.WEBHOOK, | |
) | |
return source_artifact, github_source_action | |
def create_pipeline_project(self): | |
build_environment = BuildEnvironment( | |
build_image=LinuxBuildImage.STANDARD_5_0, | |
compute_type=ComputeType.SMALL, | |
) | |
return PipelineProject( | |
scope=self, | |
id="code_pipeline_project", | |
environment=build_environment, | |
project_name="code_build_project", | |
) | |
def create_build_action(self, source_artifact, code_pipeline_project): | |
code_build_artifact = Artifact(artifact_name="code_build_artifact") | |
code_build_action = CodeBuildAction( | |
action_name="build_action", | |
project=code_pipeline_project, | |
input=source_artifact, | |
outputs=[code_build_artifact], | |
) | |
return code_build_artifact, code_build_action | |
def create_s3_deploy_action(self, code_build_artifact): | |
return S3DeployAction( | |
action_name="s3_deploy_action", | |
bucket=self.bucket, | |
input=code_build_artifact, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment