MARTIN: Hey, Katie.
Thanks for jumping on the call.
KATIE: No worries, Martin.
I heard you needed help setting up your new website.
MARTIN: Yeah, I have this repo here, my-travels.
And all the code is here on GitHub.
But I want somewhere to run this code.
And I also wanted to set up so that when
I push a new version here to GitHub,
the new version gets deployed automatically to the cloud.
KATIE: Well, that's easy.
We can set that all up for in Cloud Run.
I can show you right now in your browser.
[MUSIC PLAYING]
So I see you've got your code there, all on GitHub.
And you're logged into Cloud Console, right?
MARTIN: Uh, on Google's Cloud Platform?
KATIE: Yeah.
MARTIN: Let me look.
Yep, and I even created an empty project here, my-travels.
I haven't done anything with it, except create it.
KATIE: OK, great.
So if you want to go over into the Cloud Run section
and create a new service, we can get started.
MARTIN: All right.
Very good.
Let's see.
Cloud Run is down here.
Actually, I'll pin it, as well, so that it's easy to get to.
So here we are in the Cloud Run section.
There's nothing here yet.
KATIE: Yeah, so if you just want to go and click Create service.
MARTIN: Create service.
OK.
Up here, very nice.
Enabling the Cloud Run API.
And what do I do here?
KATIE: So under Service name, you
said this was your travel website, so if you
want to name it My Travels.
MARTIN: Oh, OK.
Very good.
Cloud Run API has been enabled.
Very good.
my-travels.
OK.
KATIE: Yep.
MARTIN: Next.
Oh, container.
I don't think I have a container image.
KATIE: Well, that's OK.
You don't have to have one already.
What we can do is, if you see just below that Deploy
container, it has an option there saying,
Continuously deploy from source control.
MARTIN: Ah, yes, that's what I want.
KATIE: Yeah.
MARTIN: All right.
Set up with Cloud Build?
KATIE: So now you're going to want to--
yep.
So go and Set up with Cloud Build.
MARTIN: All right.
Very good.
And I guess I need to enable some APIs here.
KATIE: Yeah.
MARTIN: Oh, this is looking good.
It says GitHub.
This is what I want.
But no repositories available, Katie.
KATIE: Well, see that other big blue button,
Install Google Cloud Build?
You're going to need to install Cloud Build as part of GitHub.
So if you want to click that.
MARTIN: All right.
Click the big, blue friendly button?
KATIE: Yeah.
MARTIN: Yeah.
And here, so now it took me to GitHub.
And here, I want to do it specifically for my-travels.
Very good.
Read and write, I guess?
KATIE: Yeah.
MARTIN: All right.
Let's see if-- oh, look at that.
That's my GitHub repo.
Very nice.
I understand that members blah, blah, blah.
Yes.
Sounds good.
And Next, or Advanced Options here?
KATIE: Just click Next.
MARTIN: Next.
OK, good.
Very good.
OK, Build Configuration, Dockerfile, huh.
What's all this about?
KATIE: OK, so for the branch, you're using a main branch.
So you're going to want to make sure
that you-- yep, in that selection there,
it's worked out what your main branch is.
MARTIN: Oh, it figured that out.
Excellent.
KATIE: Yeah.
So what this part will do is, anytime you
push to your main branch, your service will be redeployed.
MARTIN: Oh, that's exactly what I want.
Let's see.
I don't have a Dockerfile.
KATIE: That's OK.
You don't need a Dockerfile.
What we can do is we can use Cloud Buildpacks instead.
MARTIN: Oh, what are Buildpacks?
KATIE: They're a standard way of being able to build
your container images.
You might be familiar with them if you've used
something like Heroku before.
Your codebase doesn't need to change,
but you will have to add one file.
MARTIN: OK, got it.
So root is the build directory?
KATIE: Yeah.
MARTIN: Yeah?
KATIE: But before we continue with this, Martin,
if you want to go back to your codebase,
we're going to need to add that one file.
MARTIN: Oh, OK.
Got it.
So here's my code.
I have my readme file here.
No Dockerfile or whatever.
The other files are not here that you mentioned.
KATIE: That's OK.
So I see in your readme, you've got exactly the command
that you need to run your application.
Yeah?
MARTIN: Right.
This is how I run my app.
KATIE: And you run this--
you run this after you install your dependencies, right?
MARTIN: Mhm.
That's right.
KATIE: OK.
So what we're going to need to do
is create a special file that will tell Buildpacks to run
that python3 flask run command.
MARTIN: OK, got it.
KATIE: So create a new file called
Procfile with a capital P.
MARTIN: All right.
So let's see here.
Procfile.
Yep.
KATIE: Yeah.
MARTIN: Does it have a file extension?
KATIE: Nope, just Procfile.
MARTIN: Just Procfile.
OK, here we are in the Procfile.
KATIE: And if you want to copy that command from your readme
into this Procfile.
MARTIN: Yeah.
KATIE: Yeah, and then at the start of that line,
type in web, colon, space.
MARTIN: Like so?
KATIE: Yeah.
So what this will do is, when the Buildpack runs,
it will try to find a Procfile, and then it
will execute the command that you've tagged as web.
Web, in this case, stands for the web service.
And this is the command it will run when you
want to start your web service.
We will need to add one other thing to this command,
and that's just a host and a port.
These are specific things that Cloud Run needs to run.
As part of what Cloud Run needs, it
says that I will run anything that you
put in a container that's serving on host 0.0.0.0
and listening on a port.
So after run, if you want to type --host.
MARTIN: Host, yep.
Got it.
KATIE: Yep.
=0.0.0.0.
MARTIN: OK, very good.
KATIE: Yeah.
And then a space, --port=$PORT
MARTIN: So that's an environment variable?
KATIE: Yeah.
So we could add these configurations
within your Flask app.py itself.
Or we can get the Procfile to run Flask
with these configurations for us.
MARTIN: Hm, seems to make sense to break it out
into its own file.
OK, I like this.
Was that it for the Procfile?
KATIE: Yeah.
So what you need to do is save this file,
and commit it to your repository,
and then push it up to GitHub.
MARTIN: Oh, OK.
Got it.
So Add Procfile is what I'll call it.
Commit that.
Make sure I push that up.
It's thinking about it.
OK.
Let's see here.
KATIE: If youi jump over, refresh.
MARTIN: Yeah, let's see if it made it up.
Oh, look at that.
Procfile.
KATIE: Yeah.
MARTIN: It made it.
KATIE: Awesome.
So that's the only change you need to make to your code.
So if you want to jump back over into Cloud Console,
we can continue with the set-up process.
MARTIN: Oh, that's right.
We didn't go through with the whole thing.
Yeah, what should I do here?
KATIE: All these defaults are fine,
so if you want to click Save.
MARTIN: OK.
Let's see, Where's Save?
Down there.
Mhm.
Cloud Trigger will be created once you create the service.
OK.
KATIE: Yeah, so if you scroll down a smidge.
MARTIN: Yeah.
KATIE: And click Next.
MARTIN: Oh, OK.
Next.
KATIE: And then this is your public website, right?
MARTIN: Yeah, that's right.
I want everybody to know about where I've traveled.
KATIE: Yep.
So you're going to want to select
Allow unauthenticated invocations.
So that just means that everyone can see your website.
MARTIN: Ah, OK.
KATIE: And then click Create.
MARTIN: All right.
KATIE: And then we can sit back and watch this all happen.
MARTIN: Oh, OK.
Creating, dot, dot, dot.
Uh-huh.
Oh, so Setting IAM policy.
OK, and then some other stuff going on here.
So what's going on right now?
KATIE: OK.
So because we didn't have a container to start off with,
what Cloud Run needs to do is deploy a service
with a placeholder container.
This container, if you end up seeing it,
means that it's a placeholder.
But also, it means that you have somewhere that your service can
sit if something goes wrong.
And the placeholder will have some debugging steps
if you need some help.
But what it's doing now is, after it's
created this Build Cloud Trigger, which
will fire this process any time your code changes, what
it's doing is it's building and deploying from repository.
So Buildpacks understand specific language setups,
like Python, and Java, and Go, and some others.
What it will do is it's worked out,
because you have a Python setup, it
will use a Python environment.
It will install your dependencies
that you have in your requirements.txt.
And then as the entry point on your container,
it will run that command that you specified in the Procfile.
This Buildpack will build the container for you,
and then that will be the container
that Cloud Run deploys.
So all you had to do was add that one command to get
this all started.
So Cloud Run will deploy anything that has a container,
but you don't have to use Docker to build that container.
You can use something like Buildpacks.
MARTIN: Ah, I see.
And yeah, Buildpacks maybe make it a little easier.
You don't have to know Docker to do this.
KATIE: You don't have to.
You just need to tell it-- you need to use a standard language
that it understands implicitly.
So it understands, for Java, how to install Java dependencies;
for Python, how to install Python dependencies.
But it doesn't know how to run your service, because there
are so many different ways that you could start a web server.
So that's why you need to tell it explicitly
when you want to start my web server,
here is the command that you want to run.
And in your case, it was run flask.
MARTIN: Oh, right.
That was-- oh, yes, this--
KATIE: Yeah.
MARTIN: --piece that I run otherwise.
Got it.
KATIE: Yeah.
MARTIN: All right.
And let's see, it's Pending.
KATIE: There we go.
MARTIN: Oh, so let's see.
So it is done building my code now?
KATIE: Yeah.
So if you want to click on that URL there that starts with
my-travels--
MARTIN: Oh, you even gave me a URL.
KATIE: --you'll have your website.
MARTIN: Nice.
Let's have a look.
Ah, look at that.
It is my travel website.
This is amazing.
So I really like this.
Now, what if I want to make updates to either the content,
or if I want to make updates to the code here?
What do I do?
KATIE: Well, now that we've set up that Cloud Build Trigger,
all you need to do to redeploy your website
is update the code in your main branch
on your remote repository.
So last time we did this, you were in the VS Code.
If you want to jump over into GitHub now,
we can make a small change and redeploy your website.
MARTIN: Oh, cool.
OK, so like you said, I could do in VS Code,
but let's do it over here.
So, yeah, I like how it says My Travels there.
But it's not quite--
I don't know, not quite personal enough.
But I want to make it a little more personal.
So what if I go into the home template here,
and I edit it, not from VS Code, but from GitHub directly.
Can do it in either place, I guess.
And I will say Martin's Travels.
And then OK, and here, it will loop over.
Very good.
Let's scroll down here.
I'll commit that change.
KATIE: You see that little yellow dot that's appeared?
MARTIN: Oh, yeah.
That's new.
What's that?
KATIE: That is the status symbol that your Cloud Build Trigger
is currently processing.
MARTIN: Ah.
KATIE: So if you click that, you can see more details.
MARTIN: Oh, right.
Details, OK.
KATIE: And then if you click on View more details,
it will take you back into the Google Cloud Console,
and will show your Running build log.
MARTIN: So here I'm seeing the build log as it's running.
OK.
New stuff is popping up in this window.
It's looking good so far.
No error messages, as far as I can see.
KATIE: No.
And you can see the steps there.
The first step is Buildpack, which builds the container.
The second one is push, which pushes the container.
And then the third one is deploy.
That will use that container and then redeploy your service.
And this is going to happen any time you update
the main branch on your repo.
So you can edit in VS Code and push it up.
You can edit it in the command line and push it up.
You can edit it directly in GitHub.
Anywhere that will be a commit on that main branch
of the remote repository.
So you have to remember to push your code for it to trigger.
But anytime you edit that code, this trigger
will redeploy everything.
MARTIN: And now I made an edit to a template here.
If I made an edit to Python code,
would it rebuild the Python code?
KATIE: It will rebuild everything
from your source repository.
MARTIN: Oh, it's done.
KATIE: Yeah.
So if you refresh your website, your change should be there.
MARTIN: All right.
Let's have a look.
Martin's Travels.
Great success.
Thank you for showing me, Katie.
This was really easy.
KATIE: No worries.
I hope you get to add more things to your travels soon,
but you can now update your website automatically
with Cloud Build and Cloud Run.
MARTIN: Excellent.
Thank you for watching.
If you have suggestions on what we should bring up
in future episodes, or if you have questions about what
we did here in this episode, please add them in the comments
below.
Bye.
[MUSIC PLAYING]