aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.5k stars 3.85k forks source link

pipelines: v2 multi-branch support #29704

Open andreprawira opened 5 months ago

andreprawira commented 5 months ago

Describe the feature

AWS has released pipeline v2 that supports deployment based on multi-branch triggers. However it does not yet exist in AWS. The feature that i'm requesting should be added for this module. I already mentioned this in another issue but it was not supported yet and people suggested me to create a feature request.

Use Case

We need this feature because often times we have to automate creating pipelines for deployment based on multiple branches and at the moment we cant do that with CDK. We ended up either manually changing the pipeline branch (which is a no-no) or create however many pipelines for those branches.

Proposed Solution

from aws_cdk import (
    Stack,
    Environment,
    aws_codebuild as codebuild,
    aws_iam as iam,
    pipelines as pipelines,
    aws_codepipeline as codepipeline,
    aws_codepipeline_actions as codepipeline_actions,
)

        source_output = codepipeline.Artifact()
        l2_pipeline = codepipeline.Pipeline(
            self,
            "l2-codepipeline",
            pipeline_type=codepipeline.PipelineType.V2,
            pipeline_name="l2-cdk-pipeline",
            cross_account_keys=True,
            triggers=[
                codepipeline.TriggerProps(
                    provider_type=codepipeline.ProviderType.CODE_STAR_SOURCE_CONNECTION,
                    git_configuration=codepipeline.GitConfiguration(
                        source_action=codepipeline_actions.CodeStarConnectionsSourceAction(
                            action_name="BitBucket_Source",
                            owner="Me",
                            repo="my-dummy-repo",
                            branch="main",
                            output=source_output,
                            connection_arn="my-dummy-codestar-connection-arn",
                        ),
                        push_filter=[
                            codepipeline.GitBranchFilter(
                                branch_includes=["main, feat/dns, chore/**"]
                            )
                        ],
                    ),
                )
            ],
        )

        # Define the pipeline
        pipeline = pipelines.CodePipeline(
            self,
            "l3-cdk-pipeline",
            code_pipeline=l2_pipeline,
            code_build_defaults=pipelines.CodeBuildOptions(
                build_environment=codebuild.BuildEnvironment(
                    build_image=codebuild.LinuxBuildImage.STANDARD_6_0,
                    privileged=True,
                    compute_type=codebuild.ComputeType.LARGE,
                ),
                partial_build_spec=codebuild.BuildSpec.from_object(
                    {
                        "env": {
                            "git-credential-helper": "yes",
                        }
                    }
                ),
            ),
            synth=pipelines.CodeBuildStep(
                "Synth",
                 input=pipelines.CodePipelineSource.connection(
                     repo_string="my-dummy-repo",
                     branch="main",
                     connection_arn="my-dummy-codestar-connection-arn",
                     code_build_clone_output=True,
                     trigger_on_push=True,
                 ),
                additional_inputs={
                    "ui-repository": pipelines.CodePipelineSource.connection(
                        repo_string="my-dummy-ui-repo",
                        branch="main",
                        connection_arn="my-dummy-codestar-connection-arn",
                        code_build_clone_output=True,
                        trigger_on_push=True,
                    ),
                },
                partial_build_spec=codebuild.BuildSpec.from_object(
                    {
                        "phases": {
                            "pre_build": {
                                "commands": [
                                    "ls -la",
                                ],
                            },
                        }
                    }
                ),
                commands=[
                    "npm install -g aws-cdk",
                    "pip install -r requirements.txt",
                    "npx cdk synth -c customer=$customer",
                ],
                env={"customer": props.infra.name},
                role_policy_statements=[
                    iam.PolicyStatement(
                        actions=["sts:AssumeRole"],
                        resources=["*"],
                        conditions={
                            "StringEquals": {
                                "iam:ResourceTag/aws-cdk:bootstrap-role": "lookup"
                            }
                        },
                    )
                ],
            ),
            self_mutation=True,
            use_change_sets=False,
            docker_enabled_for_synth=True,
            docker_enabled_for_self_mutation=True,
        )

maybe something like that

Other Information

No response

Acknowledgements

CDK version used

2.131.0 (build 92b912d)

Environment details (OS name and version, etc.)

Windows 10

andreprawira commented 5 months ago

@go-to-k @atali feature request created

atali commented 5 months ago

Can we also support triggers on the additional_inputs ?

go-to-k commented 5 months ago

Thanks for this issue.

The branch trigger in the codepipeline module will be up to the following PR and we should work after this is merged. At that time, if anyone wants to work on this issue, it is welcome to do so. If no one is available, I may take it.

https://github.com/aws/aws-cdk/pull/29127

tim-finnigan commented 5 months ago

Thanks for the feature request and for referencing the PR above. It looks like this issue also overlaps with https://github.com/aws/aws-cdk/issues/25946.

andreprawira commented 1 month ago

@tim-finnigan @go-to-k any idea if the pipelines module (not the code_pipeline module) will ever get a pipeline v2 support? i know the support exists for code_pipeline module, but we have the bulk of our code using pipelines module already and prefer not to refactor the whole thing

go-to-k commented 1 month ago

@andreprawira The PR for the V2 multi-branch triggers in the code_pipeleine module I mentioned above has been reverted and now another PR is underway. Depending on this PR, the way it is handled in the pipelines module will change, so we may not know anything yet.