Jimmy Chen
Jimmy Chen

Reputation: 267

CDK Codepipeline with GitHub as the source

So, I'm using CDK codebuild.PipelineProject to create a new Codebuild project and I'm running into issues because The APi reference doesn't seem to have a way to apply GitHub as the source repo:

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

Anyone get this to work or find a way to setup Github as the primary source?

Upvotes: 2

Views: 3891

Answers (1)

blimmer
blimmer

Reputation: 2486

The answer to this depends on what your ideal end state looks like. There are two options here:

  1. CodePipeline triggers CodeBuild
  2. CodeBuild triggered by GitHub outside of CodePipeline

This part of the docs discusses the difference between these two options.

CodePipeline triggers CodeBuild

Using a CodePipeline to trigger CodeBuild is useful if you have other codepipeline_actions you want to execute from the same source commit (like a deployment).

To do this, you'll need to create a separate Source action in the CodePipeline and use its exported variables as input to the CodeBuild project (docs).

For instance,

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,
                        )
                    ],
                ),
            ],
        )

CodeBuild triggered by GitHub outside of CodePipeline

If you don't need any other CodePipeline actions, it might be simpler to just create a CodeBuild project without a CodePipeline.

You'll need to set up the Source as a property to the CodeBuild project (docs), then define the Project.

For instance:

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
        )

Upvotes: 5

Related Questions