aws / aws/aws-cdk

(pipelines): add example for How to use an existing Code Pipeline with a CDK Pipeline

Open
#16,488 4 comments 4 reactions 0 assignees View on GitHub
@aws-cdk/pipelines docs/inline documentation effort/small feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

## :question: General Issue

### The Question
The documentation of `@aws-cdk/pipelines` seems to suggest that a CDK pipeline can be added to an existing `@aws-cdk/aws-codepipeline/Pipeline`, using the `codePipeline` prop: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_pipelines.CodePipeline.html

```
codePipeline? Pipeline An existing Pipeline to be reused and built upon.
```

However, I am not able to get this to work and am experiencing multiple errors at the cdk synth step, depending on how I try to set it up. As far as I can tell there isn't really any documentation yet to cover this scenario.

Essentially, we are trying to create a pipeline that has a manual or automatic approval step BEFORE we start running the Synth step.

See: https://github.com/ChrisSargent/cdk-issues/blob/pipelines/lib/cdk-test-stack.ts and below:

```typescript
import * as cdk from "@aws-cdk/core";
import * as pipelines from "@aws-cdk/pipelines";
import * as codepipeline from "@aws-cdk/aws-codepipeline";
import * as codepipeline_actions from "@aws-cdk/aws-codepipeline-actions";

export class CdkTestStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const cdkInput = pipelines.CodePipelineSource.gitHub(
"ChrisSargent/cdk-issues",
"pipelines"
);

// Setup the code source action
const sourceOutput = new codepipeline.Artifact();
const sourceAction = new codepipeline_actions.GitHubSourceAction({
owner: "ChrisSargent",
repo: "cdk-issues",
branch: "pipelines",
actionName: "SourceAction",
output: sourceOutput,
oauthToken: cdk.SecretValue.secretsManager("git/ChrisSargent"),
});

const pipeline = new codepipeline.Pipeline(this, "Pipeline", {
stages: [
{
actions: [sourceAction],
stageName: "GitSource",
},
],
});

const cdkPipeline = new pipelines.CodePipeline(this, "CDKPipeline", {
codePipeline: pipeline,
synth: new pipelines.ShellStep("Synth", {
// Without input, we get: Error: CodeBuild action 'Synth' requires an input (and the pipeline doesn't have a Source to fall back to). Add an input or a pipeline source, coming from : https://github.com/aws/aws-cdk/blob/master/packages/@aws-cdk/pipelines/lib/codepipeline/_codebuild-factory.ts#L198
// With an input her, we get:Error: Validation failed with the following errors: Source actions may only occur in first stage
input: cdkInput,
commands: ["yarn install --frozen-lockfile", "npx cdk synth"],
}),
});

// Produces: Stage 'PreProd' must have at least one action
// pipeline.addStage(new MyApplication(this, "PreProd"));

// Produces: The given Stage construct ('CdkTestStack/PreProd') should contain at least one Stack
cdkPipeline.addStage(new MyApplication(this, "PreProd"));
}
}

class MyApplication extends cdk.Stage {
constructor(scope: cdk.Construct, id: string, props?: cdk.StageProps) {
super(scope, id, props);

console.log("Nothing to deploy");
}
}
```

So we seem to be caught in a catch 22 where we can't provide the correct inputs to the pipelines.

### Environment

- **CDK CLI Version:** 1.121.0 (build 026cb8f)
- **Module Version:** 1.120.0
- **Node.js Version:** v14.17.6
- **OS:** macOS
- **Language (Version):** TypeScript

### Other information
https://stackoverflow.com/questions/68800264/aws-cdk-pipelines-using-with-an-existing-codepipeline/68805960#68805960

Contributor guide

Open the contributing guide

Research direction

Start with the linked cdk-test-stack.ts example and the @aws-cdk/pipelines CodePipeline API documentation to understand the existing-pipeline scenario. Document a working example that includes an approval step before Synth, explains the required inputs, and shows the expected pipeline stages and successful synthesis.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, devops, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.