Running Java App using Google Cloud Run
Tags: java, vaadin, googlecloudrun
Published: 2024-02-21 21:05:00

---
title: Running Java App using Google Cloud Run
published: true
description: How to deploy a Java app to Google Cloud Run directly from GitHub without local tools. 
tags: java, vaadin, googlecloudrun
series: Creating an NPS Feedback App
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opt21hxwahbhzs6ffer2.png
# Use a ratio of 100:42 for best results.
published_at: 2024-02-21 21:05 +0000
---

In our journey we've created a Net Promoter Score (NPS) widget and application that saves data to a Google Spreadsheet. This time, we take the app to the cloud.

Since we already used Google Sheets for storage, it felt natural to use Google Cloud Run for deployment. It offers a fully managed platform with enough of resources that allows applications to run in containers accessible via the web. Additionally, when using the Cloud Console, we do not need to install any local tools, streamlining the deployment process.

##Clone, Build, Deploy
Deploying Java applications in the cloud has been a challenge. However, recent advancements have greatly simplified this process. Utilizing build packs or a Dockerfile, we can build and deploy Maven applications quickly. 

**Step 1:** Navigate to [shell.cloud.google.com](https://shell.cloud.google.com/) to access Google Cloud's integrated shell environment. This is a full-featured IDE in your browser.

**Step 2:** Clone the NPS project from GitHub [github.com/samie/nps](https://github.com/samie/nps). This repository contains a Vaadin add-on itself, but also the demo application project we want to deploy.

**Step 3:** If your project lacks a Dockerfile, you need to add one. You can use Java build packs, but a [two-phase Dockerfile](https://github.com/samie/nps/blob/v24/demo/Dockerfile) gives you a more definitive build. This first downloads all dependencies before creating your Vaadin application's production build.

![Cloud Run automatically finds the Docerfiles in the project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2ssxevl6h8dvfze511t.png).

**Step 4:** Build and deploy to Cloud Run. Select the correct Dockerfile, in this case, demo/Dockerfile, and initiate the build. Deployment build may take several minutes (typically 3 to 4 minutes).

Upon successful deployment, you will encounter an error related to Google Sheets access:

![java.lang.RuntimeException due to missing credentials.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qzpwitcr9gopy58nju4k.png){ width=60% }

This was expected. We need the credentials to access the Google Sheet.

##Configuring Secrets
Managing environment variables and secret files in cloud environments can be tricky. Google provides a "Secret Manager" tool where you can safely manage your application-specific secrets. 

![Create new secret for service account credentials](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7u2l20exp5prnqydclbw.png)

**Step 1:** Create a Secret
Create a new secret containing the service account credentials to access the Google Sheet at [console.cloud.google.com/security/secret-manager](https://console.cloud.google.com/security/secret-manager). If you already created a key for the Service Account, you can paste the content of that file, or you can create a new key at [console.cloud.google.com/iam-admin/serviceaccounts](https://console.cloud.google.com/iam-admin/serviceaccounts) and choose tab "Keys" in the correct service account. 

**Step 2:** Make Secrets Available
Make these secrets available to your deployed containers by creating a special "secrets" volume under the "Volumes" tab. Then, mount this volume to /local on the container through the "Containers" tab.

**Step 3:** Use secrets in the code
Adjust your application code (in this case, in [NPSView.java](https://github.com/samie/nps/blob/v24/demo/src/main/java/com/example/application/views/feedback/NPSView.java#L80)) to check for the proper credentials file location at `/local/<secret_name>`. 

With the secrets configured and your application adjusted, click "Deploy" to finalize the process. Your application should now be operational!

![Application successfully running](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xz8t9mgznyqg96u1tw7l.png)

The NPS feedback view is running at [nps-u342s5ewoq-uc.a.run.app/](https://nps-u342s5ewoq-uc.a.run.app/)
And the embedded page sample at [nps-u342s5ewoq-uc.a.run.app/example.html](https://nps-u342s5ewoq-uc.a.run.app/example.html)

##Conclusion

I was captivated by the simplicity of cloud-only tooling. Building directly from GitHub in browser IDE without local installations was refreshingly straightforward. The most challenging part was ensuring secure access to sensitive files, and I was a bit disappointed with how little synergy GCP offered for my architecture. Nevertheless, once you navigate its terminology and enable all the needed services, Google Cloud Platform provides robust tools to build anything.

![Java application up and running in cloud with session affinity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uplwnt3ppmzw92ixjhyh.png)

This marks the end of the series. It was initially a set of personal memos, from crafting a custom Vaadin widget, calling APIs, and integrating cloud services. But it grew into this guide, the path of deploying Java-only applications in the cloud. I hope you enjoyed the journey as much as I did.I thank you for joining this journey. 

Happy coding, and let me know what you built!