Kotlin / Gradle

Deploy to Google Cloud Run

Deploy a Micronaut application to Google Cloud Run, a fully managed serverless platform for containerized applications.

Sergio del Amo
On this guide
In this section

Getting Started

In this guide, we will create a Micronaut application written in Kotlin.

We will deploy the application to Google Cloud Run.

Develop and deploy highly scalable containerized applications on a fully managed serverless platform.

What you will need

To complete this guide, you will need the following:

Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

Writing the Application

Create an application using the Micronaut Command Line Interface or with Micronaut Launch.

mn create-app example.micronaut.micronautguide --build=gradle --lang=kotlin
Note
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.

Controller

To create a microservice that responds with "Hello World", you first need a controller.

Create a controller:

src/main/kotlin/example/micronaut/HelloController.kt

Test

Create a test to verify that when you make a GET request to /hello you get Hello World as a response:

src/test/kotlin/example/micronaut/HelloControllerTest.kt

Google Cloud Platform

Signup for the Google Cloud Platform

Cloud SDK

Install the Cloud SDK CLI for your operating system.

Cloud SDK includes the gcloud command-line tool. Run the init command in your terminal:

gcloud init

Log in to your Google Cloud Platform:

gcloud auth login

Google Cloud Platform Project

Create a new project with a unique name (replace xxxxxx with alphanumeric characters of your choice):

gcloud projects create micronaut-guides-xxxxxx
Note
In GCP, project ids are globally unique, so the id you used above is the one you should use in the rest of this guide.

Change your project:

gcloud config set project micronaut-guides-xxxxxx

If you forget the project id, you can list all projects:

gcloud projects list

Enable billing

Ensure you have a billing account created, and if not create one via the Google Cloud Platform Console.

To get a list of your billing accounts, run:

 gcloud beta billing accounts list
ACCOUNT_ID            NAME                  OPEN   MASTER_ACCOUNT_ID
XXXXXX-XXXXXX-XXXXXX  My Billing Account    True

You can then attach a billing account to your project:

> gcloud beta billing projects link micronaut-guides-xxxxxxx --billing-account=XXXXXX-XXXXXX-XXXXXX
billingAccountName: billingAccounts/XXXXXX-XXXXXX-XXXXXX
billingEnabled: true
name: projects/micronaut-guides-xxxxxxx/billingInfo
projectId: micronaut-guides-xxxxxxx

Enable the Google Cloud Container Registry API

We need somewhere to store our docker images, so we need to enable the Google Cloud Container Registry API for your project via the Google Cloud CLI:

gcloud services enable containerregistry.googleapis.com

Docker Push

Push the Docker image of your application to Google Cloud Container Registry or to Google Cloud Artifact Registry.

Modify your Gradle build to configure the image name:

...
..
.
dockerBuild {
    images = ["gcr.io/micronaut-guides-xxxxxx/micronautguide:latest"]
}
./gradlew dockerPush

The previous URL uses the pattern gcr.io/micronaut-guides-xxxxxx/micronautguide:latest. Change it to use your project ID.

You get an output such as:

....
..
.
Pushing image 'gcr.io/micronaut-guides-xxxxxx/micronautguide:latest'.

Google Cloud Run Deploy

You can deploy to Google Cloud Run via the CLI. Use the value you configured in your build as the image argument’s value.

gcloud run deploy \
    --image=gcr.io/micronaut-guides-xxxxxx/micronautguide:latest \
    --platform managed \
    --allow-unauthenticated

You will see an output such as:

Service name (micronautguide):
Please specify a region:

...

 [22] us-central1
 [23] us-east1
 [24] us-east4

...

 [29] cancel
Please enter your numeric choice:  23

To make this the default region, run `gcloud config set run/region us-east1`.

Deploying container to Cloud Run service [micronautguide] in project [micronaut-guides-xxxxxx] region [us-east1]
 Deploying... Done.
 Creating Revision...
 Routing traffic...
 Setting IAM Policy...
Done.
Service [micronautguide] revision [micronautguide-00002-fat] has been deployed and is serving 100 percent of traffic.
Service URL: https://micronautguide-63kwrzytgq-ue.a.run.app

Running the Application

curl -i https://micronautguide-li3tercjmq-ue.a.run.app/hello
HTTP/2 200
content-type: text/plain
x-cloud-trace-context: 139f91d74bfe5d24a2770fca9abef1d7
date: Sat, 02 Oct 2021 07:18:52 GMT
server: Google Frontend
content-length: 11
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-T051=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"

Hello World

Cleanup

Deleting the Cloud Run Service

Use the command gcloud run service delete to delete the service.

gcloud run services delete micronautguide

Deleting the project

The easiest way to eliminate billing is to delete the project you created for the tutorial.

Warning

Deleting a project has the following consequences:

  • If you used an existing project, you’ll also delete any other work you’ve done in the project.

  • You can’t reuse the project ID of a deleted project. If you created a custom project ID that you plan to use in the future, you should delete the resources inside the project instead. This ensures that URLs that use the project ID, such as an appspot.com URL, remain available.

  • If you are exploring multiple tutorials and quickstarts, reusing projects instead of deleting them prevents you from exceeding project quota limits.

Via the CLI

To delete the project using the Cloud SDK, run the following command, replacing YOUR_PROJECT_ID with the project ID:

gcloud projects delete YOUR_PROJECT_ID

Via the Cloud Platform Console

In the Cloud Platform Console, go to the Projects page.

In the project list, select the project you want to delete and click Delete project. After selecting the checkbox next to the project name, click Delete project

In the dialog, type the project ID, and then click Shut down to delete the project.

Deleting or turning off specific resources

You can individually delete or turn off some of the resources that you created during the tutorial.

Next Steps

You will probably want to deploy to Google Cloud Run from your CI server. Micronaut Launch contains the github-workflow-google-cloud-run feature, which adds a GitHub Actions workflow that deploys an application to Google Cloud Run from Google Container Registry.

Read more about:

Help with the Micronaut Framework

The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.

License

Note
All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images).