Skip to content
This repository was archived by the owner on Feb 15, 2024. It is now read-only.

Commit 399f111

Browse files
srijitmSrijit Mitra
andauthored
Adding Github support, refactoring (#41)
* Addin Github support, refactoring * Adding github sample * Fix for #35 * Adding instructions for Github integration Co-authored-by: Srijit Mitra <srijit@amazon.com>
1 parent 5bf6154 commit 399f111

File tree

9 files changed

+815
-725
lines changed

9 files changed

+815
-725
lines changed

architecture.png

4.49 KB
Loading

config/config.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
import configFile from "../project-config.json"
2121

2222
export enum TriggerType {
23-
CodeCommit = "CodeCommit"
23+
CodeCommit = "CodeCommit",
24+
GitHub = "GitHub"
2425
}
2526

2627
export enum Regions {
@@ -35,10 +36,12 @@ export enum StageName {
3536

3637
export type ProjectRepo = {
3738
pipelineName: string,
38-
ccRepoName: string,
39+
repository: string,
3940
branch: string,
4041
type: TriggerType,
41-
cron: string
42+
owner?: any,
43+
secret?: any,
44+
cron?: any
4245
}
4346

4447
export interface ProjectConfig {

docs/admin.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,52 @@ This will generate resources prefixed with ***acme-markets-roadrunner***
9595
- The sample provides a single TriggerType - CodeCommit. This can be extended to add Github, BitBucket etc.
9696
- An SNS Topic is generated for the pipeline. Subscribers receive notifications on status of each pipeline stage. Emails are sent by a Lambda function.
9797
- A Parameter Store parameter is generated which stores the semantic version and automatically increments (uses python semver library) on every successful build.
98-
- A Cron parameter can be set to trigger the pipeline on a schedule managed by CloudWatch.
98+
- A Cron parameter can be set to trigger the pipeline on a schedule managed by CloudWatch. (optional)
99+
- An AWS Secrets Manager secret storing the Github personal access token. (optional)
99100

100101
```text
101102
{
102103
pipelineName: string,
103104
ccRepoName: string,
104105
branch: string,
105106
type: TriggerType,
106-
cron: string
107+
cron: string,
108+
secret: string
107109
}
108110
```
109111

110-
Example:
112+
### GitHub Example
113+
114+
**Note** GitHub integration uses personal access tokens. See: [https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token)
115+
116+
In AWS Secrets Manager create a plain text secret where the value is your Github personal access token. In this example the secret is called ***github-token***
117+
118+
![New Secret](images/new_secret_01.png "New Secret")
119+
120+
121+
This sample will generate a pipeline called ***acme-markets-roadrunner-infra-eks-main***
122+
123+
```json
124+
{
125+
"pipelineName": "infra-eks",
126+
"repository": "acme-infra-eks",
127+
"branch": "main",
128+
"type": "GitHub",
129+
"owner": "srijitm",
130+
"secret": "github-token"
131+
}
132+
```
133+
134+
### CodeCommit Example
111135

112-
The following sample will generate a pipeline called ***acme-markets-roadrunner-rocket-powered-skates-master***
136+
This sample will generate a pipeline called ***acme-markets-roadrunner-rocket-powered-skates-master***
113137

114138
```json
115139
{
116140
"pipelineName": "rocket-powered-skates",
117141
"ccRepoName": "rocket-powered-skates",
118142
"branch": "master",
119143
"type": "CodeCommit",
120-
"cron": ""
121144
}
122145
```
123146

docs/images/new_secret_01.png

91.6 KB
Loading

lib/cicd-stack.ts

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ import ssm = require('@aws-cdk/aws-ssm')
2222
import lambda = require('@aws-cdk/aws-lambda')
2323
import s3deploy = require('@aws-cdk/aws-s3-deployment')
2424
import { Bucket } from '@aws-cdk/aws-s3'
25-
import { CodeCommitPipeline } from './pipelines/codecommit-pipeline'
25+
import { SimpleCicdPipeline } from './pipelines/simple-cicd-pipeline'
2626
import PipelineRole from './iam/pipeline-role';
2727

28-
import { ProjectRepo, TriggerType } from '../config/config';
28+
import { ProjectRepo } from '../config/config';
2929

3030
interface CicdStackProps extends cdk.StackProps {
3131
prefix: string,
@@ -44,10 +44,12 @@ export class CicdStack extends cdk.Stack {
4444
const artifactsBucket = Bucket.fromBucketName(this, 'artifactsBucket', artifactsBucketName.stringValue)
4545

4646
// Push assume-cross-account-role.env to S3
47+
let ts = Date.now()
4748
new s3deploy.BucketDeployment(this, 'DeployAssumeRole', {
4849
sources: [s3deploy.Source.asset('./scripts')],
4950
destinationBucket: artifactsBucket,
50-
destinationKeyPrefix: 'admin/cross-account'
51+
destinationKeyPrefix: 'admin/cross-account',
52+
metadata: { 'timestamp': ts.toString() }
5153
});
5254

5355
// Get Lambda email handler function
@@ -65,27 +67,16 @@ export class CicdStack extends cdk.Stack {
6567
const pipelineName = `${props.prefix}-${repo.pipelineName}-${repo.branch}`.replace(/\/|_/g, '-')
6668
const modulePipelineRole = new PipelineRole(this, `${pipelineName}PipelineRole`)
6769

68-
switch (repo.type) {
69-
case TriggerType.CodeCommit: {
70-
const repoName = repo.ccRepoName
71-
const repoBranch = repo.branch
72-
const cronTrigger = repo.cron
73-
74-
new CodeCommitPipeline(this, `${pipelineName}-pipeline`, {
75-
artifactsBucket,
76-
prefix: props.prefix,
77-
ssmRoot: props.ssmRoot,
78-
repoName,
79-
repoBranch,
80-
cronTrigger,
81-
pipelineName,
82-
modulePipelineRole,
83-
emailHandler,
84-
semverHandler
85-
})
86-
break;
87-
}
88-
}
70+
new SimpleCicdPipeline(this, `${pipelineName}-pipeline`, {
71+
artifactsBucket,
72+
prefix: props.prefix,
73+
ssmRoot: props.ssmRoot,
74+
repo: repo,
75+
pipelineName,
76+
modulePipelineRole,
77+
emailHandler,
78+
semverHandler
79+
})
8980
}
9081
}
9182
}

lib/pipelines/codecommit-pipeline.ts renamed to lib/pipelines/simple-cicd-pipeline.ts

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@
1818

1919

2020
import { Pipeline, Artifact } from '@aws-cdk/aws-codepipeline'
21-
import { Construct } from '@aws-cdk/core'
21+
import { Construct, SecretValue } from '@aws-cdk/core'
2222
import { IBucket } from '@aws-cdk/aws-s3'
2323
import {
2424
CodeBuildAction,
2525
CodeCommitSourceAction,
2626
ManualApprovalAction,
27-
LambdaInvokeAction
27+
LambdaInvokeAction,
28+
GitHubSourceAction
2829
} from '@aws-cdk/aws-codepipeline-actions'
2930
import config from '../../config/config'
30-
import { StageName } from '../../config/config';
31+
import { StageName, TriggerType, ProjectRepo } from '../../config/config';
3132
import CodeBuildRole from '../iam/code-build-role'
3233
import { DeployProject } from '../projects/deploy-project'
3334
import { Role } from '@aws-cdk/aws-iam'
@@ -40,28 +41,24 @@ import ssm = require('@aws-cdk/aws-ssm');
4041
import sns = require('@aws-cdk/aws-sns');
4142
import targets = require('@aws-cdk/aws-events-targets');
4243

43-
export interface CodeCommitPipelineProps {
44+
export interface SimpleCicdPipelineProps {
4445
artifactsBucket: IBucket
4546
prefix: string
4647
ssmRoot: string
47-
repoName: string
48-
repoBranch: string,
49-
cronTrigger?: string,
48+
repo: ProjectRepo
5049
pipelineName: string,
5150
modulePipelineRole: Role
5251
emailHandler: IFunction
5352
semverHandler: IFunction
5453
}
5554

56-
export class CodeCommitPipeline extends Pipeline {
57-
constructor(scope: Construct, id: string, props: CodeCommitPipelineProps) {
55+
export class SimpleCicdPipeline extends Pipeline {
56+
constructor(scope: Construct, id: string, props: SimpleCicdPipelineProps) {
5857
const {
5958
artifactsBucket,
6059
prefix,
6160
ssmRoot,
62-
repoName,
63-
repoBranch,
64-
cronTrigger,
61+
repo,
6562
pipelineName,
6663
modulePipelineRole,
6764
emailHandler,
@@ -76,6 +73,9 @@ export class CodeCommitPipeline extends Pipeline {
7673
...rest
7774
})
7875

76+
let repoName = repo.repository
77+
let repoBranch = repo.branch
78+
7979
// Provision SNS Topic for notifications
8080
const notificationTopic = new sns.Topic(this, 'Topic', {
8181
displayName: `${prefix}-${repoName}-${repoBranch}-cicd-topic` ,
@@ -90,28 +90,52 @@ export class CodeCommitPipeline extends Pipeline {
9090

9191
// Source Control Stage (CodeCommit)
9292
const sourceOutputArtifact = new Artifact('SourceArtifact')
93-
const codeCommitRepo = Repository.fromRepositoryName(
94-
scope,
95-
`${repoName}${repoBranch}CodeCommitRepo`,
96-
repoName
97-
)
98-
99-
codeCommitRepo.onCommit('OnCommit', {
100-
target: new targets.CodePipeline(this),
101-
branches: [`${repoBranch}`]
102-
})
93+
switch (repo.type) {
94+
case TriggerType.CodeCommit: {
95+
const codeCommitRepo = Repository.fromRepositoryName(
96+
scope,
97+
`${repoName}${repoBranch}CodeCommitRepo`,
98+
repoName
99+
)
100+
101+
codeCommitRepo.onCommit('OnCommit', {
102+
target: new targets.CodePipeline(this),
103+
branches: [`${repoBranch}`]
104+
})
105+
106+
const sourceAction = new CodeCommitSourceAction({
107+
repository: codeCommitRepo,
108+
branch: repoBranch,
109+
output: sourceOutputArtifact,
110+
actionName: 'Source'
111+
})
103112

104-
const sourceAction = new CodeCommitSourceAction({
105-
repository: codeCommitRepo,
106-
branch: repoBranch,
107-
output: sourceOutputArtifact,
108-
actionName: 'Source'
109-
})
113+
this.addStage({
114+
stageName: 'Source',
115+
actions: [sourceAction]
116+
})
117+
break
118+
}
119+
case TriggerType.GitHub: {
120+
const oauth = SecretValue.secretsManager(repo.secret)
110121

111-
this.addStage({
112-
stageName: 'Source',
113-
actions: [sourceAction]
114-
})
122+
const sourceAction = new GitHubSourceAction({
123+
actionName: 'Source',
124+
oauthToken: oauth,
125+
owner: repo.owner,
126+
repo: repoName,
127+
branch: repoBranch,
128+
output: sourceOutputArtifact,
129+
})
130+
131+
this.addStage({
132+
stageName: 'Source',
133+
actions: [sourceAction]
134+
})
135+
break
136+
}
137+
}
138+
115139

116140
// Building Stage
117141
const buildOutputArtifact = new Artifact('BuildArtifact')
@@ -221,11 +245,11 @@ export class CodeCommitPipeline extends Pipeline {
221245
}
222246
})
223247

224-
if (props.cronTrigger) {
248+
if (repo.cron) {
225249
const cwRule = new Rule(this, `${pipelineName}-cronTrigger`, {
226250
ruleName: `${pipelineName}-trigger`,
227251
enabled: true,
228-
schedule: Schedule.expression(`cron(${props.cronTrigger})`)
252+
schedule: Schedule.expression(`cron(${repo.cron})`)
229253
})
230254
cwRule.addTarget(new targets.CodePipeline(this))
231255
}

0 commit comments

Comments
 (0)