Skip to content

Commit fec547e

Browse files
authored
Add helloworld Cloud Run Sample (#2067)
* Add helloworld Cloud Run Sample * format pom * fix pom * fix docker * Add repackage
1 parent 1f90909 commit fec547e

File tree

8 files changed

+213
-3
lines changed

8 files changed

+213
-3
lines changed

run/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This directory contains samples for [Google Cloud Run](https://cloud.run). [Clou
1111

1212
| Sample | Description | Deploy |
1313
| ------------------------------- | ------------------------ | ------------- |
14-
|[Hello World][helloworld] | Quickstart | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_helloworld] |
14+
|[Hello World][helloworld/] | Quickstart | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_helloworld] |
1515
|[Cloud Pub/Sub](pubsub/) | Handling Pub/Sub push messages | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_pubsub] |
1616
|[System Packages](system-package/) | Using system packages with containers | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_sys_package] |
1717
|[Image Magick](image-processing/) | Event-driven image analysis & transformation | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30">][run_button_image] |
@@ -129,8 +129,7 @@ gcloud beta run deploy $SAMPLE \
129129
[run_docs]: https://cloud.google.com/run/docs/
130130
[run_build]: https://cloud.google.com/run/docs/building/containers
131131
[run_deploy]: https://cloud.google.com/run/docs/deploying
132-
[helloworld]: https://github.com/knative/docs/tree/master/docs/serving/samples/hello-world/helloworld-java-spring
133-
[run_button_helloworld]: https://deploy.cloud.run/?git_repo=https://github.com/knative/docs&dir=docs/serving/samples/hello-world/helloworld-java-spring
132+
[run_button_helloworld]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/helloworld
134133
[run_button_broken]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/hello-broken
135134
[run_button_image]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/image-processing
136135
[run_button_log]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/logging-manual

run/helloworld/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

run/helloworld/Dockerfile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Use the official maven/Java 8 image to create a build artifact.
2+
# https://hub.docker.com/_/maven
3+
FROM maven:3-jdk-11-slim as builder
4+
5+
# Copy local code to the container image.
6+
WORKDIR /app
7+
COPY pom.xml .
8+
COPY src ./src
9+
10+
# Build a release artifact.
11+
RUN mvn package -DskipTests
12+
13+
# Use AdoptOpenJDK for base image.
14+
# It's important to use OpenJDK 8u191 or above that has container support enabled.
15+
# https://hub.docker.com/r/adoptopenjdk/openjdk8
16+
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
17+
FROM adoptopenjdk/openjdk11:alpine-slim
18+
19+
# Copy the jar to the production image from the builder stage.
20+
COPY --from=builder /app/target/helloworld-*.jar /helloworld.jar
21+
22+
# Run the web service on container startup.
23+
CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/helloworld.jar"]

run/helloworld/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Cloud Run Hello World Sample
2+
3+
This sample shows how to deploy a Hello World [Spring Boot](https://spring.io/projects/spring-boot)
4+
application to Cloud Run.
5+
6+
For more details on how to work with this sample read the [Google Cloud Run Java Samples README](https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/run).
7+
8+
[![Run in Google Cloud][run_img]][run_link]
9+
10+
[run_img]: https://storage.googleapis.com/cloudrun/button.svg
11+
[run_link]: https://deploy.cloud.run/?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&dir=run/helloworld
12+
13+
## Dependencies
14+
15+
* **Spring Boot**: Web server framework.
16+
* **Junit**: [development] Test running framework.

run/helloworld/pom.xml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2019 Google LLC
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
-->
14+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
15+
<modelVersion>4.0.0</modelVersion>
16+
<groupId>com.example</groupId>
17+
<artifactId>helloworld</artifactId>
18+
<version>0.0.1-SNAPSHOT</version>
19+
<packaging>jar</packaging>
20+
21+
<!-- The parent pom defines common style checks and testing strategies for our samples.
22+
Removing or replacing it should not affect the execution of the samples in anyway. -->
23+
24+
<parent>
25+
<groupId>com.google.cloud.samples</groupId>
26+
<artifactId>shared-configuration</artifactId>
27+
<version>1.0.11</version>
28+
</parent>
29+
<dependencyManagement>
30+
<dependencies>
31+
<dependency>
32+
<!-- Import dependency management from Spring Boot -->
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-dependencies</artifactId>
35+
<version>2.2.2.RELEASE</version>
36+
<type>pom</type>
37+
<scope>import</scope>
38+
</dependency>
39+
</dependencies>
40+
</dependencyManagement>
41+
<properties>
42+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
43+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
44+
<maven.compiler.target>11</maven.compiler.target>
45+
<maven.compiler.source>11</maven.compiler.source>
46+
</properties>
47+
<dependencies>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-web</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-test</artifactId>
55+
<scope>test</scope>
56+
</dependency>
57+
</dependencies>
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-maven-plugin</artifactId>
63+
<executions>
64+
<execution>
65+
<goals>
66+
<goal>repackage</goal>
67+
</goals>
68+
</execution>
69+
</executions>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.helloworld;
18+
19+
import org.springframework.beans.factory.annotation.Value;
20+
import org.springframework.boot.SpringApplication;
21+
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
import org.springframework.web.bind.annotation.GetMapping;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
@SpringBootApplication
26+
public class HelloworldApplication {
27+
28+
@Value("${NAME:World}")
29+
String name;
30+
31+
@RestController
32+
class HelloworldController {
33+
@GetMapping("/")
34+
String hello() {
35+
return "Hello " + name + "!";
36+
}
37+
}
38+
39+
public static void main(String[] args) {
40+
SpringApplication.run(HelloworldApplication.class, args);
41+
}
42+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
server.port = ${PORT}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.helloworld;
18+
19+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
20+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
21+
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
26+
import org.springframework.boot.test.context.SpringBootTest;
27+
import org.springframework.test.context.junit4.SpringRunner;
28+
import org.springframework.test.web.servlet.MockMvc;
29+
30+
@RunWith(SpringRunner.class)
31+
@SpringBootTest
32+
@AutoConfigureMockMvc
33+
public class HelloworldApplicationTests {
34+
35+
@Autowired private MockMvc mockMvc;
36+
37+
@Test
38+
public void returnsHelloWorld() throws Exception {
39+
mockMvc.perform(get("/")).andExpect(status().isOk());
40+
}
41+
}

0 commit comments

Comments
 (0)