Skip to content

Commit 0612ac0

Browse files
authored
feat: Add codebuild cdk examples to create Project, ReportGroup… (#235)
* feat: Add codebuild cdk examples to create Project, ReportGroup and SourceCredentials This commit adds three CDK sample examples for the aws codebuild in java. It will help customers use the sample for better understanding of using CDK for codebuild.
1 parent c9e45a2 commit 0612ac0

File tree

15 files changed

+446
-0
lines changed

15 files changed

+446
-0
lines changed

java/codebuild/project/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Welcome to your CDK Java project!
2+
3+
It is a Maven-based project, so you can open this directory with any Maven-compatible Java IDE, and you should be able to build and run tests from your IDE.
4+
5+
You should explore the contents of this template. It demonstrates a CDK app to create a codebuild Project.
6+
A codebuild project includes information about how to run a build, including where to get the source code, which build environment to use, which build commands to run, and where to store the build output. In short, build project provides information to Code Build about how to build, as project contains all the information.
7+
To get more details on the properties and methods of a Project object, do look at https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-codebuild.Project.html
8+
9+
The cdk.json file tells the CDK Toolkit how to execute your app. This example relies on maven to do that.
10+
11+
## Useful commands
12+
13+
* `mvn package` compile and run tests
14+
* `cdk ls` list all stacks in the app
15+
* `cdk synth` emits the synthesized CloudFormation template
16+
* `cdk deploy` deploy this stack to your default AWS account/region
17+
* `cdk diff` compare deployed stack with current state
18+
* `cdk docs` open CDK documentation
19+
20+
Enjoy!

java/codebuild/project/cdk.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "mvn -e -q exec:java"
3+
}

java/codebuild/project/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.myorg</groupId>
7+
<artifactId>project</artifactId>
8+
<version>0.1</version>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
</properties>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.apache.maven.plugins</groupId>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<version>3.8.1</version>
20+
<configuration>
21+
<source>1.8</source>
22+
<target>1.8</target>
23+
</configuration>
24+
</plugin>
25+
26+
<plugin>
27+
<groupId>org.codehaus.mojo</groupId>
28+
<artifactId>exec-maven-plugin</artifactId>
29+
<version>1.6.0</version>
30+
<configuration>
31+
<mainClass>com.myorg.ProjectApp</mainClass>
32+
</configuration>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
37+
<dependencies>
38+
<!-- AWS Cloud Development Kit -->
39+
<dependency>
40+
<groupId>software.amazon.awscdk</groupId>
41+
<artifactId>core</artifactId>
42+
<version>[1.21.0, 2)</version>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>software.amazon.awscdk</groupId>
47+
<artifactId>codebuild</artifactId>
48+
<version>[1.21.0, 2)</version>
49+
</dependency>
50+
51+
<!-- https://mvnrepository.com/artifact/junit/junit -->
52+
<dependency>
53+
<groupId>junit</groupId>
54+
<artifactId>junit</artifactId>
55+
<version>4.12</version>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.myorg;
2+
3+
import software.amazon.awscdk.core.App;
4+
import software.amazon.awscdk.core.Environment;
5+
import software.amazon.awscdk.core.StackProps;
6+
7+
import java.util.Arrays;
8+
9+
public class ProjectApp {
10+
public static void main(final String[] args) {
11+
App app = new App();
12+
13+
new ProjectStack(app, "ProjectStack", StackProps.builder().env(
14+
Environment.builder()
15+
.account("projectStack")
16+
.region("us-west-2")
17+
.build()).build());
18+
19+
app.synth();
20+
}
21+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.myorg;
2+
3+
import software.amazon.awscdk.core.Construct;
4+
import software.amazon.awscdk.core.Duration;
5+
import software.amazon.awscdk.core.Stack;
6+
import software.amazon.awscdk.core.StackProps;
7+
import software.amazon.awscdk.services.codebuild.Artifacts;
8+
import software.amazon.awscdk.services.codebuild.ArtifactsProps;
9+
import software.amazon.awscdk.services.codebuild.BuildEnvironment;
10+
import software.amazon.awscdk.services.codebuild.BuildEnvironmentVariable;
11+
import software.amazon.awscdk.services.codebuild.BuildEnvironmentVariableType;
12+
import software.amazon.awscdk.services.codebuild.ComputeType;
13+
import software.amazon.awscdk.services.codebuild.ISource;
14+
import software.amazon.awscdk.services.codebuild.LinuxBuildImage;
15+
import software.amazon.awscdk.services.codebuild.Project;
16+
import software.amazon.awscdk.services.codebuild.S3SourceProps;
17+
import software.amazon.awscdk.services.codebuild.Source;
18+
import software.amazon.awscdk.services.iam.IRole;
19+
import software.amazon.awscdk.services.iam.Role;
20+
import software.amazon.awscdk.services.s3.Bucket;
21+
22+
import software.amazon.awscdk.services.s3.IBucket;
23+
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
27+
public class ProjectStack extends Stack {
28+
29+
public ProjectStack(final Construct scope, final String id) {
30+
this(scope, id, null);
31+
}
32+
33+
public ProjectStack(final Construct scope, final String id, final StackProps props) {
34+
super(scope, id, props);
35+
36+
BuildEnvironmentVariable buildEnvironmentVariable = new BuildEnvironmentVariable.Builder()
37+
.type(BuildEnvironmentVariableType.PLAINTEXT)
38+
.value("varValue")
39+
.build();
40+
Map<String, BuildEnvironmentVariable> environmentVariableMap = new HashMap<>();
41+
environmentVariableMap.put("varName", buildEnvironmentVariable);
42+
BuildEnvironment environment = BuildEnvironment.builder()
43+
.buildImage(LinuxBuildImage.AMAZON_LINUX_2_2)
44+
.computeType(ComputeType.SMALL)
45+
.environmentVariables(environmentVariableMap).build();
46+
47+
Artifacts artifacts = new Artifacts(ArtifactsProps.builder().build()) {
48+
@Override
49+
public String getType() {
50+
return "NO_ARTIFACTS";
51+
}
52+
};
53+
54+
IBucket s3Bucket = Bucket.fromBucketName(this, id, "s3BucketName");
55+
ISource source = Source.s3(S3SourceProps.builder().bucket(s3Bucket).path("S3Path").build());
56+
57+
IRole role = Role.fromRoleArn(this, "someId", "arn:partition:service:region:account-id:resource-type:resource-id");
58+
59+
Project.Builder.create(this, "SampleProject")
60+
.projectName("SampleProject")
61+
.description("Sample Project using AWS CDK")
62+
.role(role)
63+
.artifacts(artifacts)
64+
.environment(environment)
65+
.source(source)
66+
.timeout(Duration.minutes(10))
67+
.build();
68+
69+
}
70+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Welcome to your CDK Java project!
2+
3+
It is a Maven-based project, so you can open this directory with any Maven-compatible Java IDE, and you should be able to build and run tests from your IDE.
4+
5+
You should explore the contents of this template. It demonstrates a CDK app to create a codebuild ReportGroup cloud formation resource.
6+
A report group contains test reports and specifies shared settings. For each report group configured in a build project, a run of the build project creates a test report. Multiple runs of a build project configured with a report group create multiple test reports in that report group, each with results of the same test cases specified for that report group.
7+
The test cases are specified for a report group in the buildspec file of a build project. You can specify up to 5 report groups in one build project.
8+
To get more details on the properties and methods of a refor group, do look at https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-codebuild.CfnReportGroup.html
9+
10+
The cdk.json file tells the CDK Toolkit how to execute your app. This example relies on maven to do that.
11+
12+
## Useful commands
13+
14+
* `mvn package` compile and run tests
15+
* `cdk ls` list all stacks in the app
16+
* `cdk synth` emits the synthesized CloudFormation template
17+
* `cdk deploy` deploy this stack to your default AWS account/region
18+
* `cdk diff` compare deployed stack with current state
19+
* `cdk docs` open CDK documentation
20+
21+
Enjoy!
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"app": "mvn -e -q exec:java"
3+
}

java/codebuild/reportgroup/pom.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.myorg</groupId>
7+
<artifactId>reportgroup</artifactId>
8+
<version>0.1</version>
9+
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
</properties>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.apache.maven.plugins</groupId>
18+
<artifactId>maven-compiler-plugin</artifactId>
19+
<version>3.8.1</version>
20+
<configuration>
21+
<source>1.8</source>
22+
<target>1.8</target>
23+
</configuration>
24+
</plugin>
25+
26+
<plugin>
27+
<groupId>org.codehaus.mojo</groupId>
28+
<artifactId>exec-maven-plugin</artifactId>
29+
<version>1.6.0</version>
30+
<configuration>
31+
<mainClass>com.myorg.ReportgroupApp</mainClass>
32+
</configuration>
33+
</plugin>
34+
</plugins>
35+
</build>
36+
37+
<dependencies>
38+
<!-- AWS Cloud Development Kit -->
39+
<dependency>
40+
<groupId>software.amazon.awscdk</groupId>
41+
<artifactId>core</artifactId>
42+
<version>[1.21.0, 2)</version>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>software.amazon.awscdk</groupId>
47+
<artifactId>codebuild</artifactId>
48+
<version>[1.21.0, 2)</version>
49+
</dependency>
50+
51+
<!-- https://mvnrepository.com/artifact/junit/junit -->
52+
<dependency>
53+
<groupId>junit</groupId>
54+
<artifactId>junit</artifactId>
55+
<version>4.12</version>
56+
<scope>test</scope>
57+
</dependency>
58+
</dependencies>
59+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.myorg;
2+
3+
import software.amazon.awscdk.core.App;
4+
import software.amazon.awscdk.core.Environment;
5+
import software.amazon.awscdk.core.StackProps;
6+
7+
import java.util.Arrays;
8+
9+
public class ReportgroupApp {
10+
public static void main(final String[] args) {
11+
App app = new App();
12+
13+
new ReportgroupStack(app, "ReportgroupStack", StackProps.builder().env(
14+
Environment.builder()
15+
.account("ReportgroupStack")
16+
.region("us-west-2")
17+
.build()).build());
18+
19+
app.synth();
20+
}
21+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.myorg;
2+
3+
import software.amazon.awscdk.core.Construct;
4+
import software.amazon.awscdk.core.Stack;
5+
import software.amazon.awscdk.core.StackProps;
6+
import software.amazon.awscdk.services.codebuild.CfnReportGroup;
7+
import software.amazon.awscdk.services.codebuild.CfnReportGroupProps;
8+
9+
public class ReportgroupStack extends Stack {
10+
public ReportgroupStack(final Construct scope, final String id) {
11+
this(scope, id, null);
12+
}
13+
14+
public ReportgroupStack(final Construct scope, final String id, final StackProps props) {
15+
super(scope, id, props);
16+
17+
CfnReportGroup.S3ReportExportConfigProperty s3ReportExportConfigProperty
18+
= CfnReportGroup.S3ReportExportConfigProperty.builder()
19+
.bucket("S3BucketName")
20+
.path("S3Path")
21+
.encryptionDisabled(true)
22+
.packaging("ZIP")
23+
.build();
24+
25+
CfnReportGroup.ReportExportConfigProperty exportConfigProperty
26+
= CfnReportGroup.ReportExportConfigProperty.builder()
27+
.exportConfigType("S3")
28+
.s3Destination(s3ReportExportConfigProperty)
29+
.build();
30+
31+
CfnReportGroupProps reportGroupProps = CfnReportGroupProps.builder()
32+
.exportConfig(exportConfigProperty)
33+
.name("ReportGroupName")
34+
.type("TEST")
35+
.build();
36+
37+
new CfnReportGroup(this, id, reportGroupProps);
38+
}
39+
}

0 commit comments

Comments
 (0)