Deploying Cloud Function using Google Cloud Build

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up folks gk here welcome back to my channel to end of this video you're going to learn how you can do a sample continuous deployment in google cloud using cloud build and we're going to use uh the google cloud functions here but you know the concept is similar you can use app engine or you can use computing you know you can use whatever service you want or even kubernetes engine for that matter so the whole demonstration would be we're going to simulate a scenario where a developer would commit some code to github so basically the developer would commit the code to the github and so that github will have the cloud function so as soon as the developer comments to that it's the cloud build which is a manage service from google cloud we'll take the code so it takes the code and at the back end it saves that files and everything like you have a staging that happens in the google cloud storage so we don't have to worry about that part for his demonstration but as soon as the file is picked up in the cloud build you have a bunch of steps that you write in the cloud build which is in the form of configuration which i'm going to show what i have done for the sake of cloud function and so that steps will decide what we have to do for example if that cloud configuration file has to use a specific builder or it has to use something else so that we want to see in the configuration file so once it is picked up from github the code will be deployed to the cloud function so let's say at a start you know i have a hello world function which is a sample function but you can use any complex function as well and if you want to change or make a change to the function i will commit that to the github and automatically the cloud build has to deploy that into the hello world function and and change the code so that's the whole scenario but there are limitless ways of innovating here and you know you can use cloud deployment manager which have discussed in my previous video and you can integrate you can integrate that with cloud build as well now for starters so cloud build is a serverless cicd platform from google cloud it's a very easy to use tool so it has a build configuration so the configuration has bunch of steps like you know your ansible playbook has bunch of tasks similar to that it has bunch of build steps so each step is executed in a docker container so with that so let's go to the console so the first step is go to cloud build service and since here we're going to deploy in a cloud function go to the settings and make sure you enable cloud functions deployer rather developer so check this status as enabled as soon as you go to the cloud build and click on settings so once this is done if you go to the dashboard it will show you the history of the bills that have happened in the past and then whether their failure how much percentage of failed and passed and the duration etc etc so those things are displayed here and the history you will see all my previous builds that have happened uh obviously i had to practice uh before i have to demonstrate it to you guys so but this is this page contains all the history about the builds and the trigger which is what we're going to use here i'm going to show you what what i have done here basically but the trigger is the one that would pull the file from the github the public github i'm going to use my own github repository and deploy that cloud function file or the python file to the cloud function service so that's that's the tutorial before what i would recommend even before setting up the triggers is go to the cloud shell by clicking on activate cloud shell and open this in a new window you can close this after you've opened that so i have created a cloud build folder so in this folder you know i have basically a simple main dot py file and this is a very simple python default hello world file so as soon as you go to the cloud functions you create a sample code you will get this code but in any case if you are struggling with this you can go back to my repository which i'm going to share in the description you might have already used this in my past videos but in this repository go to the main dot py file and copy this python file and you know create that file inside the cloud shell so that's number one and number two is obviously you need a requirements.txt this is again the requirement for putting all your libraries inside the cloud function so these two files are the default files so once you have those two files i'm going to show you by first using cloud build from the gcloud command line interface so that you know you would know how to test this out first right so the command is gcloud builds and submit hyphen iphone config config.yml and function hyphen source.zip so before we even execute this command i'm going to show you the sample configuration file that i was talking about let me open this in here itself okay so if i maximize this this is the main config.ml file so the steps so these are the number of steps that we're defining so we have only one step here so that's going to deploy the cloud function and this is the cloud builders so if you are not aware of what is cloud builder so cloud builders are nothing but bunch of you know docker container images so we're going to use the gcloud image here and if you want to look at all the cloud builders you can see here these are all the default builders but there are community-based builders as well i think so if you click on any of these links you can see a common like you know builders are gcloud and then git and then gk deploy for instance if you want to use maven or npm to build your code you can also use those builders so in this case what we are trying to do is we trying to use the gcloud builder and in a gcloud as you know you know you can deploy your cloud function through the command line interface so exactly we're going to pass the same parameters for example you know if you have to deploy the cloud function using gcloud you would do gcloud function deploy and this is the function name and you're going to have the main dot py files and you know requirements.txt and all those files in that source repo i mean in that location wherever you're trying to deploy from or if you're picking up from the source you would give the git repository name there so so this is a standard command when you're trying to deploy using gcloud right and if i go back to my code here the builder is gcloud and pretty much we're going to use the same arguments here so arguments here is function so basically we're going to deploy the function so it is functions and deploy and this is the name of the function that i have defined in my main dot py and here the source is important so source either you can give a complete path of the source where you have the actual code or you can give you know a repository or anything that you can define here and because i want to you know get cloned obviously whenever there is a commit i would clone the whole thing it automatically will clone the whole thing and the source will obviously be at the same specific directory or the working directory so i don't have to specify anything here so that's why i left it as dot here which means like the source will be picked up from that specific directory wherever this the whole build is happening or maybe it is happening inside a container so it's going to pick it up from there and the trigger is http right because i want to use http based endpoint for this function to trigger so that's http and if you want to use pub sub you can use pub sub here but we're not doing that for this demo and runtime i'm using python373.7 you can use 3.8 as well allow unauthenticated so this is nothing but you know i want to allow the cloud function to be open for internet uh for anybody who can access it without even having an iam permission or service account or anything so it's open for everybody to access it so that's unauthenticated and these things are you know very well documented and defined in the cloud functions documentation itself you can go through that and you would know exactly what you have to pass as the parameters here and so we have the cloud build.tml file that i have created you can also create it in the json format so this is one important file and obviously we have the actual python code like i was showing you before you know it it has nothing it has just it returns hello world and it takes the arguments and this is the standard flask format that you would have to use and then requirements.txt has all the dependencies here we're not using any other dependencies so it's it's all blank for now okay so we're going to quickly test this from the gcloud command so basically what what i'm trying to do is i'm trying to run gcloud builds and submit this is the command configuration here i'm going to use cloud build.gml and function so this is the source dot zip so when you're trying to run from g cloud builds you know from the gcloud interface or the command line you have to give the source as a zip file so i have compressed main.py and requirements.txt into the zip file and i'm passing that as a source now if i run this okay so i have to set the project before that so gcloud config set project project id is youtube demo we're going to run this again so if you want to if you want to look at the build file cloud build.tml you can see that i'm deploying a sample function here all right now so let's run this my bad okay could not find the source function hyphen source dot sip okay let me see okay spelling wrong uh spelling mistake here f u n c diy so what it does is first it will stage the zip file into a gcs bucket and that's why you might have seen here i've put the gcs bucket here in in the previous diagram and here the flag minus minus runtime is required for the newer functions which have missed in the code so i can do unzip function source all vi main my bad so vi cloud build.tml and i can't get the runtime from my code oops and i will also pass this here okay good so once you have done this for the first time this would be required so let's say you want to make the change to the cloud function again next time you don't have to worry about the runtime because the cloud function is already created you're just changing the code at that point of time all right so now let's do this again so it can find the image basically what happens is first time when i was running this before before when i was creating this video you know i have already used the gcloud builder so when i use that the gcloud builder image is part of the container registry so that's how it maintains the whole thing in the container industries right so next time when i'm trying to use g cloud again it doesn't have to download the image from the public repository and stuff so it has maintains that now if you want to look at so what's going on in the actual build you can look into the console and in the history tab you can see here the real time execution of the whole build and while this is happening you can also open a duplicate tab maybe and go to the cloud function all right so the sample function is getting created so as soon as this is done we would know if the function got created successfully so it's not done yet completely okay it says the main has no attribute sample hyphen function so for that what we have to do is whenever you see this error go here and fix the main function to or you either change the function name or fix it here it's the same thing so let's say put sample function here so go to your cloud build tml file and make sure that the name is same here as well so now by the way i'll remove some of the things that are that i don't require in this directory i'm going to build this again [Music] so now if i go back to the cloud functions and you will see a new function is getting executed i mean rather deployed or created i can delete this old function which i no more needed so let's give it some time and i'll resume the video once it is done all right so now if you see the build is successful if i go to the build history here so you can you see a red tick which is successful and you see you know the source and stuff all right this is good okay now let's go to the cloud function if we go to the sample underscore function you see the source is here and we have the function to execute a sampling school function perfect everything is good but the main demo starts now right basically what we want to do is we want to trigger or deploy function as soon as there are changes made to the repository in this case which is github so for that go back to your cloud build go to the trigger section on the left side and create a new trigger let's say you do not have any repository yet for the first time you have to create a new repository here so if you're using github repository like you know most of the most of you guys might be using that so click click on github repository and click on continue so here i have already have connected my cloud advocate account uh which you guys are aware of it or if you are if you're trying to create a new if you're trying to you know create a new account or rather add a new account you would click on add a new account so what this will do is it will it will get permission behalf of you and add github to this cloud build repository which means like you know your your github needs permission to send a hook whenever there is a new uh commit to the branch so all those permissions will be taken care if you have any questions on this let me know in the comment section i'll help you out but i have done that part it's very easy step by step process you know click on next next and then you will see what you have to do so once that is done you will have the trigger created and now when i go to the settings so this is this is the important part here so the name is the default name and here the description is pushed to any branch which means like any time if i make any change to any part of the code in any branch this build will be triggered so obviously you know it will try to find for the build configuration which is cloud build.tml file or whatever that is starting with dot ml file and stuff so that is good thing it will search for that but uh the best scenario or the recommended way of doing this is you know give a specific branch let's say you create a builds branch or you know a google cloud branch especially for this then give that branch here and try to do it in a proper way i have some i just gave it default and now if i make a change in my master branch it will be triggered automatically which i'm going to show you so that's with respect to push to any branch and if you want to use a tag you can use a tag but this was the default setting here you can use git ignore and all those things and so the build configuration it will automatically detect a cloud build.tml file and if you want to specifically give a specific name like for example you don't want to use cloud build.tml you want to use you know something else then you can define that or name that here cancel so now what i'm going to do is i'm going to make a small change in the github and you will see that it will trigger the build as soon as i make a change in the github so i'm going to keep the history tab and you can see the time is 955 which i've executed before so now we're going to make a change here i'm going to decrease the screen size and go to the cloud google cloud and i'm make and then i'll make a small change here let's say that first time i want to deploy this right so i want to go to main dot py and edit this file and call it as just put a few more marks here and comment the changes as soon as i've clicked on commit you will see that the build will start so the build will trigger automatically and you'll also get the commit id here and if you click on that it goes to the github page and you will see the actual commit id that's a good thing it has a proper integration with github all right so while this is happening i'll pause the video and i'll resume it once this is done all right so the build got successfully executed and then we can see all the details here we have a green tick if i go back to the cloud functions i see a new function that got deployed and now if i want to test this out real quick i can go to testing and click on test the function and you can see the three marks here now let's say you want to make a change to the code and you know say instead of hello world i would say hello advocates or something like that and commit the change go back to the history uh we see a new build that is getting executed so while that is happening you can see you know the cloud function is being deployed because there's a new function or new changes that are getting deployed and this is where you would like to use you know canary based deployments or the blue green deployments and stuff all right now we have the new function let's go to the testing again and click on test function yep that was a caching issue but you can see we have a new output hello cloud advocates so this is a sample demonstration of this whole thing like you know as a developer if you commit to change to the github how it gets called by the cloud build and it gets deployed to the cloud function now you can do the same exercise for app engine or google kubernetes engine or you can also integrate you know cloud deployment manager and you can make this whole thing more complex by integrating with kit so the most important thing is you know let's say if anybody would ask you how cloud build is different from jenkins and in which scenarios or why would you why would you have to use jenkins instead of cloud build so this is where i think you have to think about this in a way that jenkins is a cloud agnostic tool which means like you know you should you can use jenkins and you can deploy into multiple clouds and most of the enterprises and companies use jenkins to to control multiple cloud service providers so that's where it would be more helpful to use jenkins in those scenarios now if you are sticking only to google cloud and you know cloud will will might shine there so that's uh you know one advantage of cloud build is obviously because it's a managed service you don't have to maintain your slaves or you know servers and all those things unlike jenkins because jenkins when you set up an environment you have to maintain the master nodes you have to maintain the slaves uh you have to do all the maintenance work there but in the case of cloud build it is completely controlled by google cloud obviously and that's why they call it as a serverless you know platform but if you have any other questions on this tutorial let me know in the comment section and that's all i wanted to cover here and we might maybe come up with more scenarios that i think that would be helpful for you all but this is an important concept to remember and to practice thank you so much for watching and let me know your feedback thank you take care bye you
Info
Channel: Cloud Advocate
Views: 7,152
Rating: undefined out of 5
Keywords: Google Cloud, Cloud Computing, Cloud Technologies, GCP exam, GCP Associate exam, Google Associate exam, Cloud Engineer, Cloud Career tips, cloud advocate, google cloud build ci cd, cloud build with cloud function, cloud build
Id: TeguiTFprFo
Channel Id: undefined
Length: 25min 17sec (1517 seconds)
Published: Mon Sep 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.