Skip to content

Instantly share code, notes, and snippets.

@misskecupbung
Last active December 8, 2024 06:42
Show Gist options
  • Select an option

  • Save misskecupbung/8c923e62629cfd6cd5043e7e809ee4df to your computer and use it in GitHub Desktop.

Select an option

Save misskecupbung/8c923e62629cfd6cd5043e7e809ee4df to your computer and use it in GitHub Desktop.
DevFest Bali 2024 - Cloud Run.md

Step 1 Initialize a Spring Boot App

Generate a new Spring Boot app with Spring Initializr.

curl https://start.spring.io/starter.tgz \
-d type=maven-project \
-d language=java \
-d dependencies=web \
-d baseDir=maven-cloud-run | tar -xzvf -
cd maven-cloud-run/

Change to the directory of the template app and build and run the app using Maven.

cd maven-cloud-run

 ./mvnw -DskipTests spring-boot:run

Step 2 Add a web controller

 nano src/main/java/com/example/demo/DemoApplication.java

...
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @GetMapping("/")
    public String saySomething() {
        return "Java app on Cloud Run, containerized by Jib!";
    }
}
...

Step 3 Rebuild and run the app.

Run command below

./mvnw spring-boot:run

Step 4 Containerize your app and publish to Container Registry

Activate the Container Registry API.

gcloud services enable artifactregistry.googleapis.com

Step 5 Containerize your app and publish to Container Registry

Run Jib to build a Docker image and publish to Container Registry.

./mvnw com.google.cloud.tools:jib-maven-plugin:3.3.1:build \
    -Dimage=gcr.io/$GOOGLE_CLOUD_PROJECT/kotlin-jib-cloud-run \
    -Djib.from.image=eclipse-temurin:17-jre
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment