Skip to content

Commit

Permalink
fix(codebuild): Fixed build spec file format to return yaml (#13445)
Browse files Browse the repository at this point in the history
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
janario authored Mar 22, 2021
1 parent f2436bf commit fab93c6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-amplify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const amplifyApp = new amplify.App(this, 'MyApp', {
repository: '<repo>',
oauthToken: cdk.SecretValue.secretsManager('my-github-token')
}),
buildSpec: codebuild.BuildSpec.fromObject({ // Alternatively add a `amplify.yml` to the repo
buildSpec: codebuild.BuildSpec.fromObjectToYaml({ // Alternatively add a `amplify.yml` to the repo
version: '1.0',
frontend: {
phases: {
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-amplify/test/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('create an app connected to a GitHub repository', () => {
repository: 'aws-cdk',
oauthToken: SecretValue.plainText('secret'),
}),
buildSpec: codebuild.BuildSpec.fromObject({
buildSpec: codebuild.BuildSpec.fromObjectToYaml({
version: '1.0',
frontend: {
phases: {
Expand All @@ -34,7 +34,7 @@ test('create an app connected to a GitHub repository', () => {
// THEN
expect(stack).toHaveResource('AWS::Amplify::App', {
Name: 'App',
BuildSpec: '{\n \"version\": \"1.0\",\n \"frontend\": {\n \"phases\": {\n \"build\": {\n \"commands\": [\n \"npm run build\"\n ]\n }\n }\n }\n}',
BuildSpec: 'version: \"1.0\"\nfrontend:\n phases:\n build:\n commands:\n - npm run build\n',
IAMServiceRole: {
'Fn::GetAtt': [
'AppRole1AF9B530',
Expand Down
3 changes: 3 additions & 0 deletions packages/@aws-cdk/aws-codebuild/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ const project = codebuild.Project(stack, 'MyProject', {
});
```

If you'd prefer your buildspec to be rendered as YAML in the template,
use the `fromObjectToYaml()` method instead of `fromObject()`.

Because we've not set the `name` property, this example will set the
`overrideArtifactName` parameter, and produce an artifact named as defined in
the Buildspec file, uploaded to an S3 bucket (`bucket`). The path will be
Expand Down
25 changes: 25 additions & 0 deletions packages/@aws-cdk/aws-codebuild/lib/build-spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IResolveContext, Lazy, Stack } from '@aws-cdk/core';
import * as yaml_cfn from '@aws-cdk/yaml-cfn';

/**
* BuildSpec for CodeBuild projects
Expand All @@ -8,6 +9,15 @@ export abstract class BuildSpec {
return new ObjectBuildSpec(value);
}

/**
* Create a buildspec from an object that will be rendered as YAML in the resulting CloudFormation template.
*
* @param value the object containing the buildspec that will be rendered as YAML
*/
public static fromObjectToYaml(value: {[key: string]: any}): BuildSpec {
return new YamlBuildSpec(value);
}

/**
* Use a file from the source as buildspec
*
Expand Down Expand Up @@ -70,6 +80,21 @@ class ObjectBuildSpec extends BuildSpec {
}
}

/**
* BuildSpec that exports into YAML format
*/
class YamlBuildSpec extends BuildSpec {
public readonly isImmediate: boolean = true;

constructor(public readonly spec: {[key: string]: any}) {
super();
}

public toBuildSpec(): string {
return yaml_cfn.serialize(this.spec);
}
}

/**
* Merge two buildspecs into a new BuildSpec
*
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-codebuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"@aws-cdk/aws-secretsmanager": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/region-info": "0.0.0",
"@aws-cdk/yaml-cfn": "0.0.0",
"constructs": "^3.2.0"
},
"homepage": "https://github.com/aws/aws-cdk",
Expand All @@ -118,6 +119,7 @@
"@aws-cdk/aws-secretsmanager": "0.0.0",
"@aws-cdk/core": "0.0.0",
"@aws-cdk/region-info": "0.0.0",
"@aws-cdk/yaml-cfn": "0.0.0",
"constructs": "^3.2.0"
},
"engines": {
Expand Down
28 changes: 28 additions & 0 deletions packages/@aws-cdk/aws-codebuild/test/test.project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,34 @@ export = {
test.done();
},

'can use yamlbuildspec literal'(test: Test) {
// GIVEN
const stack = new cdk.Stack();

// WHEN
new codebuild.Project(stack, 'Project', {
buildSpec: codebuild.BuildSpec.fromObjectToYaml({
text: 'text',
decimal: 10,
list: ['say hi'],
obj: {
text: 'text',
decimal: 10,
list: ['say hi'],
},
}),
});

// THEN
expect(stack).to(haveResourceLike('AWS::CodeBuild::Project', {
Source: {
BuildSpec: 'text: text\ndecimal: 10\nlist:\n - say hi\nobj:\n text: text\n decimal: 10\n list:\n - say hi\n',
},
}));

test.done();
},

'must supply buildspec when using nosource'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit fab93c6

Please sign in to comment.