3

Build a Node.js App using Docker and Google Cloud Platform | Bits and Pieces

 2 years ago
source link: https://blog.bitsrc.io/build-your-application-in-google-cloud-platform-d5548ad37e72
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Build your Application on Google Cloud Platform

Our first step into deploying a full-stack application in the cloud

Photo by Aleksandar Pasaric: https://www.pexels.com/photo/view-of-cityscape-325185/

Some time ago, I had the luck to step into Google’s offices in London. This was fantastic, but more than that, I discovered how incredible their cloud platform is. I’ve always been into programming since then, so I decided to dig into GCP and understand how this works.

In this article, we are going to deploy a simple API using Node.js and Express into GCP.

In my previous article, I already created the API and built it with Docker. If you are interested in learning that, go have a look! Otherwise, you’ll find the code I’ll start from on my Github.

The final code for this article can be found on my Github.

Create a GCP project and enable the services

The first step of our journey is going to be about creating the new project on Google Cloud Platform and enabling the different services that will be needed.

Creating our project

In your browser, go to the GCP console (you’ll have to log in using your Google account or create a new one if you don’t have any)

Once logged in, at the top left-hand corner of the page will be the project selector

The project selector

Click on select a project and you’ll see the following popup:

Just click on NEW PROJECT and fill in the project name (and organization if needed)

Once created, you now have access to your project.

GCP might ask you to fill in some billing information in case you are reaching the thresholds. Don’t worry too much about this, the whole tutorial won’t use any paid features.

At the top of the page, we can see a search bar, we’ll use it to navigate between the different services as GCP has tons of them!

Enabling Cloud build

Let’s start with Cloud Build. Cloud build is a service that builds automatically your project, it’s even able to watch your git repo and build every time you push on a specific branch!

In the search build, type Cloud build and you should have the following result:

As you can see, the search is split into three parts:

  • Products and pages: Any result around a product or the different pages linked to this product. They are for usage
  • Documentation and tutorials: Self-explanatory, it’s a bunch of knowledge!
  • Marketplace: That’s where you can enable the services and that’s where we’re going.

Click on Cloud Build API just under the marketplace. You’ll end up on this page:

There are two things to consider there:

  • The first one is the Enable button that will allow us to enable the service. You can already click on that;
  • The second thing is around Pricing and as we can see here, we are allowed 120 minutes per day of building time, which is more than enough for what we are doing here!

Artifact registry

The second service to enable is going to be artifact registry. This service is meant to store your Docker images after being built by Cloud build.

Once again, in the search bar, look for Artifact registry select the first result in the marketplace section and enable it

In terms of pricing, you’d start to pay after storing around 500 megabyte or more per month. Once again, more than enough for our little project

And that’s it! All our required services are enabled!

It’s time to get to work!

Building our project

We’ll get started with Cloud Run. In the search bar, look for Cloud build and this time, click on the result under Products and pages

Link the service to Git

In the menu on the left, click on the Triggers tab.

You should see at the top a button called Manage repositories, click on it and then select Connect repository.

You are going to see multiple options, I’m going to select Github as it’s what I’m using. Authenticate with Github and follow the steps. In the end, don’t click on create a trigger just yet, click on Done instead.

Setup our trigger

On the left, navigate to Triggers again and this time, create a trigger.

Trigger creation page

What’s important here is the name and the source.

Be sure to select the right repo and the right branch (I’ll use master for this).

The second half of the form

On the second part of the form, you’ll see Configuration although Autodetected works, I’d recommend choosing Cloud build configuration file .

We will create and fill this file in the next step. For now, just scroll down and click on Save.

We now have a Trigger that triggers when there is a new push on the branch. You could also run it manually but I’m afraid it won’t work just now!

Creating our configuration file

Back to our local project, create a new file called cloudbuild.yaml and paste the following content:

steps:
- name: gcr.io/cloud-builders/docker
args: [build, '-t', 'europe-west1-docker.pkg.dev/${PROJECT_ID}/master-build/build:latest', '.']
images:
- 'europe-west1-docker.pkg.dev/${PROJECT_ID}/master-build/build:latest'
options:
logging: CLOUD_LOGGING_ONLY

Let’s analyze this file shall we:

steps:
- name: gcr.io/cloud-builders/docker
args: [build, '-t', 'europe-west1-docker.pkg.dev/${PROJECT_ID}/master-build/build:latest', '.']

This is a description of the steps to build our application. Each step is composed of a name and args.

  • The name is the builder you’ll use. You can find the list of all available builders in the GCP repo
  • Args is the parameters that you give to your builder.

In our case, the builder is docker and we are building using our Dockerfile located at the root of the project.

You may find this parameter quite interesting: europe-west1-docker.pkg.dev/${PROJECT_ID}/master-build/build:latest it’ll make more sense when we’ll set up our artifact registry.

images:
- 'europe-west1-docker.pkg.dev/${PROJECT_ID}/master-build/build:latest'

This is to tell GCP the name of the image built to use it for later (in our case through Artifact registry)

options:
logging: CLOUD_LOGGING_ONLY

This option is to tell Cloud build where to put the logs. We are keeping the default one here.

Build

We are ready to give it a go! Just push onto your target branch and see Cloud Build do its magic.

Head to the history tab on the left and you’ll see your job running

Your build is going to fail! Sorry! However, if you open it (click on the id) and check the logs, it’s saying that the repository master-build does not exist. And that’s true! Let’s create it.

Set up artifact registry

Our build failed because there is no repository to store the result. Let’s create it!

In the search bar, look for Artifact registry and navigate to the service.
At the top, click on Create repository.

There are two important things to change here. The first one is the name. It should be identical to what’s in the cloudbuild.yaml just after ${PROJECT_ID}. In our case, it’s master-build

The second is the region, it needs to be the same as the start of this same parameter (in our case, europe-west1)

If you’d like to work in another region, simply change it in the cloudbuild.yaml and in your registry configuration.

Click on CREATE.

Build (again)

Move back to Cloud Build and in the list of triggers, click on RUN
Head to the history and wait for the build to finish. After a few seconds, it’ll finish successfully!

You can also see the new build image by going to the artifact registry!

And here we are! We can now build our application automatically using Cloud Build and the artifact registry. Don’t hesitate to play around a bit more to get more comfortable with the tools!

Build composable web applications

Don’t build web monoliths. Use Bit to create and compose decoupled software components — in your favorite frameworks like React or Node. Build scalable and modular applications with a powerful and enjoyable dev experience.

Bring your team to Bit Cloud to host and collaborate on components together, and speed up, scale, and standardize development as a team. Try composable frontends with a Design System or Micro Frontends, or explore the composable backend with serverside components.

Give it a try →

https://cdn-images-1.medium.com/max/800/1*ctBUj-lpq4PZpMcEF-qB7w.gif

Learn More


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK