[0:00]Hello, welcome back. Today, we're going to do a quick project on building a CI CD pipeline using Google Cloud Build and GitHub. So just a quick overview here, as you can see, this is going to be our CI CD workflow. The developer is going to make changes to the code, push the changes to a GitHub repository, and that's going to trigger a web hook. The web hook is going to send a notification to Google Cloud Build, which is going to retrieve the code from GitHub. It's going to run tests on the code to make sure that everything's good to go. Then it's going to build a Docker image. It's going to store that image in Google Container Registry, and then it's going to deploy that image to Google Kubernetes Engine. So that's the whole workflow in a nutshell, and this is what we're going to be building today. So let's jump right in. The first thing we need to do is go to the Google Cloud console. I'm already logged in. If you're not logged in, go ahead and log in. You will need a Google Cloud account. The project we're going to build today will be well within the free tier, so you won't incur any charges. If you don't have an account, go ahead and create one. If you already have an account, just log in and you'll be good to go. Now, once you're logged in, you'll see a screen similar to this. You may not have a project set up. The first thing we're going to need to do is go up here to the top and create a new project. You can use the drop down here. I already have a project created. I'm going to go ahead and use this project. But if you don't have one, just click on new project here. It'll ask you to give it a name. I'm going to cancel this. I'm going to just use my existing project, but you can go ahead and create a project. Once your project is created, the next thing you're going to need to do is to open up Cloud Shell. So go ahead and open up Cloud Shell right here. It's going to provision a machine for you. And then we're going to just set some environment variables and make sure that we have all the correct permissions. So the first command we're going to run is to set the project ID. Now to get your project ID, you can go up to this drop down up here at the top. This is your project ID. I'm going to copy that and I'm going to paste it in here. It's going to be different for you. If you go ahead and hit enter, that's going to set your project ID. The next thing we're going to need to do is to enable the Google Kubernetes Engine API. So I'm going to copy and paste this command and I'm going to enable the Google Kubernetes Engine API. It may take a few seconds to enable the API, so just bear with it. Once that's done, we're going to do the same thing for Google Cloud Build. We're going to enable the Cloud Build API. So just copy and paste that command and hit enter. It's going to enable the Cloud Build API. Then we're going to enable the Container Registry API. So we'll do the same thing. Copy and paste. And that's going to enable the Container Registry API. The next thing we're going to need to do is to give Google Cloud Build the correct permissions to be able to deploy to Google Kubernetes Engine. By default, Google Cloud Build does not have those permissions. So we're going to get the Google Cloud Build service account. So I'm going to paste this command. This is going to retrieve the Google Cloud Build service account. I'm going to set it as an environment variable. And then we're going to grant that service account permissions to Google Kubernetes Engine. So this command is going to grant the Google Cloud Build service account the Kubernetes Engine developer role. And that's going to give it the necessary permissions to be able to deploy to Kubernetes Engine. So go ahead and hit enter. It's going to take a few seconds for that to go through. Once that's done, the next thing we're going to do is to create a Google Kubernetes Engine cluster. This is where we're going to deploy our application. I'm going to just copy and paste this command here. We're going to create a Google Kubernetes Engine cluster. We're going to call it CI CD cluster. We're going to create it in the US Central one region, and we're going to provision two nodes for it. We're going to disable HTP load balancing because we don't need it for this project. And we're going to set the machine type to E2 medium. So go ahead and hit enter. This is going to take a while for the cluster to provision. So just sit back and relax. It may take up to five to ten minutes, so just be patient. And once it's done, we'll continue on. All right, so as you can see, our Google Kubernetes Engine cluster has been provisioned. So we're good to go. The next thing we're going to do is to create our GitHub repository. So I'm going to go to GitHub. I'm going to create a new repository. I'm going to call it CI CD project. I'm going to make it private, but you can make yours public if you want. I'm not going to add a readme file, so I'm going to go ahead and click create repository. So now our repository is created. The next thing we need to do is to clone the repository. So I'm going to copy this command and I'm going to go back to Cloud Shell. I'm going to paste that command in and I'm going to clone the repository. Now, if you have two factor authentication enabled on your GitHub account, which I do, you're going to need to generate a personal access token. So I'm going to go ahead and do that right now. So I'm going to go to my profile here. I'm going to go to settings. I'm going to go to developer settings. Personal access tokens. I'm going to generate a new token. I'm just going to call it CI CD token. I'm going to set it to expire in seven days. And I'm going to give it all the permissions here. So I'm going to select all the scopes. So select all the scopes and then generate token. I'm going to copy this token. This is important. You only see it once. So make sure you copy it. I'm going to go back to Cloud Shell. And I'm going to just paste that token in and hit enter. So now that's going to clone the repository. So now if I do LS, I have the CI CD project folder. I'm going to CD into that folder. The next thing we're going to need to do is to create our application files. So I'm going to go to the project. I'm going to go to the files. I have all the files here in this folder called CI CD project. So I'm just going to copy these files into my Cloud Shell editor. I'm going to use the Cloud Shell editor right here. You can open up the editor if you want. I'm going to open it in a new window. And I'm going to go here and I'm going to go to open folder. I'm going to open up the CI CD project folder. I'm going to click open. Now I'm going to create a new file. I'm going to call it Docker file. Now, if you're not familiar with Docker, Docker file is a text file that contains all the commands that you would use to build a Docker image. So it's basically a blueprint for your Docker image. So I'm going to paste in the contents for my Docker file. So if you want to follow along, you can go ahead and copy this code. I'm going to explain what's going on here. So the first line is from Python 3.9 slim Buser. So this is just saying that we're going to use a Python 3.9 image as our base image. We're going to set the work deer to app. So we're going to create a folder called app in our image. We're going to copy the requirements dot text file, which we're going to create right now. So let's go ahead and create that file. So new file requirements dot text. And in this file, we're going to have just one line. It's going to be flask. So this is just saying that we're going to install flask into our Docker image. So if we go back to the Docker file, we're going to copy the requirements dot text file into our image. Then we're going to run pip install or requirements dot text. So we're going to install all the dependencies that are in our requirements dot text file. In this case, it's just flask. Then we're going to copy all the files from our current directory into the app directory in our image. Then we're going to expose port 5,000 because our flask application is going to run on port 5,000. Then we're going to set the entry point to Python app.py. So this is going to run our app.py file. So now we're going to create our app.py file. So new file app.py. And this is going to be a very simple flask application. So I'm going to paste in the code. So what's going on here is we're importing flask. We're creating an instance of the flask app. We're defining a route for the root URL. So when you go to the root URL, it's going to return hello from CI CD. Then we're going to run the app on port 5,000. So that's our very simple flask application. The next file we're going to need is our Google Cloud Build configuration file. So I'm going to create a new file called cloudbuild.yaml. Now, if you're not familiar with Google Cloud Build, Google Cloud Build is a service that executes your builds on Google Cloud. It can import source code from a variety of sources, including Cloud Storage, Cloud Source Repositories, and GitHub. It can also run a series of build steps to produce artifacts such as Docker images or Kubernetes manifests. So this is going to be the configuration file for our Google Cloud Build pipeline. So I'm going to paste in the contents. So let's go over what's going on here. The first step is to build a Docker image. So we're using the Cloud Build Docker builder. We're setting the args to the project ID. So this is going to be your project ID. And then the image name is going to be your project ID slash CI CD project. So this is going to build a Docker image and tag it with this name. Then we're going to push that image to Google Container Registry. So we're using the Cloud Build Docker builder again. And we're pushing that image to Google Container Registry. The next step is to deploy to Google Kubernetes Engine. So we're going to use the Cloud Build GKE builder. We're going to set the cluster to CI CD cluster, which is the name of our cluster. We're going to set the location to US Central one, which is the region where our cluster is located. And then we're going to run kubectl apply F Kubernetes.yaml. So this is going to apply our Kubernetes manifest file to our Kubernetes cluster. So the last file we're going to need is our Kubernetes manifest file. So I'm going to create a new file called Kubernetes.yaml. And this is going to be the deployment for our application to Google Kubernetes Engine. So I'm going to paste in the contents. So let's go over what's going on here. So the first thing we're defining is a Kubernetes deployment. The name of the deployment is going to be CI CD project. We're going to have three replicas, so we're going to have three instances of our application running. We're going to set the selector to app CI CD project. We're going to set the template metadata labels to app CI CD project. We're going to set the containers name to CI CD project. And then the image is going to be GCR IO slash your project ID slash CI CD project. So this is going to be the image that we built in the previous step. We're going to set the container port to 5,000 because our flask application is running on port 5,000. The next thing we're defining is a Kubernetes service. So this is going to expose our application to the outside world. So the name of the service is going to be CI CD project service. We're going to set the selector to app CI CD project. We're going to set the type to load balancer. So this is going to create a load balancer for our application. And then we're going to set the ports to port 80, target port 5,000. So this is just saying that when you go to port 80, it's going to redirect to port 5,000. So that's our Kubernetes manifest file. So now we have all the files we need. We have the Docker file, requirements dot text, app.py, cloudbuild.yaml, and Kubernetes.yaml. So now we're going to go back to Cloud Shell. We're going to do a git add dot to add all the files. Then we're going to do a git commit dash M initial commit. And then we're going to do a git push. So this is going to push all the files to our GitHub repository. So now if we go back to our GitHub repository and we refresh, we should see all the files there. So there you have it. All the files are there. The next thing we're going to need to do is to connect our GitHub repository to Google Cloud Build. So I'm going to go back to the Google Cloud console. I'm going to go to Cloud Build. I'm going to go to triggers. I'm going to create a trigger. I'm going to call it CI CD project trigger. I'm going to set the region to Global. I'm going to set the event to push to a branch. I'm going to set the source to GitHub. I'm going to connect to GitHub. I'm going to authorize Google Cloud Build to access my GitHub account. I'm going to install Google Cloud Build to my GitHub account. I'm going to install it on all repositories. So now I'm going to select my repository. So it's CI CD project right here. So I'm going to select that. I'm going to click connect repository. I'm going to leave the branch as master. I'm going to leave the build configuration as Cloud Build configuration file. And I'm going to click create. So now our trigger has been created. So now if we make any changes to our GitHub repository, it's going to trigger a Google Cloud Build pipeline. So let's go ahead and test that out. I'm going to go back to Cloud Shell. I'm going to open up the editor. I'm going to go to app.py. And I'm going to change the message to hello from CI CD version two. I'm going to save that file. I'm going to go back to Cloud Shell. I'm going to do a git add dot. Then I'm going to do a git commit dash M version two. Then I'm going to do a git push. So now this is going to push the changes to our GitHub repository. And it's going to trigger our Google Cloud Build pipeline. So if we go back to Google Cloud Build, we should see a build running. So there it is. The build is running. It's going to take a few minutes for the build to complete. So just be patient. And once it's done, we'll check the results. All right, so our build has completed successfully. So if we go into the build, we can see all the steps that it took. So it built the Docker image, pushed it to Container Registry, and then deployed it to Google Kubernetes Engine. So now if we go to Google Kubernetes Engine, we should see our application running. So I'm going to go to Google Kubernetes Engine. I'm going to go to workloads. We should see our CI CD project deployment. It's healthy. It has three replicas. So we're good to go. Now, if we go to services and ingress, we should see our CI CD project service. And it has an external IP address. So if we click on that, it should take us to our application. So there it is. Hello from CI CD version two. So our CI CD pipeline is working as expected. So every time we make a change to our GitHub repository, it's going to trigger a Google Cloud Build pipeline. It's going to build a Docker image, push it to Container Registry, and then deploy it to Google Kubernetes Engine. So that's how you build a CI CD pipeline using Google Cloud Build and GitHub. I hope you enjoyed this project. If you have any questions, feel free to leave a comment below. And don't forget to like and subscribe for more videos like this. Thanks for watching.
Watch on YouTube
Share
MORE TRANSCRIPTS



