Skip to content
This repository was archived by the owner on Nov 4, 2022. It is now read-only.

Commit 9ba756a

Browse files
committed
Building the pipeline with CDK
1 parent 0153001 commit 9ba756a

File tree

10 files changed

+140
-2
lines changed

10 files changed

+140
-2
lines changed

workshop/content/buildpipe/cdkinit/_index.en.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,9 @@ After a few seconds, our new CDK project should look like this:
3131

3232
![CdkInit](/images/chapter4/screenshot-cdk-init.png)
3333

34-
The main file that you will be interacting with is the `lib/pipeline-stack.ts`. And the entry point of your CDK project is `bin/pipeline.ts`. For this workshop, don't worry about the rest of the files and folders, although if you are curious, feel free to poke around the project structure.
34+
35+
### Project structure
36+
The main files that you will be interacting with are _lib/pipeline-stack.ts_ and _bin/pipeline.ts_. Don't worry about the rest of the files and folders for now. Open the `bin/pipeline.ts` file, which is your entry point to the CDK project, and change the name of the stack to `sam-app-cicd`:
37+
38+
![CdkEntryPoint](/images/chapter4/screenshot-bin-pipeline-ts.png)
39+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
+++
2+
title = "Pipeline as code"
3+
date = 2019-11-01T15:26:09-07:00
4+
weight = 22
5+
+++
6+
7+
Open the file `lib/pipeline-stack.ts` in your Cloud9 workspace. It is empty at the moment, but you will be adding code to describe and build your CI/CD pipeline in the following pages.
8+
9+
![CdkEmptyLib](/images/chapter4/screenshot-cdk-empty.png)
10+
11+
### Build the CDK project
12+
13+
Even though we haven't wrote any code yet, let's get familiar with how to build and deploy a CDK project, as you will be doing it multiple times in this workshop and you should get comfortable with the process. Start by building the project with the following command:
14+
15+
```
16+
cd ~/environment/sam-app/pipeline
17+
npm run build
18+
```
19+
20+
### Deploy a CDK project
21+
22+
After the build has finished, go ahead and deploy the pipeline project by using the CDK CLI:
23+
24+
```
25+
cdk deploy
26+
```
27+
28+
The output should look like the following:
29+
30+
![CdkDeploy](/images/chapter4/screenshot-cdk-deploy.png)
31+
32+
A new CloudFormation stack was created in your account, but because your CDK project is empty, the only resource that was created was an AWS::CDK::Metadata. If you check your [CloudFormation Console](https://console.aws.amazon.com/cloudformation/home), you will see the new stack and the metadata resource.
33+
34+
![CdkMetadata](/images/chapter4/screenshot-cdk-metadata.png)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
+++
2+
title = "Artifacts Bucket"
3+
date = 2019-11-01T15:26:09-07:00
4+
weight = 10
5+
+++
6+
7+
Every Code Pipeline needs an artifacts bucket, also known as Artifact Store. CodePipeline will use this bucket to pass artifacts to the downstream jobs and its also where SAM will upload the artifacts during the build process.
8+
9+
Let's get started and write the code for creating this bucket:
10+
11+
```js
12+
// lib/pipeline-stack.ts
13+
14+
import * as cdk from '@aws-cdk/core';
15+
import s3 = require('@aws-cdk/aws-s3');
16+
17+
export class PipelineStack extends cdk.Stack {
18+
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
19+
super(scope, id, props);
20+
21+
// The code that defines your stack goes here
22+
const artifactsBucket = new s3.Bucket(this, "ArtifactsBucket");
23+
}
24+
}
25+
```
26+
27+
Easy right? Now build and deploy the project like you did it earlier:
28+
29+
```
30+
npm run build
31+
cdk deploy
32+
```
33+
34+
The output will show that the S3 bucket got created:
35+
36+
![CdkBucket](/images/chapter4/screenshot-cdk-s3-bucket.png)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
+++
2+
title = "Source Stage"
3+
date = 2019-11-01T15:26:09-07:00
4+
weight = 15
5+
+++
6+
7+
The source stage is the first step of any CI/CD pipeline and it represents your source code. This stage is in charge of triggering the pipeline based on new code changes (i.e. git push or pull requests). In this workshop, we will be using AWS CodeCommit as the source provider, but CodePipeline also supports S3, GitHub and Amazon ECR as source providers.
8+
9+
Let's go ahead and add a Source provider to our pipeline:
10+
11+
```js
12+
// lib/pipeline-stack.ts
13+
14+
import * as cdk from '@aws-cdk/core';
15+
import s3 = require('@aws-cdk/aws-s3');
16+
import codecommit = require('@aws-cdk/aws-codecommit');
17+
import codepipeline = require('@aws-cdk/aws-codepipeline');
18+
import codepipeline_actions = require('@aws-cdk/aws-codepipeline-actions');
19+
20+
export class PipelineStack extends cdk.Stack {
21+
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
22+
super(scope, id, props);
23+
24+
// The code that defines your stack goes here
25+
const artifactsBucket = new s3.Bucket(this, "ArtifactsBucket");
26+
27+
// Import existing CodeCommit sam-app repository
28+
const codeRepo = codecommit.Repository.fromRepositoryName(
29+
this,
30+
'AppRepository', // Logical name within CloudFormation
31+
'sam-app' // Repository name
32+
);
33+
34+
// Pipeline creation starts
35+
const pipeline = new codepipeline.Pipeline(this, 'Pipeline', {
36+
artifactBucket: artifactsBucket
37+
});
38+
39+
// Declare source code as an artifact
40+
const sourceOutput = new codepipeline.Artifact();
41+
42+
// Add source stage to pipeline
43+
pipeline.addStage({
44+
stageName: 'Source',
45+
actions: [
46+
new codepipeline_actions.CodeCommitSourceAction({
47+
actionName: 'CodeCommit_Source',
48+
repository: codeRepo,
49+
output: sourceOutput,
50+
}),
51+
],
52+
});
53+
}
54+
}
55+
```
56+
57+
Since we already have the CodeCommit repository, we don't need to create a new one, we just need to import it using the repository name.
58+
59+
Also notice how we define an object `sourceOutput` as a pipeline artifact; This is necessary for any files that you want CodePipeline to pass to downstream stages. In this case, we want our source code to be passed to the Build stage.
60+
61+
{{% notice info %}}
62+
Don't do a `cdk deploy` just yet because you will get an error saying that a Pipeline needs to have at least 2 stages to be created. So, let's continue adding the Build stage in the next page.
63+
{{% /notice%}}

workshop/content/buildpipe/pipeline/_index.en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
+++
2-
title = "Creating the pipeline"
2+
title = "How to build a pipeline"
33
date = 2019-11-01T15:26:09-07:00
44
weight = 20
55
+++
111 KB
Loading
71.5 KB
Loading
132 KB
Loading
151 KB
Loading
94.5 KB
Loading

0 commit comments

Comments
 (0)