#96 Deploy to Digital Ocean App Platform

11/11/2022
deployment
Transcript
Hello. So as, uh, you must already know, Heroku is depreciating its free tier as of the 28th of November. And it's really sad because over, I guess 10 years or even longer, Heroku was the first place where a developer could, uh, deploy an application to production and see how it would look in, uh, a reduction environment versus a development environment. But this error is over. And personally I don't see any sense of, uh, paying for a paid, uh, tier on Heroku where you can, uh, pay the same amount or approximately the same amount on other platforms. And, uh, the platform of choice for me is digital Ocean. So, uh, here I've already deployed one application on digital Ocean App platform that, uh, costs, uh, minimum $12 per month. That will be $7 for the database and $5 for the application, uh, itself. And, uh, I'm kind of satisfied and it gives you a great, uh, possibility to, uh, have more control than you have in Ruku over your application. Now there are droplets that, uh, are like containers and they, uh, can potentially be cheaper, but, uh, apps on the ad platform are much easier to manage and, uh, it is much closer to what you'd get on Heroku. So in this episode, they go to deploy an application to Digital Ocean app platform. And if you still do not have a digital, digital Ocean account, uh, first of all try to create one. Now here I've got this referral link. So if you use it, I'll also add it in the video description. You'll get the $200 to, for the start end, I will get 25 onto my credits. So for everybody anyway, if you create additional Ocean account and go to billing, so, uh, there'll be a tax, uh, dependent on the country you're working, uh, you're creating the account from. So, uh, you can also try changing that billing address. And if the billing address is in the us, then there will be no additional tax. If it'll be in the U, then you'll pay around 20%, uh, more on all everything you do in the digital ocean. So if you have a US address, then it's kind of going to be cheaper everything by default. So when you're done with the creation of, uh, your account, uh, you will go to apps and you can deploy and your application to do it, you would go to create and you select app. And I'm going to select one of the repositories that I already have. So I have already connected my GitHub to Digital Ocean and I can select a repository. And the repository that I'm going to select is going to be this UR menu app. Now, before we continue, let's have a quick, uh, look and understanding of this application that I am going to deploy. So imagine you don't know anything about this application yet and you want to deploy it. Uh, so, uh, let's have a quick look at this application in development. Now, when I see an application that, uh, I haven't worked with, uh, before and I need to understand something, the first place I go is the Gem file. So yeah, the gem file, I see it's running Ruby 3.1 rails seven, uh, input maps for JavaScript. Uh, and uh, yeah, it's from Puma. Okay, so well everything is uh, nearly default rails, uh, seven and yeah, it's also got read as, uh, most of the default stuff on rail seven VDI for two broadcasts. Now, um, we are going to check rails application credentials, whether the application is using any credentials. No, as you see all the credentials files are, uh, lines are commented out. So we don't use any secret credentials in this application at the moment, but we still need a master key. So, um, I've, uh, deleted the previous credentials via ml and I've generated a new master key by, uh, typing editor VM rails credentials added. So now I'll type it once again, and as I got a master key, it can be crypt, uh, decrypt credentials via mail. So I'm going to exit fim and okay, I've had a look at the GEM file. So just a simple rail seven application. Let's have a look at our rules. So we don't have device, we don't have, uh, current user as I see, and we've got just a few, uh, different rules and we've got an admin namespace. So what is this admin name space, maybe we have some kind of admin that we can log in with. Let's have a look. I'm going to our app controllers and here we've got this, uh, admin orders and stats, and they belong to secure controller. So secure controller. Here it is, we have some kind of basic HGTP authentication. So to access the admin pages, we will need to use this, uh, username and this password. Okay, now let's try running this application locally. So I've, uh, run bundle, run rails, db, create, I've run Rails, DB seed. If I look into the seeds, there are actually some seeds here. And let's try run this application locally. I will start the server by saying rail server and let's have a look at it. So this application is a basic, uh, like menu, online menu, order ordering application. So, uh, here I can select a few things, add them to cart. So I will add like three bounties. I will add one split like this, or two, I'll add some ellos. I'll go to my cart. Yeah, here is my cart. And you see I have a bunch of items in my cart. I can remove some items if I want or I can complete my order. And if I complete my order, let me display. So if I click complete order, then it appears in the orders in progress. And you know, like you go to McDonald's and uh, you see all these screens with the, like the orders that have been prepared. So the same way you would be able to see orders in progress. So, uh, there is one that is done and one that is, uh, submitted and users that have access to this admin, they can go to manage orders. So here I will, uh, go to manage orders and I can, uh, change the status of the process, uh, of the order. So like I see, uh, from the admin side, okay, this order has appeared. I can have a look at the items in the order and I can say, okay, I'm going to start working on this order. So I update the status and everybody who's uh, ing this page of the queue receives a broadcast Adobe broadcast that the status of this order has changed. So I'm changing to delivery and to dawn. And you see there are already zero orders in progress. So the application is using the hot wire broadcast for delivering this, and Hotwire broadcast require Redis. This is another important thing, uh, you need to know, uh, to be able to run a Rail seven application that uses Hotwire. And the more thing, if you look to at Gem file log, you would have a look at, uh, the platforms and you would need to ensure that you have, uh, uh, the Linux platform, otherwise you would have, uh, problems with running the application. Okay, enough about this application. Let me comment the changes that I've created. So here I've got this updated credentials file. Let's, uh, save it and push and name of the application on GitHub is QR menu app. So let me finally start, uh, this application on the digital app Ocean Apps platform. So I'm going to create, uh, I think I've done here already. Yeah, I'm going to create, I'm select GitHub, and uh, I will select QR menu app. So I'm going to use the main branch and let's click next. Now, I'm also selecting auto Deploy. And uh, actually if you don't select Auto deploy the main branch, then I'm not sure how you can, um, select it once again. So usually it's better to have auto deploy. I'm clicking next and here are the resources. So, uh, this is what you're going to pay for. So I will go and click edit plan. Okay, everything looks um, fine here, let's go back. Uh, yeah, here is added plan. And you see by default it says this one, but the, the one that is $12 per month, but you can, uh, select something cheaper. So, uh, you can select this basic thing and by default it's $10 per month, but you can select a cheaper tier. So $5 per month. So if you clicks and you select the cheapest tier, and that's actually enough to get you going for the beginning, you can always, uh, scale it up. Now I'm going back and I'm going to add some resources. So, uh, you can say detect from Source code, but usually I would, uh, explicitly say that I want to add a database. So I would say Add, and it's going to be a, uh, Postgres scale database, and the cheapest one is $7 per month. And uh, you see it's not, uh, a production ready database, but it is the cheapest one. The, the cheapest production Ready one is around, uh, 12 I guess dollars per month. So anyway, $7, I'm saying, uh, grade and attach. So we've got our application and we have got our database. Okay, and, uh, let's continue. I'm going to click next and we've got environment variables. Now this is also really important. So, uh, for our application we are going to add our rails key. So, uh, if you go and search for Rails Master Key, so the Rails master key is the one that you've got in the, uh, master dot key. So you will need this key. Without it, your application will, well just not strong. So we've got the Rails master key, but it's not the only variable that you'll need. Uh, let's actually have some inspiration from a Heroku application. So I will take, uh, one of the Heroku apps. I've got, uh, let's take, um, this one for example, I'll go to settings, uh, reveal configure variables. And you see, uh, Haruku did a lot of stuff for, for you that you didn't have to think about. So you will need to set these things yourself on Digital Ocean and on any other place where you are deploying your code. So we are going to set our, uh, rec environment. Uh, yeah, let's actually just say bulk editor. So Rails master the key rec n equals production, then Rails n equals production. Then uh, rails log to SDD out enabled. If you don't, uh, make it enabled, then you will see no logs. So enabled then Rails serve static files also enabled, otherwise UCSS and GS will not load. And we have our, uh, rails Gnostic key and our secret key base is available inside our Rails credentials. So we don't need to serve the secret key base separately. Let's again go to editor BIM Rails credentials edit. And here is our secret key base. So they're not serving it separately, but uh, if you're not using real credentials, you could just serve the secret key base. Anyway, uh, we have added these uh, environment arrivals, so the master be key, uh, and a few settings for our application. And let's click save. Okay, and uh, again, I'm clicking save and click next. So now you are deciding where you want to deploy. Uh, it can be London, it can be any other region that is closest to where your application users are. So you should think of uh, choosing a region that is uh, close not to where you are, but to where the biggest quantity of your users are. That way they will have the best experience. So anyway, it can be London for me, that's fine. And I'm clicking next. And you see we've got our application, we've got our database. Same on Heroku. You would have database URL, but it'll come back to that later. And you see it says, uh, it would be five plus $7 a month and I'm gonna create resources and application is being traded and is being built. Okay? So it took around seven minutes to deploy and uh, here it shows that uh, the build was successful and that the deployment was successful. So, uh, it says deployed successfully and going up, you see it was provided a URL and you can also click live app. So if you click, it says, so something went wrong. Now let's see what went wrong. I will go to runtime logs and they are available. These runtime logs are available because uh, we added the environment variable, uh, to send the logs. So I'm going to set ins two components, uh, to the application component, QR menu app, and uh, he have this rails log to out enabled. If it was not enabled, then the runtime logs would not be updated. But again, what's the problem? Active record connection not established. So, uh, we don't have the connection to the database. We can also try to check the connection to the database by going to their console. So this is kind of a web console for your Rails application. I'm going to Rails console and uh, let's see if something does get started. Okay, we've got the rails. Let's uh, try getting something from our application. I'll go to Schema for example, and we have menu items or we have orders, let's say order.com. And you see the connection is not established. So the problem is that we have created a Postgres scale database, but we did not attach it to our application. So now we need to attach our Postgres database. Um, we can go to our database in our settings. So we go to our database and here we see that it is yes that database and we need to get the connection details for this database. So you see on Heroku we have this database, URL and there some kind of long URL. So we are going to get the same kind of URL for our digital ocean database that we have created. So I'm going to disconnection details and click view. Here we have the connection parameters. I'm going to use the connection stream. So here we have the connection stream. I'm going to copy it and I'm going to add this connection stream to our application here. So I'm going to this other component, QR menu app. And in all these environment variables, variety, environment variables, I am going to add the this database, URL. So here it is and I'm going to just say that is the database URR, okay? And uh, now it is going to redeploy the application. So you see it's building the application again because each time you change something in the settings, it's going to redeploy the application. Okay? And again, the application is deployed, so let's try going to the live application. And yeah, we still have this that, sorry, but something is wrong. So let's have a look what's wrong this time I'm going back to the runtime logs of the application and what do we have? So, uh, statement invalid relation orders, that does not exist. So here the problem is that we just need to run rails to be migrate. So the easiest way would be to go to the console and here you would say rails be migrate, but the solution would, uh, require you to run Rails to be migrate each time we deploy with new changes in database. So I'm not going to do that. Instead I'm going to say rails be migrate inside the settings of the application. So each time when we deploy the application, they're going to run the migrations. So here I'm going to find again going not to the app settings, but to the component settings. Here is our application component. And here I'm going to find the build command. So here is the build command and the run command. And inside the run command, before running the rail server, I'm going to say, uh, uh, rails, they be my grade and they should do it. So the expectation is that each time before the application is started with the new deployment, we're going to run Rails DB migrate. So I'm going to save. And again, considering that we've done some changes in the application settings, it is redeploying the application and I will expect it to run the migrations. We can go to the deploy logs and uh, monitor what happens. Okay? So once again, it says that the application has been deployed and have see our build logs and our deploy logs. And if I go to our deploy logs, we say that the application has been started, but before the application has been started, all these immigrations have been drawn. So the immigrations have been drawn, okay, I'm going back to the application and I will see, yes, I see the menu. Now of course, I didn't have anything seeded, so I can actually try see it, uh, now and here we also see there was an error once again. Let's see what kind of error this falls. I'm going to the runtime logs of the application. Yeah, let's give it a moment. Let me refresh and uh, I'll refresh once again. Yeah, apparently it needs some time. Maybe it's because I've just added a new application, okay, this time it seems to be working Okay. So yeah, here's some kind of error. And there error is with SOT connection. So I can assume that it is because I don't have red running. Yes. So see here I see the problem with red. Why do I have the problem with the red? Because then going to this menu page slash menu, uh, I have a tool of broadcast from this page. So let me go to menu index in the code. And here I will have, uh, or maybe not here broadcast. Let's see if I have, uh, broadcasts, uh, turbo stream from, okay, so on the order page and on the uh, queue page, so I have a turbo stream from uh, orders. And to establish this double stream, we would need the uh, red dis connection. So the application doesn't work now, uh, because we don't have, uh, red disconnected. So now I will need to connect the register the application. So to connect red this I will need to go to databases and create a new database. So here I already have one database. I'm going to create a new database And uh, yeah, the region is London, that's fine. Uh, I already have a Postgre scale database and now I'll need a red database. And we see that unfortunately the cheapest plan is $15 per month. So it already puts our price to uh, five plus seven plus 15. So that's $29 per month. If our application does need Redis. So if our application is using the Gem credits or um, sidekick, or if we're using the top stream broadcasts, then we we'll need Redis. So yeah, the name of the Redis cluster, let me name it QR menu app, Redis. We don't need any tags or actually we can say QR menu app. Okay? And I'm creating this database cluster. Now, it'll take a few minutes, uh, possibly to create the application, but we already have our connection parameters, so I'm going to take the connection stream, I will copy it and I will add this connection stream to our application. I'm going back to apps. Um, here is the app that we just created, going to settings, going to the settings of the Rails application. And here I'm going to connect the Stratus, uh, uh, database that we are creating at the moment in our databases. So here we have six environment variables. I'm going to add one more. I will say Redis, URL and add this variable. And let's have a look at web, have the Stratis, URL in our code. So I will search for this CRL. And here you see we have config cable via ml. And here we would look for the red CRL in our environment variables for production. Okay, so uh, I'm going to click save. And now again, it's trying to redeploy our application. I hope there will not be a collision that our database for Redis is uh, not ready yet. You see, it's still being prepared, but our application is already being deployed, so possibly I will have to redeploy or forced redeploy the application once more. Maybe I should have first waited for the Redis application to be, uh, fully created and then connected to our, um, app, uh, because now it's trying to redeploy our application and it might, uh, give an error. But anyway, let's wait and see. Okay, so now I see that the database has been deployed. Let's have a look at our application. Okay, not droplets, but I should have gone to apps. So going to apps, here's our app. The app has, uh, well also been deployed. Let's, uh, open the app and yeah, it seems to be working. So let me open the app in a new tab and see if the to broadcast part that uses Reddits actually works. But for this, I would need to add some records to the database. So I will need to run the seeds to run seeds. I'm going to go to the, uh, console and say Rails the B seed, and this should, uh, well run our seeds. So yeah, it must have worked. Let's go back to the application. I will refresh and you see some records have been created. So I will, uh, try to add some records to my card. So add in a few of these. You see there is no full page refresh. Looks fine. I'll go to the cart. Uh, on the other screen I'm going to the queue. I will say complete order. And you see an item has appeared in the queue. So read this and Tobo stream broadcasts are working, looks, uh, quite satisfying to me. So yeah, here is how we deployed Rail seven application with the Postgre Scale database and the drug to production. But let's go further. Let's add a custom domain. So, uh, at the moment we don't have a domain and if we go to the app settings already, not the application, not the Rails application settings, but the whole app settings here, we have these domains. So let's add a domain. Uh, let me go to name Chip and here I have a few domains. So, uh, let's see. Uh, I will connect this domain, let's say very 30. I'm not using it for anything at the moment, I guess let's just check, no, I'm not using it. Okay, so I'm going to connect this domain er.com. So I'm going to our domains. I'm going to add a new domain. So it's going to be very cty.com, like very certified or whatever. And uh, uh, I manage my domain and I'm going to create a CNAME and add this LAS. So I'm going to do the main settings. I'm going to advance DNS. And uh, here I already have some kind of, uh, CNAs. I'm going to delete these and I'm going to create, uh, a few new ones. So these ones were using Hero previously, and these ones are used to, uh, get the domain verified on Amazon. So I'm going to create a C name, uh, it's going to be at and, uh, the Digital Ocean application. So at is when they're using the bare domain. And if you're using www, then we'll add the www on. So I'm saying add Domain, and yes, I'm also going to add the www to direct to the same, uh, application. So, uh, I will say www.veryc.com. So again, I manage my domain and it says www CNAME should direct to this, uh, uh, coral app. So I'm going back to name Chip, going to create a CNAME record www. And here's Dogg, save the Changes. So I'm done with name chip and uh, let's click add domain. And the domains are being added on the digital Ocean side. Now you see it's deploying once again. So let's wait for the deployment to be complete. Okay, it says it's complete. Let's go to the live application. Now it says, uh, it's got some trouble maybe because it, uh, requires a few minutes for the, the settings to be updated and for it to work. So here, sometimes when updating like the main related stuff, you just need to wait for a little time. So let's wait. Okay, so the domain still doesn't seem to be working and uh, I've been waiting for like 20 minutes now and uh, I've tried pinging the CNAME of a domain in some kind of DY check tool. It does find the domain name. So really it can take, uh, longer to make the domain work, but from the setting perspective, we've done everything that we needed. We've added the domains and uh, if we've added the CNAME records, so we just need to wait until they get refreshed. And a final touch to our deployment would be log forward in. So log forwarding is here, and what it allows us to do, it allows us to connect a specialized application to which we would send all the logs of our application. So all the logs that we see in runtime logs, you see now I click runtime logs and we see just a tiny bit of, uh, history and, uh, different applications like Datadog or Paper Trail allow you to keep a big history of logs and to potential analyze these logs or search for something specific in your logs. So a good practice for production applications would be to use log forward in the tools like Paper Trail for an example. So Paper Trail by default does have a free tier and they're going to connect it. So here, if I go to my dashboard, I already have just created an account on Paper Trail and I already have one application connected. I'm going to create one more, uh, paper trail application. So I'm going to go to settings and I'm going to add a new log destination. So, uh, I'm going to click Create Log Destination. Uh, I will name it, uh, how do I name this application, uh, QR menu app. So QR menu app, uh, allow logs, uh, click and create. And here we have this log destination. So it is 52 38 4. And I'm going to add this log destination in our application settings. So I'm going, uh, here, going to our application settings, log forwarding, maybe actually do it on our, uh, yeah, app level settings. So here I'm going to go to log forwarding. I'm going to, uh, add Paper Trail and the Endpoint. So I've added this endpoint and uh, now I expect to be able to go to that dashboard in Paper Trail here. I, uh, must see already not one system, but two systems maybe I need to actually send some kind of logs. So I will, I will try again to refer this, this application. Okay, it still doesn't work. So I will go to this, uh, domain name that was provided by, uh, by Digital Ocean. Where is it? Domains. Here's this domain. I'm going to this domain and uh, well, let's try pushing some logs. So I'll also open the runtime logs here. So, uh, let me, I don't know, add something to the code. I, Yeah, you see, it's not super comfortable to see stuff here, but uh, you see there was a Adobe Stream rendered menu item 88. I will click once again. There was again a top stream. I will click, uh, once again. Okay, it's been a bit, uh, slow. Maybe because the application has just been deployed for, well the first time it's still not working on full speed. So I guess that's okay. So let me add some more items, uh, and see if we see them in the locks. I'm adding this item. You see we have many item I nine, uh, top stream. And let's see if we already are sending this logs to Paper Trail. So I will refresh that dashboard once again and I still don't see the second system. Interesting. Okay, so now I do see how this Coral app from Digital Ocean in our paper trail account. And what I had to do is go to the Digital Ocean application, go to actions for Build and Deploy and Deploy. And when I did it, it send information to Paper Trail and Paper Trail registered this, uh, second system, the Coral app. So now if I go to this Coral app in Paper Trail, I will see the whole log history and let me try something. I'm just going to the application here. I'm going to refresh it. I'm going to and click on a few pages and you see all these logs are being registered in Paper Trail. So you have the whole log history and that's about it. I think it was quite a long video, but, uh, covered a lot of stuff. So we've created a new app on Digital Ocean app platform. We've set up a red database, we've set up a regular database. We've set up, uh, the domain names, we've set up log forwarding, and importantly we've set up our application variables. So, uh, here we have our environment variables, the rails tic key, the environments, the log for paper trail, and for being able to see the logs in runtime logs. We've set up the rail self static files so that the CSS and Gs are bundled. The database URL and this URL so that the, our Rails application on Digital Ocean can communicate with our Redis and the, uh, and with our, uh, postgre scale application on Digital Ocean. And we've also added added rails DB migrate before Rail Server on the run command so that we don't have to run Rails DB migrate each time we deploy. And that's about it. Thanks for your attention and have, uh, a great experience deploying your Rails applications in the future. Goodbye.
1
Join the conversation
Sign in to access PRO lessons, access private repos, leave comments, create watch lists.
We collect your email address, name and username to create your account. We do not share your email address with anyone else.