So instead of having to CD directly into the microservice and run NPM install again, we can remove
this line and then we'll remove our NPM install from our first root package.json move it after we copy
over the services we need, including apps auth and the common libs folder, paste the npm install back
in and then add a dash r flag to install, which means recursive, which will go through each project
in the workspace and NPM install, which is exactly what we want to do in this case.
So let's copy this command and go ahead and do the same thing in the rest of our projects.
I'll go ahead and replace all of the NPM installs with this p npm install dash R.
We'll go ahead and do the same thing for the payments Docker file here.
And lastly for the reservations Docker file.
Sure that we don't forget to copy over the nest cli.
Dot JSON, so make sure we copy that over because that contains important information about how to actually
build our app.
So let's go ahead and add this line to all of our Docker files underneath the Tsconfig.json to make
sure that we do copy this over as well.
Next.
Since we're copying over the nest JSON, I want to explicitly tell the NPM run build command here that
we're just building auth and we don't get confused and try to build any other service here.
So let's go ahead and update all the Docker files now to explicitly build the application that we are
in.
So I'll go ahead and add this to notifications payments and lastly, reservations here.
Now, before you start your containers back up, make sure you run NPM install dash r from the root
here to make sure we have all of our node modules up to date.
Fortunately, we'll have a new node modules folder in each of our services because of the package.json.
Make sure that we go to our root gitignore here and change our node module.
Gitignore to be star star slash node node modules so we ignore every node modules folder and then we
can run Docker, compose up dash, dash, build to force, rebuild all of our containers.
Now you can see all of our containers have started up with our newly refactored Productionized Docker
files.
Okay, so we've made more changes to how we build our images and it's getting really cumbersome to have
to keep manually rebuilding, tagging and pushing each and every one of our images manually so we can
actually use Gcloud to set up a CI CD pipeline and make our build and deployment process completely
automated so that we have new images that get built.
Every time we push a commit to our repository.
We can do this with Google Cloud build, which is going to allow us to define a configuration file where
we can list out all the steps required to build and push our Docker images.
So let's go ahead and build this file.
We're going to go ahead and call this Cloudbuild.yaml and then we're going to go ahead and add a steps
key here and open up a new list where we're going to take advantage of a pre-built builder called Gcr.io,
slash cloud builders slash Docker.
And this is going to be a pre-built container that allows us to build, tag and push Docker images directly
in Google Cloud build.
Next, we're going to go ahead and specify the args that we're going to pass to the Docker command.
In this case, we're going to pass build and then we're going to pass dash T here to tag it.
Next, we're going to pass in the URL to our actual repository.
So remember to get this, we can go to the artifact registry and click the copy button up top and then
we can go ahead and paste this in.
Then we'll add the slash production because this will be for our production image.
And then next we're specify dash F, which is the path to the Docker file.
In this case, it will be an app slash reservation slash dockerfile and then we're going to specify
the context will be at the current root of the directory.
So this is everything we need to build the Docker image.
Let's go ahead and add another step here which will be to actually push the image so we can actually
go ahead and copy this existing list here and we'll go ahead and paste it.
The name will still be the same here.
We're using the same Docker builder, but now we can just change the command here to be just push and
then we pass in the full URL of the Docker image here and then we can get rid of the rest of the commands
here.
So now we have instructions for how to actually build and push the reservations Docker file.
Let's go ahead and copy this chunk here and we'll go ahead and paste it in three more times.
So we have set up for the rest of our services.
We can even go ahead and add a comment here.
So we'll label the label saying this is for the reservation section and this next block here will be
for the auth service.
So let's go ahead and change out the URL here to auth.
And the path to the Docker file will be off.
And lastly, when we push it, this will now be off.
Next we'll move on to the notifications service.
So change the URL to notifications repository and go to the notifications folder for the Docker file.
And lastly, make sure we actually push the notifications image.
Finally, we have the payments image.
So let's change out the payments URL, the path to the Docker file and how to actually push it.
So back in Google Cloud console, go ahead and look for cloud build in the search bar here.
Go ahead and click on Cloud Build and we'll go ahead and click enable to enable this API.
Okay, so after we've enabled Cloud build, we're going to go ahead and firstly set up a build trigger
which is going to kick off an automatic build.
In our case, we are going to want to detect pushes to our GitHub repository and automatically start
building new Docker image every time we push up.
So let's go ahead and give this trigger a name of sleeper dash push and then for the event we're going
to push to a new branch.
We want to invoke this trigger and then for the source we'll just keep first generation and then for
the repositories, this is where we're going to actually connect our repository.
So go ahead and choose GitHub as this is.
Where we're hosting this code.
I'll go ahead and click continue and make sure you allow pop ups to start the OAuth flow.
Go ahead and sign in to your GitHub account and allow Google Cloud build to access your repository for
this project.
Next, you can see we have a message saying the GitHub app is not installed and any one of your repositories.
So we'll go ahead and click on install Google Cloud Build and then we can go ahead and select which
repository we'd like to allow it to have access to.
So go ahead and select this repository.
I'll go ahead and select the newly linked repository and click okay.
And go ahead and click on Connect.
We'll leave the branch trigger to be on the main branch right now.
In fact, we can go ahead and just select any branch so that all branches will build.
We'll go ahead and keep the branch that we only build when we push to the main branch here, which will
be where our production code lives.
We'll go ahead and leave the configuration to auto detect the cloudbuild.yaml at the root of our project.
We can even go ahead and click send build logs to repository and then go ahead and click on Create.
So now that we've configured our remote trigger, let's go ahead and commit this Cloudbuild.yaml and
we'll actually push it up to our repository.
So after you go ahead and push this up, you should then be able to go back to the cloud build dashboard
now and see that we are running our trigger here.
The sleeper push we just set up from our repository because it detected our commit.
We can even click into it and see how each stage of the build or each Docker image build is proceeding
and see the logs in real time, which can be super helpful to debug any issues.
So after we've given the builds a little bit of time, you should see that it is successful.
Now we've correctly built all of our Docker images after committing and pushing to our branches and
we are automatically pushing to the repository in artifact registry.
So now all of our code commits will automatically be built and deployed and there's nothing we'll have
to do manually.