This βHow Toβ is not recommended for beginners, therefore, I assume that:
Here is what weβre going to set up:
Letβs get started!
Firstly let's talk a little bit more about Cloud Build. As I already told you, Cloud Build is (a very cool) serverless CI/CD platform.
Cloud Build will allow us to create complete workflows to quickly build our code, containerize it (or package source to a non-container artifacts) and deploy it to a VM, k8s or serverless for instance.
It basically works with something called "Triggers" which contains the configuration on how Cloud Build will be triggered. We could say that our triggers represent pipelines.
For instance, in a trigger's configuration you'll have the following things:
Now, stop talking and let's build π
For this How To, I'll deploy a simple "hello world" NodeJS app. At the moment, I have the following files in my repository:
Well, the first thing to do is to link our Github repository in Cloud Build in order to trigger any event from it. To do this:
We are going to create a GCP artifact registry repository in order to store our app 's docker images. To do so:
In case you got an error like Error: Error creating Repository: googleapi: Error 403: Artifact Registry API has not been used in project XXXXXXXXXXX before or it is disabled while creating resources with terraform , it means that you need to enable the Artifact Registry API in order to create your repository.
So, either you can enable the API by going to the GCP console, or you can do it using terraform, like this:
Our first trigger will be pretty simple:
It will be executed when a push event to master branch occurs and will do everything described in the cloudbuild.yaml from our GitHub repository. We will also create this file immediately!
You can put the following file at the repository' root:
We can see two fields here:
At the moment, the step introduced in our freshly created cloudbuild.yaml file will just display a pretty "hello world" in the build's logs on the GCP console.We'll update this file later on the Cloud Run section π
Well, now we have our GitHub repository linked to Cloud Build, an artifact registry repository ready to welcome our app's docker images, a Cloud Build trigger, and finally a cloudbuild.yaml file, we are able to test our pipeline π
We will now make sure that our pipeline is working. To do this, we simply need to commit & push on the master branch of our GitHub repository. In my case, I just committed and pushed the newly created cloudbuild.yaml file.
Once you pushed something on your GitHub repository, from the History section of the Cloud Build console, you should see something like this:
Now, if you click on the build's ID link, you should see our "hello world" message:
We successfully ensured that our pipeline is working correctly.Now, all we have to do is to build and deploy our NodeJS backend running on Cloud Run! π€
Cloud Run is a fully managed serverless platform that allows you to run stateless containers reachable via web requests (or Pub/Sub events). Cloud Run abstract the entire infrastructure so it let you focus on your app development.
In the previous sections, we saw how to use Cloud Build to deploy a simple pipeline that displays an "hello world" message in the build's logs.
Now, what we want is to build and deploy our NodeJS backend on Cloud Run using Cloud Build.
As you may already understand, we'll have edit the cloudbuild.yaml file in order to create following pipeline's steps:
Let's do this!
In order to build a docker image, we first need to create a Dockerfile in our GitHub repository that will be used by the docker build command:
I'll not explain Dockerfile file each lines since it is really easy to understand π
Well, now, we have to edit the cloudbuild.yaml file in order to add the docker image build step:
Woaaww wait! What are these variables?! π±
We can see two kind of variables here:
Therefore, we need to update our trigger adding these substitution variables. To do this, will have to edit the trigger.tf file:
My _REGISTRY variable refers to the artifact registry repository terraform resource and for the _REGISTRY_URL variable, you can check the repository URL from the Artifact Regsitry console.
If you want more information about Cloud Build's substitution variables, you can check the official documentation.
Once your freshly updated terraform manifest applied, if you go to the Triggers section of the Cloud Build console and click on the trigger that we've just updated, you should see your substitution variables:
Ok, we know how to build our application docker image, but now if we want to run our application using Cloud Run, we'll need to push it to our artifact registry repository. To do so, add the following step to your cloudbuild.yaml file:
We'll wait until the end of step 3 before pushing the cloudbuild.yaml file.
We're almost done! Let's add the last step to our cloudbuild.yaml file:
As you may see, we have to add a few substitution variables to our trigger! Let's add them:
Also, we'll need to ensure that the Cloud Run API is enabled, and that the Cloud Build service account (automatically created) has sufficient rights to use Cloud Run. To to this, let's edit the trigger.tf file consequently:
It's time to push! But be careful... Before pushing the final cloudbuild.yaml file to your GitHub repository, the trigger need to be updated so do not forget to apply your terraform manifest!
I feel like we're ready to run our application on Cloud Run!! π€©π
I assume that you already pushed the final version of the cloudbuild.yaml file.
The very first thing to check after pushing everything is the build status. If the build successful, you should see this from the History section of the Cloud Build console:
Well, it's a good start! Let's take a look at the Cloud Run console now:
It seems like our application is running but to be sure, we still need to check that the backend is working correctly.
To retrieve the app's URL, just click on the app name from the Cloud Run console:
Now you can use the curl command, an application like Postman or simply use your browser to check that everything is working fine. You should see a pretty "Hello World!":
> curl https://helloworld-oqdeheujtq-ue.a.run.app
Hello World!%
That's it! We just built and deployed a NodeJS backend on Cloud Run using Cloud Build.
Our application is 100% managed by GCP and it is publicly accessible since we used following flags:
As you may see, the URL is automatically and randomly generated by GCP. Know that it is possible to use your own domain name thanks to Domain Mapping.
Fully managed does not mean that it is not possible to customize the settings of your application. Cloud Run use knative to manage your application and it allow you to customize some kubernetes related things like:
To know more about what you can do, I invite you to use the gcloud run deploy --help command or read the official documentation.