class CDKConfig(Construct): build_action: CodeBuildAction = None cdk_artifact: Artifact = Artifact() def __init__( self, scope: Construct, id: str, envs: EnvSettings, build_stage: IStage, input_artifact: Artifacts, ): super().__init__(scope, id) project = PipelineProject( self, "CDKStackBuild", project_name=f"{envs.project_name}-build-stack", environment=BuildEnvironment(build_image=LinuxBuildImage.STANDARD_4_0, privileged=True,), build_spec=BuildSpec.from_source_filename("./infra/stacks/ci/buildspecs/cdk.yaml"), cache=Cache.local(LocalCacheMode.CUSTOM), ) self.build_action = self.create_build_action("stack", project, input_artifact) build_stage.add_action(self.build_action) def create_build_action(self, name: str, project: PipelineProject, input_artifact: Artifacts): return CodeBuildAction( action_name=f"build-{name}", project=project, input=input_artifact, outputs=[self.cdk_artifact], extra_inputs=[], run_order=3, )
def prepare_api_changes(envs: EnvSettings, cdk_artifact: Artifact): return CloudFormationCreateReplaceChangeSetAction( action_name="prepare-app-changes", stack_name=f"{envs.project_name}-api", change_set_name="APIStagedChangeSet", admin_permissions=True, template_path=cdk_artifact.at_path( "infra/cdk.out/schema-cms-api.template.json"), run_order=2, )
def crate_build_action(self, name: str, project: PipelineProject): output = Artifact() action = CodeBuildAction( action_name=f"build-{name}", project=project, input=self.input_artifact, outputs=[output], run_order=1, ) return action, output
def __init__(self, scope): super().__init__(scope, "bug") bucket = Bucket.from_bucket_name( self, "artifacts", core.Fn.import_value("CodeArtifactsBucket") ) pipeline_role = Role.from_role_arn( self, "pipeline", core.Fn.import_value("CodePipelineRole") ) pipeline = Pipeline( self, "Pipeline", artifact_bucket=bucket, role=pipeline_role, stages=[ StageProps( stage_name="Source", actions=[ GitHubSourceAction( action_name="Source", run_order=1, oauth_token=core.SecretValue("something"), output=Artifact(artifact_name="SourceArtifact"), owner="me", repo="repo", branch="master", ) ], ) ], ) pipeline.add_stage( stage_name="Fails", actions=[ LambdaInvokeAction( action_name="LambdaInvokeAction", run_order=1, lambda_=Function.from_function_arn( self, "function", core.Fn.import_value("SomeFunction") ), ) ], )
def prepare_image_resize_lambda_changes(self, envs: EnvSettings, cdk_artifact: Artifact, function_code: Code): return CloudFormationCreateReplaceChangeSetAction( action_name="prepare-image-resize-lambda-changes", stack_name=f"{envs.project_name}-image-resize", change_set_name="imageResizeLambdaStagedChangeSet", admin_permissions=True, template_path=cdk_artifact.at_path( "infra/cdk.out/schema-cms-image-resize.template.json"), run_order=2, parameter_overrides={ **function_code.assign( bucket_name=self.output.s3_location.bucket_name, object_key=self.output.s3_location.object_key, object_version=self.output.s3_location.object_version, ) }, extra_inputs=[self.output], )
class ImageResizeLambdaCiConfig(Construct): input_artifact: Artifact = None output: Artifact = Artifact() def __init__( self, scope: Construct, id: str, envs: EnvSettings, build_stage: IStage, input_artifact: Artifact, ): super().__init__(scope, id) build_project = self.create_build_project(envs) build_stage.add_action( self.crate_build_action("image-resize-lambda", build_project, input_artifact)) def create_build_project(self, envs: EnvSettings): project = PipelineProject( self, "ImageResizeLambdaBuild", project_name=f"{envs.project_name}-build-image-resize-lambda", environment=BuildEnvironment( build_image=LinuxBuildImage.STANDARD_3_0), build_spec=BuildSpec.from_source_filename( "./infra/stacks/ci/buildspecs/image_resize.yaml"), ) return project def crate_build_action(self, name: str, project: PipelineProject, input_artifact: Artifact): return CodeBuildAction( action_name=f"build-{name}", project=project, input=input_artifact, outputs=[self.output], run_order=1, ) def prepare_image_resize_lambda_changes(self, envs: EnvSettings, cdk_artifact: Artifact, function_code: Code): return CloudFormationCreateReplaceChangeSetAction( action_name="prepare-image-resize-lambda-changes", stack_name=f"{envs.project_name}-image-resize", change_set_name="imageResizeLambdaStagedChangeSet", admin_permissions=True, template_path=cdk_artifact.at_path( "infra/cdk.out/schema-cms-image-resize.template.json"), run_order=2, parameter_overrides={ **function_code.assign( bucket_name=self.output.s3_location.bucket_name, object_key=self.output.s3_location.object_key, object_version=self.output.s3_location.object_version, ) }, extra_inputs=[self.output], ) @staticmethod def execute_image_resize_lambda_changes(envs: EnvSettings): return CloudFormationExecuteChangeSetAction( action_name="execute-image-resize-lambda_changes", stack_name=f"{envs.project_name}-image-resize", change_set_name="imageResizeLambdaStagedChangeSet", run_order=3, )
def get_output_artifact(envs: EnvSettings): return Artifact.artifact(name=f"{envs.project_name}-source")
def __init__(self, scope: Construct, id: str, *, github_branch: str, github_owner: str, github_repo: str, github_token_secret: SecretValue, environment_variables: Dict[str, str] = None) -> None: """ :param scope: The scope in which to define this construct. :param id: The scoped construct ID. Must be unique amongst siblings. If the ID includes a path separator (``/``), then it will be replaced by double dash ``--``. :param github_branch: GitHub branch that will be used for deployment :param github_owner: User or organization, this is case sensitive :param github_repo: Name of the repository :param github_token_secret: Secret value containing the github developer token :param environment_variables: Map of environment variable names and parameter store keys. This construct only supports parameter store env vars. """ super().__init__(scope, id) # The code that defines your stack goes here if environment_variables: environment_variables = { k: BuildEnvironmentVariable( value=v, type=BuildEnvironmentVariableType.PARAMETER_STORE) for k, v in environment_variables.items() } project = PipelineProject(self, f"build-deploy", project_name=f"{id}-build-deploy", environment_variables=environment_variables) bucket = Bucket(self, f"{id}-bucket") pipeline = aws_codepipeline.Pipeline(self, f"{id}-pipeline", pipeline_name=f"{id}", artifact_bucket=bucket, restart_execution_on_update=True) app_source = Artifact() pipeline.add_stage(stage_name='Source', actions=[ GitHubSourceAction( action_name="GitHub", owner=github_owner, repo=github_repo, branch=github_branch, oauth_token=github_token_secret, output=app_source, trigger=GitHubTrigger.POLL, run_order=1, ) ]) app_artifacts = Artifact() pipeline.add_stage(stage_name="BuildAndDeploy", actions=[ CodeBuildAction( action_name="CodeBuild", input=app_source, project=project, outputs=[app_artifacts], run_order=1, ) ])