MARTIN OMANDER: Hi, Aaron.
How are you doing?
AARON WANJALA: I'm doing great, thanks.
MARTIN OMANDER: I've been asked to start
building a new inventory tracking system on Cloud Run.
We use Cloud Run, which is serverless,
to reduce the Ops work needed.
The application will use Java and Spring Boot,
but I'm not sure how to get that started on Cloud Run.
AARON WANJALA: No worries, Martin.
It's actually pretty simple these days.
Let me show you.
[MUSIC PLAYING]
So remind me again, what kind of app were we building here?
MARTIN OMANDER: It's a pretty standard web
app with an HTML user interface and it
will connect to a database.
AARON WANJALA: All right.
Well, first things first, how about we get a simple service
created and deployed?
MARTIN OMANDER: Sounds like a good start to me.
AARON WANJALA: We'll start by making a new Google Cloud
Platform project for your app.
MARTIN OMANDER: All right.
And we can call it inventory-tracking-app.
AARON WANJALA: Perfect.
Once that's created, we can open up the Cloud Shell.
MARTIN OMANDER: Do we need to use the Cloud Shell for this,
though?
AARON WANJALA: No, that's entirely optional.
As long as you have the gcloud command-line tool installed,
you'll be able to do everything from your local machine
if you prefer to do that.
MARTIN OMANDER: Excellent.
Well, I will use the Cloud Shell to get started for convenience.
I'm used to using the wizard at start.spring.io to get
new Spring Boot applications.
AARON WANJALA: That should work great.
All we'll need is the web dependency.
It may be easier to use the command-line version of that
to get the ZIP file in your Cloud Shell environment.
I sent you the command.
Did you get it?
MARTIN OMANDER: Yep, I see it.
All right, let me run it.
Looks like I got a ZIP file with a starter project
like I'm used to from start.spring.io.
I'll just unzip it here and open the editor.
OK, all set.
What's next, Aaron?
AARON WANJALA: Well, that command
gave us a good starting point.
But we'll need something to give us a sign of life
once we're running this.
Let's add a Hello World endpoint to help us with that.
MARTIN OMANDER: Sure.
I'll just add a HelloController here.
It will return a placeholder inventory
count of a placeholder product for now,
and this is just so we can see that all parts of the system
are working.
Now to get started, how can we get this running
from the command line?
AARON WANJALA: We can actually run it the same way
you would on your machine.
Then we can hit the Preview button
in the top right of the Cloud Shell
to see it in your browser.
MARTIN OMANDER: Mhm.
There it is.
Very nice.
So the application is running from my command line.
I can use this for testing as I build it out.
Now how can we get this deployed to Cloud Run up in the cloud
so others can test the application?
AARON WANJALA: In the simplest case,
we can do it in one line with gcloud run deploy.
MARTIN OMANDER: Wow.
That is a short command.
But is there a way I can see what kind of magic
is happening behind the scenes here?
AARON WANJALA: Absolutely.
You'll be prompted for a few things
during the run deploy command, like providing the source code
location, providing the service name,
enabling the Cloud Run API, selecting your region,
and enabling Artifact Registry.
MARTIN OMANDER: Hmm, I see.
I did that.
Now I'm being asked if I want to enable unauthenticated requests
to this service?
AARON WANJALA: Yeah.
To make it easy to test here, we'll
want to allow unauthenticated requests.
After all that enabling is done, it'll
start the actual deploy process.
Now, a lot is going on behind the scenes of this command,
which you don't need to know, but you might find interesting.
What's happening during this step
is that Cloud Build zips and saves your source in a Cloud
Storage bucket, uses the Cloud Native Computing Foundation
Buildpacks in the background to build your image,
creates a registry to store the resulting container
image, if not already present, and creates a Cloud Run
service to host your app, if that's not already present.
MARTIN OMANDER: Wow.
And, just like that, with one single command, we're live, eh?
AARON WANJALA: Yes, we are.
MARTIN OMANDER: Well, that was pretty simple.
But for my app to get to production,
I'm going to need some tests.
And I'll want them to pass before I deploy.
AARON WANJALA: Fair enough.
We can get that going pretty quick, as well.
MARTIN OMANDER: OK, so why don't we
get started by adding a simple failing test?
The test checks if 1 plus 1 equals 3.
And it will fail, of course.
And then I will fix it.
And if all that works, I can add real tests later on.
AARON WANJALA: Great.
Once that test is done, we'll have
to inform Cloud Build that we want
to treat running those steps as their own step
and to stop if they fail.
MARTIN OMANDER: How much config are we
looking at for this, Aaron?
AARON WANJALA: One file and just a handful of lines.
I'll send you the Cloud Build YAML.
This file tells Cloud Build to run your tests,
install the project's Maven dependencies,
build the image using gcloud build submit
and the --pack flag to select Buildpacks,
and then deploy the resulting image to Cloud
Build with gcloud run deploy.
MARTIN OMANDER: OK, that makes sense.
Now, how do we use it to run the tests before deployment?
AARON WANJALA: Well, since we have a Cloud Build YAML file
present in our source directory, we
can just run gcloud build submit and it will look for that file
by default.
MARTIN OMANDER: Great.
Mhm.
And, as expected, it failed.
Good.
AARON WANJALA: Yeah.
Now we can fix it and rest assured
that we won't be pushing broken builds to prod down the line.
MARTIN OMANDER: Very good.
Next, I'd add my code to source control.
Then I'd like to automatically run the tests
and deploy to my Cloud Run test environment
whenever code is committed to the dev branch.
AARON WANJALA: Those sound like great next steps.
Let me show you how to do them in the next video.
MARTIN OMANDER: Thank you for showing me, Aaron.
And thank you, everyone, for watching.
If you have questions for me or Aaron,
please add them in the comments below.
Also, let us know if there are other topics you'd like
to hear about on this show.
Bye.
[MUSIC PLAYING]