以GitHub为源的CDK代码管道

2024-04-29 03:18:15 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我正在使用CDK codebuild.PipelineProject创建一个新的codebuild项目,我遇到了一些问题,因为APi引用似乎没有办法将GitHub应用为源repo:

https://docs.aws.amazon.com/cdk/api/latest/python/aws_cdk.aws_codebuild/PipelineProject.html

有人能让这项功能发挥作用,或者找到将Github设置为主要源的方法吗


Tags: 项目httpsgithubcomawsapidocsamazon
1条回答
网友
1楼 · 发布于 2024-04-29 03:18:15

这个问题的答案取决于你理想的最终状态。这里有两个选项:

  1. 代码管道触发代码构建
  2. 由代码管道外部的GitHub触发的代码构建

This part of the docs讨论这两个选项之间的区别

代码管道触发代码构建

如果要从同一个源提交(如部署)执行其他codepipeline_actions,则使用代码管道触发代码构建非常有用

为此,您需要在CodePipeline中创建一个单独的源操作,并使用其导出的变量作为CodeBuild项目(docs)的输入

比如说,

from aws_cdk import core as cdk
from aws_cdk import aws_codepipeline as codepipeline
from aws_cdk import aws_codepipeline_actions as codepipeline_actions
from aws_cdk import aws_codebuild as codebuild


class CdkPythonStack(cdk.Stack):
    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        source_output = codepipeline.Artifact()
        source_action = codepipeline_actions.GitHubSourceAction(
            action_name="Source",
            owner="blimmer",
            repo="my_repo",
            # You'll need to create this secret per the docs
            # https://docs.aws.amazon.com/cdk/api/latest/docs/aws-codepipeline-actions-readme.html#github
            oauth_token=cdk.SecretValue.secrets_manager('my-github-token'),
            output=source_output,
        )
        pipeline = codepipeline.Pipeline(
            self,
            "MyPipeline",
            stages=[
                codepipeline.StageProps(stage_name="Source", actions=[source_action]),
                codepipeline.StageProps(
                    stage_name="Build",
                    actions=[
                        codepipeline_actions.CodeBuildAction(
                            action_name="Build",
                            # Configure your project here
                            project=codebuild.PipelineProject(self, "MyProject"),
                            input=source_output,
                        )
                    ],
                ),
            ],
        )

由代码管道外部的GitHub触发的代码构建

如果您不需要任何其他CodePipeline操作,只创建一个没有CodePipeline的CodeBuild项目可能会更简单

您需要将源设置为CodeBuild项目(docs)的属性,然后定义该项目

例如:

from aws_cdk import core as cdk
from aws_cdk import aws_codebuild as codebuild


class CdkPythonStack(cdk.Stack):
    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        project = codebuild.Project(
            self,
            "MyProject",
            # You'll need to configure this first. See:
            # https://docs.aws.amazon.com/cdk/api/latest/docs/aws-codebuild-readme.html#githubsource-and-githubenterprisesource
            source=codebuild.Source.git_hub(
                owner="blimmer", repo="my_repo", webhook=True
            ),
            # Configure your project here
        )

相关问题 更多 >