On this edition of Serverless Expeditions, we take a look at how to deploy a GitHub repo on GitHub, though Cloud Run's new "point-and-click" integration, no Dockerfile required.
Check out the video version of this blog post.
Say you've created a website for a travel blog, and you want to host it online. You've got your code on GitHub, and you have a README of instructions of how a friend could deploy a copy of your website: they have to have your programming language installed on their machine, install some package dependencies, then run a command to start a web server.
If you want to host your website, you need a way that you can run these steps any time you want to update your code. This process is known as Continuous Deployment: when you commit a change to code a process is started that takes the new version of your code, performs the prerequisite tasks, and then starts a web server of your new code.
Cloud Run, a serverless platform from Google Cloud, allows you to to bring any container to serve your website, no matter the programming language used inside, as long as it follows the Runtime Container contract: a web server must be listening for requests on
0.0.0.0, on port 8080 (or the configured
PORT environment variable).
So, if you have a website, how do you turn it into a container? You could write a Dockerfile, a set of instructions to build a container image based on your setup. But if you're using a common language like Python, Go, or Ruby, the instructions are going to be similar for many websites: copy your code into the container, install your dependencies, and start the web server.
You can setup your Cloud Run service through the Google Cloud Console using the new walkthrough integration with Cloud Build: you connect your GitHub repo with Cloud Run, and set up your website to be built any time your code updates.
However, instead of having to write a Dockerfile, you can now use Buildpacks, a set of common instructions for popular programming languages that help you build your code into a container image that Cloud Run can deploy.
If you're using a language like Go, Node.js, Python, Java or .NET Core, this will automatically be detected when building a container using Buildpacks. These common languages and their respective package installation processes are handled for you; all you need to do is provide the command to start your web server in a special file called Procfile.
In the example in the video, Martin is using Flask, a Python-based web framework. To start his application, he runs:
python3 -m flask run
So his Procfile will be:
web: python3 -m flask run
The "web" in this case means "web server", and is the special term Buildpack looks for in a Procfile to start the web server.
He should also configure his Flask app to listen on the expected IP and port. He can either add that to his app.run command, or in the Procfile itself as part of starting Flask:
web: python3 -m flask run --host 0.0.0.0 --port $PORT
As long as he adds this file to his codebase, he doesn't need to change anything else about his website to host it on Cloud Run.
Once configured, any commits made to the GitHub repo -- from the command line, VSCode, GitHub's website, or anywhere else -- are automatically detected and deployed.
To learn more about the concepts discussed in this video, read more:
- Container runtime contract | Cloud Run Documentation
- Continuous deployment from Git using Cloud Build
- GoogleCloudPlatform/buildpacks | GitHub.com
You can try some of the sample buildpack apps at https://github.com/GoogleCloudPlatform/buildpack-samples
Serverless Expeditions is a fun and cheeky video series that looks at what serverless means and how to build serverless apps with Google Cloud.