Skip to content

Commit

Permalink
moved app delivery building blocks to codepipeline-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Elad Ben-Israel committed Apr 2, 2019
1 parent a37b26c commit 76be772
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 162 deletions.
6 changes: 3 additions & 3 deletions packages/@aws-cdk/app-delivery/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ class MyAppPipeline extends Stack {
constructor(scope: Construct, id: string, props: StackProps) {
super(scope, id, props);
new DeploymentPipeline(this, 'Pipeline', {
new codepipelinePipeline(this, 'Pipeline', {
bootstrap: 'cdk-workshop',
stages: [
{
name: 'Deploy',
actions: [
new DeployStackAction({ stack: new WorkshopStack(app, 'workshop-stack'), admin: true }),
new DeployStackAction({ stack: new RandomStack(app, 'random-stack'), admin: true })
new CdkDeployAction({ stacks: [ new WorkshopStack(app, 'workshop-stack').name ], admin: true }),
new CdkDeployAction({ stack: [ new RandomStack(app, 'random-stack').name ], admin: true })
]
}
]
Expand Down
8 changes: 4 additions & 4 deletions packages/@aws-cdk/app-delivery/bootstrap-app/app.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import cdk = require('@aws-cdk/cdk');
import fs = require('fs');
import yaml = require('yaml');
import { Pipeline, PipelineProps } from '../lib/pipeline';
import { BootstrapPipeline, BootstrapPipelineProps } from './pipeline';

const config = readConfig();
const app = new cdk.App();
const stack = new cdk.Stack(app, 'cdk-pipelines');

for (const [ id, props ] of Object.entries(config)) {
new Pipeline(stack, id, props);
const stack = new cdk.Stack(app, `cdk-bootstrap-${id}`);
new BootstrapPipeline(stack, id, props);
}

interface Config {
[name: string]: PipelineProps
[name: string]: BootstrapPipelineProps
}

function readConfig(): Config {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import codebuild = require('@aws-cdk/aws-codebuild');
import codepipeline = require('@aws-cdk/aws-codepipeline');
import actions = require('@aws-cdk/aws-codepipeline-actions');
import s3 = require('@aws-cdk/aws-s3');
import secretsmanager = require('@aws-cdk/aws-secretsmanager');
import { CfnOutput, Construct, Secret } from '@aws-cdk/cdk';
import { BuildAction } from '../lib/build';
import { DeployAction } from '../lib/deploy';
import { Construct, SecretValue } from '@aws-cdk/cdk';

export interface PipelineProps {
export interface BootstrapPipelineProps {
/**
* Github oauth secrets manager ARN.
*/
Expand Down Expand Up @@ -70,8 +68,8 @@ export interface PipelineProps {
readonly version?: string;
}

export class Pipeline extends Construct {
constructor(scope: Construct, id: string, props: PipelineProps) {
export class BootstrapPipeline extends Construct {
constructor(scope: Construct, id: string, props: BootstrapPipelineProps) {
super(scope, id);

const sourcePrefix = 'https://github.com/';
Expand All @@ -81,53 +79,45 @@ export class Pipeline extends Construct {
const source = props.source.substr(sourcePrefix.length);
const [ owner, repo ] = source.split('/');

const oauth = new secretsmanager.SecretString(this, 'OauthTokenSecret', {
secretId: props.oauthSecret
});

const version = props.version || 'latest';
const branch = props.branch;
const publishBucket = new s3.Bucket(this, 'Publish', { versioned: true });
const objectKey = 'cloud-assembly.zip';

const sourceAction = new codepipeline.GitHubSourceAction({
const sourceAction = new actions.GitHubSourceAction({
actionName: 'Pull',
owner,
repo,
oauthToken: new Secret(oauth.stringValue),
oauthToken: SecretValue.secretsManager(props.oauthSecret),
outputArtifactName: 'Source',
branch
});

const buildAction = new BuildAction(this, 'BuildDeploy', {
const buildAction = new actions.CdkBuildAction(this, 'Build', {
sourceArtifact: sourceAction.outputArtifact,
workdir: props.workdir,
build: props.build,
environment: props.environment,
install: props.install,
version: props.version
}).action;

const publishBucket = new s3.Bucket(this, 'Publish', {
versioned: true
});

const objectKey = 'cloud-assembly.zip';
const deployAction = new actions.CdkDeployAction(this, 'Deploy', {
admin: true,
assembly: buildAction.assembly,
environment: props.environment,
stacks: props.stacks,
version: props.version,
exclusively: props.exclusively
});

const publishAction = new s3.PipelineDeployAction({
inputArtifact: buildAction.outputArtifact,
const publishAction = new actions.S3DeployAction({
inputArtifact: buildAction.assembly,
actionName: 'Publish',
bucket: publishBucket,
objectKey,
extract: false
});

const deployAction = new DeployAction({
admin: true,
assembly: buildAction.outputArtifact,
stacks: props.stacks,
version: props.version,
exclusively: props.exclusively
});

new codepipeline.Pipeline(this, 'Bootstrap', {
restartExecutionOnUpdate: true,
stages: [
Expand All @@ -138,21 +128,10 @@ export class Pipeline extends Construct {
]
});

const exportPrefix = `cdk-pipeline:${id}`;

new CfnOutput(this, 'PublishBucketName', {
value: publishBucket.bucketName,
export: `${exportPrefix}-bucket`
});

new CfnOutput(this, 'PublishObjectKey', {
value: objectKey,
export: `${exportPrefix}-object-key`
});

new CfnOutput(this, 'ToolchainVersion', {
value: version,
export: `${exportPrefix}-toolchain-version`
actions.CdkSourceAction.exportArtifacts(this, {
boostrapId: id,
bucketName: publishBucket.bucketName,
objectKey
});
}
}
2 changes: 1 addition & 1 deletion packages/@aws-cdk/app-delivery/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"dependencies": {
"@aws-cdk/aws-cloudformation": "^0.27.0",
"@aws-cdk/aws-codebuild": "^0.27.0",
"@aws-cdk/aws-codepipeline-api": "^0.27.0",
"@aws-cdk/aws-codepipeline-actions": "^0.27.0",
"@aws-cdk/aws-iam": "^0.27.0",
"@aws-cdk/aws-secretsmanager": "^0.27.0",
"@aws-cdk/cdk": "^0.27.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/app-delivery/test/cdk.pipelines.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cdk-workshop:
source: https://github.com/aws-samples/aws-cdk-intro-workshop
workdir: code/typescript
oauthSecret: arn:aws:secretsmanager:us-east-1:260708760616:secret:github-token-7nnF5O
oauthSecret: arn:aws:secretsmanager:us-east-1:585695036304:secret:github-token-B5IVBl
branch: pipeline
stacks:
- WorkshopPipeline
5 changes: 2 additions & 3 deletions packages/@aws-cdk/app-delivery/test/integ.bootstrap-only.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { App, Stack } from '@aws-cdk/cdk';
import { Pipeline } from '../lib/pipeline';
import { BootstrapPipeline } from '../bootstrap-app/pipeline';

const app = new App();

const stack = new Stack(app, 'integ-app-delivery-bootstrap-only');

new Pipeline(stack, 'pipeline', {
new BootstrapPipeline(stack, 'pipeline', {
source: 'https://github.com/aws-samples/aws-cdk-intro-workshop',
oauthSecret: 'arn:aws:secretsmanager:us-east-1:585695036304:secret:github-token-B5IVBl',
workdir: 'code/typescript',
branch: 'pipeline',
stacks: [ 'WorkshopPipeline' ]
});

8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-codepipeline-actions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,11 @@ lambdaAction.outputArtifact('Out2'); // returns the named output Artifact, or th
See [the AWS documentation](https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.html)
on how to write a Lambda function invoked from CodePipeline.
#### AWS CDK
TODO
* [ ] `CdkBuildAction`
* [ ] `CdkDeployAction`
* [ ] `CdkSourceAction`
25 changes: 16 additions & 9 deletions packages/@aws-cdk/aws-codepipeline-actions/lib/cdk/build.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import codebuild = require('@aws-cdk/aws-codebuild');
import codepipeline = require('@aws-cdk/aws-codepipeline');
import { Construct } from '@aws-cdk/cdk';
import { CodeBuildBuildAction } from '../codebuild/pipeline-actions';

export interface BuildActionProps {
export interface CdkBuildActionProps {
/**
* Working directory to run build command.
* @default - root directory of your repository
Expand All @@ -11,6 +12,7 @@ export interface BuildActionProps {

/**
* CodeBuild environment to use.
* @default - Node.js 10.1.0
*/
readonly environment?: codebuild.BuildEnvironment;

Expand All @@ -33,14 +35,15 @@ export interface BuildActionProps {
/**
* The source artifact to build from.
*/
sourceArtifact: codepipeline.Artifact;
readonly sourceArtifact: codepipeline.Artifact;
}

export class BuildAction extends Construct {
public readonly action: codebuild.PipelineBuildAction;

constructor(scope: Construct, id: string, props: BuildActionProps) {
super(scope, id);
/**
* A CodePipeline build action for building your CDK app.
*/
export class CdkBuildAction extends CodeBuildBuildAction {
constructor(scope: Construct, id: string, props: CdkBuildActionProps) {
const parent = new Construct(scope, id);

const workdir = props.workdir || '.';
const install = props.install || 'npx npm@latest ci';
Expand Down Expand Up @@ -71,15 +74,19 @@ export class BuildAction extends Construct {
}
};

const buildProject = new codebuild.PipelineProject(this, 'BuildDeploy', {
const buildProject = new codebuild.PipelineProject(parent, 'BuildDeploy', {
environment,
buildSpec
});

this.action = new codebuild.PipelineBuildAction({
super({
inputArtifact: props.sourceArtifact,
project: buildProject,
actionName: 'Build',
});
}

public get assembly() {
return this.outputArtifact;
}
}
Loading

0 comments on commit 76be772

Please sign in to comment.