Skip to content

Commit 6374071

Browse files
authored
Update Cloud Run Samples to use Jib (GoogleCloudPlatform#2027)
* Change run samples to use Jib * update readme * fix pom format * readme format * fix pom format * change kokoro to build jib * change kokoro to build jib * Add base image * Format * Add clean up step * Add region tags * remove beta command * Update ReadMes * Add region tag back * Update build names * add back cloud run button * Update run instructions * Update version * Update port * Add region tags and fix lint
1 parent bc55409 commit 6374071

File tree

20 files changed

+233
-396
lines changed

20 files changed

+233
-396
lines changed

.kokoro/tests/build_cloud_run.sh

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ set -eo pipefail
2121
function cleanup {
2222
set -x
2323
gcloud container images delete "${CONTAINER_IMAGE}" --quiet --no-user-output-enabled || true
24-
gcloud beta run services delete ${SERVICE_NAME} \
24+
gcloud run services delete ${SERVICE_NAME} \
2525
--platform=managed \
2626
--region="${REGION:-us-central1}" \
2727
--quiet --no-user-output-enabled
28+
mvn clean
2829
}
2930
trap cleanup EXIT
3031

@@ -41,12 +42,16 @@ export SAMPLE_VERSION="${KOKORO_GIT_COMMIT:-latest}"
4142
SUFFIX=${KOKORO_GITHUB_PULL_REQUEST_NUMBER:-${SAMPLE_VERSION:0:12}}
4243
export SERVICE_NAME="${SAMPLE_NAME}-${SUFFIX}"
4344
export CONTAINER_IMAGE="gcr.io/${GOOGLE_CLOUD_PROJECT}/run-${SAMPLE_NAME}:${SAMPLE_VERSION}"
45+
export SPECIAL_BASE_IMAGE="gcr.io/${GOOGLE_CLOUD_PROJECT}/imagemagick"
46+
BASE_IMAGE_SAMPLES=("image-processing" "system-packages")
4447

4548
# Build the service
4649
set -x
47-
gcloud builds submit --tag="${CONTAINER_IMAGE}" --quiet --no-user-output-enabled
4850

49-
gcloud beta run deploy "${SERVICE_NAME}" \
51+
mvn jib:build -Dimage="${CONTAINER_IMAGE}" \
52+
`if [[ "${BASE_IMAGE_SAMPLES[@]}" =~ "${SAMPLE_NAME}" ]]; then echo "-Djib.from.image=${SPECIAL_BASE_IMAGE}"; fi`
53+
54+
gcloud run deploy "${SERVICE_NAME}" \
5055
--image="${CONTAINER_IMAGE}" \
5156
--region="${REGION:-us-central1}" \
5257
--platform=managed \
@@ -56,10 +61,6 @@ gcloud beta run deploy "${SERVICE_NAME}" \
5661

5762
set +x
5863

59-
echo 'Cloud Run Links:'
60-
echo "- Logs: https://console.cloud.google.com/logs/viewer?project=${GOOGLE_CLOUD_PROJECT}&resource=cloud_run_revision%2Fservice_name%2F${SERVICE_NAME}"
61-
echo "- Console: https://console.cloud.google.com/run/detail/${REGION:-us-central1}/${SERVICE_NAME}/metrics?project=${GOOGLE_CLOUD_PROJECT}"
62-
6364
echo
6465
echo '---'
6566
echo

run/README.md

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,59 +21,68 @@ This directory contains samples for [Google Cloud Run](https://cloud.run). [Clou
2121

2222
For more Cloud Run samples beyond Java, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples).
2323

24+
## Jib
25+
26+
These samples use [Jib](https://github.com/GoogleContainerTools/jib) to
27+
build Docker images using common Java tools. Jib optimizes container builds
28+
without the need for a Dockerfile or having [Docker](https://www.docker.com/)
29+
installed. Learn more about [how Jib works](https://github.com/GoogleContainerTools/jib).
30+
2431
## Setup
2532

2633
1. [Set up for Cloud Run development](https://cloud.google.com/run/docs/setup)
2734

28-
2. Clone this repository:
35+
1. Clone this repository:
2936

3037
```
3138
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
3239
```
3340
34-
Note: Some samples in the list above are hosted in other repositories. They are noted with the symbol "➥".
35-
41+
1. In the samples's `pom.xml`, update the image field for the `jib-maven-plugin`
42+
with your Google Cloud Project Id:
43+
44+
```XML
45+
<plugin>
46+
<groupId>com.google.cloud.tools</groupId>
47+
<artifactId>jib-maven-plugin</artifactId>
48+
<version>2.0.0</version>
49+
<configuration>
50+
<to>
51+
<image>gcr.io/PROJECT_ID/SAMPLE_NAME</image>
52+
</to>
53+
</configuration>
54+
</plugin>
55+
```
3656
3757
## How to run a sample locally
3858
39-
1. [Install docker locally](https://docs.docker.com/install/)
40-
41-
2. [Build the sample container](https://cloud.google.com/run/docs/building/containers#building_locally_and_pushing_using_docker):
59+
1. [Build the sample container using Jib](https://github.com/GoogleContainerTools/jib):
4260
43-
```
44-
export SAMPLE=<SAMPLE_NAME>
45-
cd $SAMPLE
46-
docker build --tag $SAMPLE .
61+
```Bash
62+
mvn compile jib:dockerBuild
4763
```
4864
49-
3. [Run containers locally](https://cloud.google.com/run/docs/testing/local)
65+
1. [Run containers locally](https://cloud.google.com/run/docs/testing/local) by
66+
replacing `PROJECT_ID` and `SAMPLE_NAME` with your values.
5067
5168
With the built container:
5269
53-
```
54-
PORT=8080 && docker run --rm -p 8080:${PORT} -e PORT=${PORT} $SAMPLE
55-
```
56-
57-
Overriding the built container with local code:
58-
59-
```
60-
PORT=8080 && docker run --rm \
61-
-p 8080:${PORT} -e PORT=${PORT} \
62-
-v $PWD:/app $SAMPLE
70+
```Bash
71+
PORT=8080 && docker run --rm -p 9090:${PORT} -e PORT=${PORT} gcr.io/PROJECT_ID/SAMPLE_NAME
6372
```
6473
6574
Injecting your service account key for access to GCP services:
6675
67-
```
68-
# Set the name of the service account key within the container
69-
export SA_KEY_NAME=my-key-name-123
70-
71-
PORT=8080 && docker run --rm \
72-
-p 8080:${PORT} \
73-
-e PORT=${PORT} \
74-
-e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/${SA_KEY_NAME}.json \
75-
-v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/${SA_KEY_NAME}.json:ro \
76-
-v $PWD:/app $SAMPLE
76+
```Bash
77+
PORT=8080 && docker run \
78+
-p 9090:${PORT} \
79+
-e PORT=${PORT} \
80+
-e K_SERVICE=dev \
81+
-e K_CONFIGURATION=dev \
82+
-e K_REVISION=dev-00001 \
83+
-e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/[FILE_NAME].json \
84+
-v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/[FILE_NAME].json:ro \
85+
gcr.io/PROJECT_ID/SAMPLE_NAME
7786
```
7887
7988
* Use the --volume (-v) flag to inject the credential file into the container
@@ -83,35 +92,26 @@ For more Cloud Run samples beyond Java, see the main list in the [Cloud Run Samp
8392
* Use the --environment (-e) flag to set the `GOOGLE_APPLICATION_CREDENTIALS`
8493
variable inside the container
8594
95+
1. Open http://localhost:9090 in your browser.
96+
8697
Learn more about [testing your container image locally.][testing]
8798
8899
## Deploying
89100
90-
1. Set your GCP Project ID as an environment variable:
91-
```
92-
export GOOGLE_CLOUD_PROJECT=<PROJECT_ID>
93-
```
94-
95-
2. Choose to push your image to Google Container Registry or submit a build to
96-
Cloud Build:
97-
* Push the docker build to Google Container Registry:
98-
```
99-
docker tag $SAMPLE gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
100-
docker push gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
101-
```
102-
Learn more about [pushing and pulling images][push-pull].
103-
104-
* Submit a build using Google Cloud Build:
105-
```
106-
gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
107-
```
108-
109-
3. Deploy to Cloud Run:
110-
```
111-
gcloud beta run deploy $SAMPLE \
112-
--set-env-vars GOOGLE_CLOUD_PROJECT=${GOOGLE_CLOUD_PROJECT} \
113-
--image gcr.io/${GOOGLE_CLOUD_PROJECT}/${SAMPLE}
114-
```
101+
1. [Build the sample container using Jib](https://github.com/GoogleContainerTools/jib):
102+
103+
```
104+
mvn compile jib:build
105+
```
106+
107+
**Note**: Using the image tag `gcr.io/PROJECT_ID/SAMPLE_NAME` automatically
108+
pushes the image to [Google Container Registry](https://cloud.google.com/container-registry/).
109+
110+
1. Deploy to Cloud Run by replacing `PROJECT_ID` and `SAMPLE_NAME` with your values:
111+
112+
```bash
113+
gcloud run deploy --image gcr.io/PROJECT_ID/SAMPLE_NAME
114+
```
115115
116116
## Next Steps
117117
* See [building containers][run_build] and [deploying container images][run_deploy]

run/hello-broken/Dockerfile

Lines changed: 0 additions & 38 deletions
This file was deleted.

run/hello-broken/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ For more details on how to work with this sample read the [Google Cloud Run Java
1616
## Dependencies
1717

1818
* **Spark**: Web server framework.
19+
* **Jib**: Container build tool.
1920
* **Junit**: [development] Test running framework.

run/hello-broken/pom.xml

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,18 @@ limitations under the License.
6969
<!-- [START run_broken_service_build] -->
7070
<build>
7171
<plugins>
72+
<!-- [START run_broken_service_jib] -->
7273
<plugin>
73-
<artifactId>maven-compiler-plugin</artifactId>
74-
<version>3.8.0</version>
74+
<groupId>com.google.cloud.tools</groupId>
75+
<artifactId>jib-maven-plugin</artifactId>
76+
<version>2.0.0</version>
7577
<configuration>
76-
<source>11</source>
77-
<target>11</target>
78-
</configuration>
79-
</plugin>
80-
<plugin>
81-
<artifactId>maven-assembly-plugin</artifactId>
82-
<configuration>
83-
<archive>
84-
<manifest>
85-
<mainClass>com.example.cloudrun.App</mainClass>
86-
</manifest>
87-
</archive>
88-
<descriptorRefs>
89-
<descriptorRef>jar-with-dependencies</descriptorRef>
90-
</descriptorRefs>
78+
<to>
79+
<image>gcr.io/PROJECT_ID/hello-service</image>
80+
</to>
9181
</configuration>
9282
</plugin>
83+
<!-- [END run_broken_service_jib] -->
9384
</plugins>
9485
</build>
9586
<!-- [END run_broken_service_build] -->

run/image-processing/Dockerfile

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
1-
# Copyright 2019 Google LLC. All rights reserved.
2-
# Use of this source code is governed by the Apache 2.0
3-
# license that can be found in the LICENSE file.
4-
5-
# Use the official maven/Java 11 image to create a build artifact.
6-
# https://hub.docker.com/_/maven
7-
FROM maven:3.6.3-jdk-11 as builder
8-
9-
# Copy local code to the container image.
10-
WORKDIR /app
11-
COPY pom.xml .
12-
COPY src ./src
13-
14-
# Build a release artifact.
15-
RUN mvn package -DskipTests
1+
# Copyright 2019 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+
# https://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.
1614

15+
# [START run_imageproc_dockerfile]
1716
# Use AdoptOpenJDK for base image.
1817
# It's important to use OpenJDK 8u191 or above that has container support enabled.
1918
# https://hub.docker.com/r/adoptopenjdk/openjdk11
20-
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
2119
FROM adoptopenjdk/openjdk11:alpine-slim
2220

2321
# Install Imagemagick into the container image.
2422
# For more on system packages review the system packages tutorial.
2523
# https://cloud.google.com/run/docs/tutorials/system-packages#dockerfile
2624
RUN apk add --no-cache imagemagick
27-
28-
# Copy the jar to the production image from the builder stage.
29-
COPY --from=builder /app/target/image-processing-*.jar /image-processing.jar
30-
31-
# Run the web service on container startup.
32-
CMD ["java","-Dserver.port=${PORT}","-jar","/image-processing.jar"]
25+
# [END run_imageproc_dockerfile]

run/image-processing/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ For more details on how to work with this sample read the [Google Cloud Run Java
1414
* **Image Magick**: for image processing.
1515
* **@google-cloud/storage**: Google Cloud Storage client library.
1616
* **@google-cloud/vision**: Cloud Vision API client library.
17+
* **Jib**: Container build tool.
1718

1819
## Environment Variables
1920

run/image-processing/pom.xml

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ limitations under the License.
4646
<type>pom</type>
4747
<scope>import</scope>
4848
</dependency>
49-
<!-- // [START run_imageproc_dep_management] -->
49+
<!-- [START run_imageproc_dep_management] -->
5050
<dependency>
5151
<groupId>org.springframework.cloud</groupId>
5252
<artifactId>spring-cloud-gcp-dependencies</artifactId>
5353
<version>1.2.1.RELEASE</version>
5454
<type>pom</type>
5555
<scope>import</scope>
5656
</dependency>
57-
<!-- // [END run_imageproc_dep_management] -->
57+
<!-- [END run_imageproc_dep_management] -->
5858
</dependencies>
5959
</dependencyManagement>
6060
<dependencies>
@@ -72,7 +72,7 @@ limitations under the License.
7272
<artifactId>spring-boot-starter-test</artifactId>
7373
<scope>test</scope>
7474
</dependency>
75-
<!-- // [START run_imageproc_dep] -->
75+
<!-- [START run_imageproc_dep] -->
7676
<dependency>
7777
<groupId>com.google.code.gson</groupId>
7878
<artifactId>gson</artifactId>
@@ -97,22 +97,29 @@ limitations under the License.
9797
<artifactId>commons-io</artifactId>
9898
<version>2.6</version>
9999
</dependency>
100-
<!-- // [END run_imageproc_dep] -->
100+
<!-- [END run_imageproc_dep] -->
101101
</dependencies>
102102
<build>
103103
<plugins>
104104
<plugin>
105105
<groupId>org.springframework.boot</groupId>
106106
<artifactId>spring-boot-maven-plugin</artifactId>
107-
<version>2.2.2.RELEASE</version>
108-
<executions>
109-
<execution>
110-
<goals>
111-
<goal>repackage</goal>
112-
</goals>
113-
</execution>
114-
</executions>
115107
</plugin>
108+
<!-- [START run_imageproc_jib] -->
109+
<plugin>
110+
<groupId>com.google.cloud.tools</groupId>
111+
<artifactId>jib-maven-plugin</artifactId>
112+
<version>2.0.0</version>
113+
<configuration>
114+
<from>
115+
<image>gcr.io/PROJECT_ID/imagemagick</image>
116+
</from>
117+
<to>
118+
<image>gcr.io/PROJECT_ID/pubsub</image>
119+
</to>
120+
</configuration>
121+
</plugin>
122+
<!-- [END run_imageproc_jib] -->
116123
</plugins>
117124
</build>
118125
</project>

0 commit comments

Comments
 (0)