Skip to content

Commit e6bcec0

Browse files
committed
wip
1 parent 9a31434 commit e6bcec0

File tree

1 file changed

+152
-0
lines changed
  • content/en/user-guide/aws/codebuild

1 file changed

+152
-0
lines changed

content/en/user-guide/aws/codebuild/index.md

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,161 @@ tags: ["Pro image"]
88

99
## Introduction
1010

11+
AWS CodeBuild is a fully managed continuous integration service that compiles source code, runs tests, and produces software packages that are ready to deploy.
12+
It's part of the AWS Developer Tools suite and integrates with other AWS services to provide an end-to-end development pipeline.
13+
14+
LocalStack supports the emulation of most of the CodeBuild operations.
15+
The supported operations are listed on the [API coverage page]({{< ref "coverage_codebuild" >}}).
16+
17+
AWS CodeBuild emulation is powered by the [AWS CodeBuild agent](https://docs.aws.amazon.com/codebuild/latest/userguide/use-codebuild-agent.html).
1118

1219
## Getting Started
1320

21+
This tutorial will show you how to use AWS CodeBuild to test and build a deployable version of a Java executable.
22+
23+
It assumes basic knowledge of the [`awslocal`](https://github.com/localstack/awscli-local) wrapper, Apache Maven, and Java.
24+
25+
### Create the source code
26+
27+
In the first step, we have to create the project that we want to build with AWS CodeBuild.
28+
29+
In an empty directory, we need to re-create the following structure:
30+
31+
```bash
32+
root-directory-name
33+
├── pom.xml
34+
└── src
35+
├── main
36+
│   └── java
37+
│   └── MessageUtil.java
38+
└── test
39+
└── java
40+
└── TestMessageUtil.java
41+
```
42+
43+
Let us walk through these files.
44+
`MessageUtil.java` is the file implementing the logic of this small application.
45+
It does nothing more than print a salutation message.
46+
Copy the following content into the `src/main/java` directory.
47+
48+
```java
49+
public class MessageUtil {
50+
private String message;
51+
52+
public MessageUtil(String message) {
53+
this.message = message;
54+
}
55+
56+
public String printMessage() {
57+
System.out.println(message);
58+
return message;
59+
}
60+
61+
public String salutationMessage() {
62+
message = "Hi!" + message;
63+
System.out.println(message);
64+
return message;
65+
}
66+
}
67+
```
68+
69+
Every build needs some testing!
70+
Therefore, create the `TestMessageUtil.java` file in the `src/test/java` directory.
71+
72+
```java
73+
import org.junit.Test;
74+
import org.junit.Ignore;
75+
import static org.junit.Assert.assertEquals;
76+
77+
public class TestMessageUtil {
78+
79+
String message = "Robert";
80+
MessageUtil messageUtil = new MessageUtil(message);
81+
82+
@Test
83+
public void testPrintMessage() {
84+
System.out.println("Inside testPrintMessage()");
85+
assertEquals(message,messageUtil.printMessage());
86+
}
87+
88+
@Test
89+
public void testSalutationMessage() {
90+
System.out.println("Inside testSalutationMessage()");
91+
message = "Hi!" + "Robert";
92+
assertEquals(message,messageUtil.salutationMessage());
93+
}
94+
}
95+
```
96+
97+
This small suite simply verifies that the greeting message is built correctly.
98+
99+
Finally, we need a `pom.xml` file to instruct Maven about what to build and which artifact needs to be produced.
100+
Create this file at the root of your directory.
101+
102+
```xml
103+
<project xmlns="http://maven.apache.org/POM/4.0.0"
104+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
105+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
106+
<modelVersion>4.0.0</modelVersion>
107+
<groupId>org.example</groupId>
108+
<artifactId>messageUtil</artifactId>
109+
<version>1.0</version>
110+
<packaging>jar</packaging>
111+
<name>Message Utility Java Sample App</name>
112+
<dependencies>
113+
<dependency>
114+
<groupId>junit</groupId>
115+
<artifactId>junit</artifactId>
116+
<version>4.11</version>
117+
<scope>test</scope>
118+
</dependency>
119+
</dependencies>
120+
<build>
121+
<plugins>
122+
<plugin>
123+
<groupId>org.apache.maven.plugins</groupId>
124+
<artifactId>maven-compiler-plugin</artifactId>
125+
<version>3.8.0</version>
126+
</plugin>
127+
</plugins>
128+
</build>
129+
</project>
130+
```
131+
132+
With the following configuration, Maven will compile the `java` files into a executable jar and run the specified tests.
133+
134+
### Create the buildspec file
135+
136+
Now that we have our project set up, we need to create a `buildspec` file.
137+
A `buildspec` file is a collection of settings and commands, specified in YAML format, that tells AWS CodeBuild how to run a build.
138+
139+
Create this `buildspec.yml` file in the root directory.
140+
141+
```yaml
142+
version: 0.2
143+
144+
phases:
145+
install:
146+
runtime-versions:
147+
java: corretto11
148+
pre_build:
149+
commands:
150+
- echo Nothing to do in the pre_build phase...
151+
build:
152+
commands:
153+
- echo Build started on `date`
154+
- mvn install
155+
post_build:
156+
commands:
157+
- echo Build completed on `date`
158+
artifacts:
159+
files:
160+
- target/messageUtil-1.0.jar
161+
```
14162
15163
## Limitations
16164
165+
- CodeBuild currently only supports S3 as a code source.
166+
You can use AWS CodePipeline to integrate CodeBuild with a source code repository provider via CodeStarSourceConnection.
167+
- We only use one build
168+
- Talk to the host (pass via the host network)

0 commit comments

Comments
 (0)