AWS CDK Code Pipeline with Code Build GitHub Source

0

I am attempting to build an AWS Code Pipeline with AWS CDK in Python. I have managed to build a basic Code Pipeline but am having an issue trying to use GitHub as a source for Code Build.

I've included an example of the code below. When I try and deploy this I get a CDK error:

TypeError: PipelineProject.init() got an unexpected keyword argument 'source'

The source configuration in the example code below is exactly as per the code.build.Source class example. If I rem out the source lines in the code the CDK deploy works with no errors. Any ideas would be gratefully received.

docker_build_project = codebuild.PipelineProject(
            self, "Docker Build",
            build_spec=codebuild.BuildSpec.from_source_filename("./buildspec.yml"),
            source=codebuild.Source.git_hub(
                owner="<owner>",
                repo="<repo>"
            ),
            environment=codebuild.BuildEnvironment(
                build_image=codebuild.LinuxBuildImage.STANDARD_5_0, 
                privileged=True,
                compute_type=codebuild.ComputeType.LARGE,
                environment_variables={
                    "IMAGE_TAG": codebuild.BuildEnvironmentVariable(
                        type=codebuild.BuildEnvironmentVariableType.PLAINTEXT,
                        value='latest'
                    ),
                    "IMAGE_REPO_URI": codebuild.BuildEnvironmentVariable(
                        type=codebuild.BuildEnvironmentVariableType.PLAINTEXT,
                        value=ecr_repository.repository_uri
                    ),
                    "AWS_DEFAULT_REGION": codebuild.BuildEnvironmentVariable(
                        type=codebuild.BuildEnvironmentVariableType.PLAINTEXT,
                        value=os.environ["CDK_DEFAULT_REGION"]
                    )
                }
            ),
        )
1 Answer
1
Accepted Answer

I think you need to take a step back and review. When using CodePipeline there needs to be a source stage. The source stage will then produce an artifact which is passed to the CodeBuild stage.

Also, I am going to assume that you are using aws_codepipeline_actions and not pipelines CDK constructs. Personally, I prefer aws_codepipeline_actions since it provides more control.

Below is some typescript that shows how you could add the docker_build_project to a build action step. Notice it has a variable called "sourceOutput" which is the output from the prior step (not shown), which is the source stage. This "sourceOutput" is ultimately input to CodeBuild

    const buildAction = new aws_codepipeline_actions.CodeBuildAction({
      actionName: `actionname`,
      docker_build_project,
      input: sourceOutput,
      outputs: [s3BucketArtifact],
    });

Hope this helps, if it does please accept this answer.

profile picture
answered 9 months ago
profile picture
EXPERT
reviewed a month ago
  • Thanks Bryant your suggestion pointed me in the right direction. I was conflating the CDK constructs and aws_codepipeline_actions mainly because I wanted to use the GitHubV2 connection method and at first the aws_codepipeline_actions did not look to support the CodeStarConnectionsSourceAction. Deeper down in the documentation there is a caveat where you can use the CodeStarConnectionsSourceAction with GitHub but you have to specify action_name="Bitbucket_Source".

  • That's great news, Simon! I am glad you have things going in the correct direction! Happy coding!

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions