From the course: Google Cloud Platform Essential Training for Developers: Cloud-Native Development with Gemini

Using Gemini Code Assist: generating code, Dockerfiles, and REST APIs

Question, how much time do you spend on Stack Overflow or just Googling syntax? Probably too much. In this chapter, we're going to pair program with AI. I'm going to show you how to use Gemini Code Assist to generate Docker files and even write some unit tests and also explain complex errors in seconds. It's not about AI replacing you, it's about helping you move faster. Let's open the Cloud Shell Editor and see what it can do. So now what we're going to talk about is Google Cloud Code Assist. Now this is a really powerful tool to really help you level up your coding right within the Google Cloud, Cloud Shell Editor application, which is really nice. So before we jump into that, let's take a look at Code with Gemini Code Assist, and you're going to see on this Google Cloud website here, they have all the features laid out. There's always things being updated. So I definitely recommend checking this page out first. Anytime you want to get up to speed with the latest features, because they're always adding new things. With that being said, let's jump back into the console. I'm just going to go ahead and open up the editor. And once you go ahead and open this up, you're going to see in the top right-hand corner, there's the Cloud Assist chat app. Now that's actually different from the Code Assist. So the Cloud Assist we'll talk about later, but you want to be sure that you're using the Code Assist. So to get that going, typically it should just pop on up. So I'm going to go ahead and click on this repo that we used earlier. And I'm going to use this example. I'm going to use a disk one. And then as soon as I clicked on this file, you'll see that Gemini icon has actually showed up. And then when I highlight it, it says Gemini Code Assist. So once I highlight all of this code here, if I come up here and I click on this button on the right-hand side, it's actually going to give me this dropdown of, let's say, explain this. So now let's just say we want to go ahead and let's start something from scratch, right? We've been using some of these templates. Now let's say we want to create something completely from scratch. So what I'll do now, I'm going to close out that main TF file and I'm going to say, create a Docker file that will run a Debian image in Cloud Run with all required files in my root directory. So I'm going to go ahead and run this prompt. As you can see, as Gemini is going through it, it's if you click on that drop down arrow, you can see how it's thinking and figuring it out. And it's saying, OK, you need to run this to run a Debian based container cloud when you need a Docker file. And just like that, these are the files to review. So I could click on each one of these and see everything that it's included. I'm just going to accept it. Accept. It has put these files and created them in the home directory in the root. And not only did it recreate or actually create these from scratch, it went ahead and gives me a full breakdown of each of these files. So if we look at the Docker file itself, it breaks it down. It's saying it's using the official Debian 11 Bullseye slim image, as you can see there. It's telling you the work directory here, where the copy and the application files go, the port that you're using for access, and you're also exposing that port, and then Python 3 and HTTP server. So it's breaking down every part of this, And ultimately, this is going to give us a simple web page, which is where this index.html file comes in. And the title of that page will be Hello from Cloud Run. And then you're going to see this running on the page here, Hello World. And then this Docker ignore file is just more of a best practice and tells you which directories and files to exclude when building out the image. So this basically keeps your image smaller so you don't have it bloated and taking up more space than you need to and keeping the build times a lot faster. So the last thing we wanna do here, let's just say, how can we run some unit tests on this? So I'm gonna ask that to Gemini here. How can we run some unit tests? Actually, I'm not even gonna write that. Let's back that up. And let's go ahead and I'm gonna click on this button here at the top. and we're going to say generate some unit tests. We're going to just get them generated for us. So instead of saying how, we're going to use the autocomplete right there from the Gemini code assist console and generate some tests. So what this ended up doing is it actually did a container structure test, and this is what Google has available, and it basically validates the structure of an image by running a series of tests against it. So it generated, you know, 35 more lines of code there. So we're going to just accept all these. And then once we get this in, you could see a lot more of this code. We can explain it, but on the right-hand side, it's automatically breaking this down for us. So this is the new YAML file that we have. If we'll go there, and this is breaking down and it's telling us everything that's happening. So all these commented out, It tells you this part, file existence test, make sure the files are where they should be, the command test and metadata test all the way down. And then once all of this is done, we're pretty much ready to run this Dockerfile in Cloud Run. So now that we've generated all this code, let's just see how this could run if we want to do it using the Cloud Run emulator. Now this will run it locally, and to do that, down here at the bottom where it says Cloud Code No Project, I'm going to click on that, and I'm going to say Run on Cloud Run Emulator. Now this is pretty cool because this won't cost you anything, and it's going to run it locally. So just like that, it will start running, and once it's ready, we'll be able to go to it and launch it. So everything is running, resource is being uploaded, it's been deployed, scroll down. So let's go ahead and check our service out. So if you click on command click, we have deployed our container that we generated using CodeAssist. All the code, the Docker files, everything was generated using Gemini and we were able to deploy it locally without costing us anything. So this will help you be able to do some test runs, get a better understanding of how your code is performing locally, even before you start spending money in Cloud Run. Now, let's jump over to the next video, Gemini CloudAssist.

Contents