WEBVTT

00:00.020 --> 00:07.130
So instead of having to CD directly into the microservice and run NPM install again, we can remove

00:07.130 --> 00:15.620
this line and then we'll remove our NPM install from our first root package.json move it after we copy

00:15.620 --> 00:23.000
over the services we need, including apps auth and the common libs folder, paste the npm install back

00:23.000 --> 00:31.820
in and then add a dash r flag to install, which means recursive, which will go through each project

00:31.820 --> 00:37.460
in the workspace and NPM install, which is exactly what we want to do in this case.

00:37.460 --> 00:42.050
So let's copy this command and go ahead and do the same thing in the rest of our projects.

00:42.050 --> 00:49.160
I'll go ahead and replace all of the NPM installs with this p npm install dash R.

00:49.190 --> 00:53.270
We'll go ahead and do the same thing for the payments Docker file here.

00:53.690 --> 00:57.200
And lastly for the reservations Docker file.

01:01.110 --> 01:05.670
Sure that we don't forget to copy over the nest cli.

01:06.660 --> 01:12.510
Dot JSON, so make sure we copy that over because that contains important information about how to actually

01:12.510 --> 01:13.670
build our app.

01:13.680 --> 01:21.300
So let's go ahead and add this line to all of our Docker files underneath the Tsconfig.json to make

01:21.300 --> 01:23.640
sure that we do copy this over as well.

01:30.160 --> 01:30.670
Next.

01:30.670 --> 01:38.080
Since we're copying over the nest JSON, I want to explicitly tell the NPM run build command here that

01:38.080 --> 01:43.630
we're just building auth and we don't get confused and try to build any other service here.

01:43.630 --> 01:50.290
So let's go ahead and update all the Docker files now to explicitly build the application that we are

01:50.290 --> 01:50.710
in.

01:50.710 --> 01:56.800
So I'll go ahead and add this to notifications payments and lastly, reservations here.

01:58.630 --> 02:04.600
Now, before you start your containers back up, make sure you run NPM install dash r from the root

02:04.600 --> 02:07.960
here to make sure we have all of our node modules up to date.

02:07.990 --> 02:14.170
Fortunately, we'll have a new node modules folder in each of our services because of the package.json.

02:14.200 --> 02:20.590
Make sure that we go to our root gitignore here and change our node module.

02:20.590 --> 02:28.180
Gitignore to be star star slash node node modules so we ignore every node modules folder and then we

02:28.180 --> 02:35.890
can run Docker, compose up dash, dash, build to force, rebuild all of our containers.

02:35.920 --> 02:41.980
Now you can see all of our containers have started up with our newly refactored Productionized Docker

02:41.980 --> 02:42.850
files.

02:42.940 --> 02:49.630
Okay, so we've made more changes to how we build our images and it's getting really cumbersome to have

02:49.630 --> 02:56.890
to keep manually rebuilding, tagging and pushing each and every one of our images manually so we can

02:56.890 --> 03:04.220
actually use Gcloud to set up a CI CD pipeline and make our build and deployment process completely

03:04.220 --> 03:07.400
automated so that we have new images that get built.

03:07.400 --> 03:10.390
Every time we push a commit to our repository.

03:10.400 --> 03:16.790
We can do this with Google Cloud build, which is going to allow us to define a configuration file where

03:16.790 --> 03:21.740
we can list out all the steps required to build and push our Docker images.

03:21.740 --> 03:23.680
So let's go ahead and build this file.

03:23.690 --> 03:30.380
We're going to go ahead and call this Cloudbuild.yaml and then we're going to go ahead and add a steps

03:30.410 --> 03:39.680
key here and open up a new list where we're going to take advantage of a pre-built builder called Gcr.io,

03:39.680 --> 03:43.430
slash cloud builders slash Docker.

03:43.430 --> 03:50.540
And this is going to be a pre-built container that allows us to build, tag and push Docker images directly

03:50.540 --> 03:52.280
in Google Cloud build.

03:52.310 --> 03:56.870
Next, we're going to go ahead and specify the args that we're going to pass to the Docker command.

03:56.900 --> 04:02.240
In this case, we're going to pass build and then we're going to pass dash T here to tag it.

04:02.270 --> 04:07.280
Next, we're going to pass in the URL to our actual repository.

04:07.280 --> 04:13.700
So remember to get this, we can go to the artifact registry and click the copy button up top and then

04:13.700 --> 04:15.380
we can go ahead and paste this in.

04:15.410 --> 04:19.940
Then we'll add the slash production because this will be for our production image.

04:19.940 --> 04:24.740
And then next we're specify dash F, which is the path to the Docker file.

04:24.740 --> 04:30.320
In this case, it will be an app slash reservation slash dockerfile and then we're going to specify

04:30.320 --> 04:33.410
the context will be at the current root of the directory.

04:33.410 --> 04:36.170
So this is everything we need to build the Docker image.

04:36.170 --> 04:42.440
Let's go ahead and add another step here which will be to actually push the image so we can actually

04:42.440 --> 04:47.390
go ahead and copy this existing list here and we'll go ahead and paste it.

04:47.390 --> 04:48.950
The name will still be the same here.

04:48.950 --> 04:55.760
We're using the same Docker builder, but now we can just change the command here to be just push and

04:55.760 --> 05:01.700
then we pass in the full URL of the Docker image here and then we can get rid of the rest of the commands

05:01.700 --> 05:02.330
here.

05:02.330 --> 05:07.700
So now we have instructions for how to actually build and push the reservations Docker file.

05:07.700 --> 05:14.900
Let's go ahead and copy this chunk here and we'll go ahead and paste it in three more times.

05:14.900 --> 05:17.420
So we have set up for the rest of our services.

05:17.420 --> 05:19.790
We can even go ahead and add a comment here.

05:19.790 --> 05:25.550
So we'll label the label saying this is for the reservation section and this next block here will be

05:25.580 --> 05:27.380
for the auth service.

05:27.380 --> 05:31.760
So let's go ahead and change out the URL here to auth.

05:31.760 --> 05:34.580
And the path to the Docker file will be off.

05:34.580 --> 05:37.820
And lastly, when we push it, this will now be off.

05:38.000 --> 05:42.110
Next we'll move on to the notifications service.

05:42.110 --> 05:50.960
So change the URL to notifications repository and go to the notifications folder for the Docker file.

05:50.960 --> 05:55.400
And lastly, make sure we actually push the notifications image.

05:55.430 --> 05:57.770
Finally, we have the payments image.

05:57.770 --> 06:04.990
So let's change out the payments URL, the path to the Docker file and how to actually push it.

06:05.110 --> 06:13.120
So back in Google Cloud console, go ahead and look for cloud build in the search bar here.

06:13.120 --> 06:18.700
Go ahead and click on Cloud Build and we'll go ahead and click enable to enable this API.

06:18.790 --> 06:24.580
Okay, so after we've enabled Cloud build, we're going to go ahead and firstly set up a build trigger

06:24.580 --> 06:27.100
which is going to kick off an automatic build.

06:27.100 --> 06:33.460
In our case, we are going to want to detect pushes to our GitHub repository and automatically start

06:33.460 --> 06:36.430
building new Docker image every time we push up.

06:36.430 --> 06:43.450
So let's go ahead and give this trigger a name of sleeper dash push and then for the event we're going

06:43.450 --> 06:45.010
to push to a new branch.

06:45.010 --> 06:51.610
We want to invoke this trigger and then for the source we'll just keep first generation and then for

06:51.610 --> 06:55.660
the repositories, this is where we're going to actually connect our repository.

06:55.660 --> 06:57.850
So go ahead and choose GitHub as this is.

06:57.950 --> 06:59.660
Where we're hosting this code.

07:00.380 --> 07:05.550
I'll go ahead and click continue and make sure you allow pop ups to start the OAuth flow.

07:05.570 --> 07:11.960
Go ahead and sign in to your GitHub account and allow Google Cloud build to access your repository for

07:11.960 --> 07:12.950
this project.

07:12.980 --> 07:18.530
Next, you can see we have a message saying the GitHub app is not installed and any one of your repositories.

07:18.530 --> 07:23.420
So we'll go ahead and click on install Google Cloud Build and then we can go ahead and select which

07:23.420 --> 07:26.590
repository we'd like to allow it to have access to.

07:26.600 --> 07:29.210
So go ahead and select this repository.

07:29.240 --> 07:33.590
I'll go ahead and select the newly linked repository and click okay.

07:33.590 --> 07:35.780
And go ahead and click on Connect.

07:35.810 --> 07:39.680
We'll leave the branch trigger to be on the main branch right now.

07:39.840 --> 07:44.230
In fact, we can go ahead and just select any branch so that all branches will build.

07:44.240 --> 07:48.590
We'll go ahead and keep the branch that we only build when we push to the main branch here, which will

07:48.590 --> 07:50.300
be where our production code lives.

07:50.300 --> 07:56.330
We'll go ahead and leave the configuration to auto detect the cloudbuild.yaml at the root of our project.

07:56.360 --> 08:01.940
We can even go ahead and click send build logs to repository and then go ahead and click on Create.

08:01.940 --> 08:07.580
So now that we've configured our remote trigger, let's go ahead and commit this Cloudbuild.yaml and

08:07.580 --> 08:10.640
we'll actually push it up to our repository.

08:10.640 --> 08:16.010
So after you go ahead and push this up, you should then be able to go back to the cloud build dashboard

08:16.010 --> 08:19.640
now and see that we are running our trigger here.

08:19.640 --> 08:25.820
The sleeper push we just set up from our repository because it detected our commit.

08:25.850 --> 08:32.150
We can even click into it and see how each stage of the build or each Docker image build is proceeding

08:32.150 --> 08:36.830
and see the logs in real time, which can be super helpful to debug any issues.

08:36.830 --> 08:40.880
So after we've given the builds a little bit of time, you should see that it is successful.

08:40.880 --> 08:46.850
Now we've correctly built all of our Docker images after committing and pushing to our branches and

08:46.850 --> 08:51.350
we are automatically pushing to the repository in artifact registry.

08:51.350 --> 08:56.450
So now all of our code commits will automatically be built and deployed and there's nothing we'll have

08:56.450 --> 08:57.050
to do manually.
