Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Deploying to Heroku7:37 with Chris Ramacciotti
Knowing how to deploy an app to a cloud server is a seriously powerful skill. In this video we leverage the Platform as a Service (PaaS) model and deploy our application to a publicly available Heroku server.
Heroku Sign Up
Go to https://signup.heroku.com to sign up for a free Heroku account.
Heroku Toolbelt Installation
Go to https://toolbelt.heroku.com, choose your platform, and follow the instructions for getting started.
Using Github With This Course
You can complete this course by using the code I've made available to you in two ways:
- Use the Github repository I've made available (recommended), or
- Use the project files linked at the bottom of this page
If you choose the recommended option of using the Github repository, it can be found at
To utilize Github with this course, you can download the Github desktop client for your system or use the command line interface provided with Git.
Clone this repository to your machine using the Github desktop client, or using the following command:
git clone email@example.com:treehouse-projects/spring-deploy-weather.git
Knowing how to deploy your app using some sort of cloud service is seriously helpful 0:00 if you want to make your app quickly available to others. 0:04 Heroku is a cloud application platform that makes it easy for 0:07 developers to deploy web apps to Heroku servers 0:10 without having to spend needless time configuring and managing those servers. 0:12 The idea is that companies like Heroku can provide nicely configured, 0:16 low-maintenance platforms from which a developer can run applications. 0:19 This is the basis of the platform as a service model or 0:23 PaaS as you may have seen it abbreviated. 0:26 Now, in order to use Heroku, you'll need to sign up for a free account and 0:30 download and install what's called the Heroku Toolbelt. 0:33 The Toolbelt will allow you to configure and 0:36 deploy a local application to Heroku from the command line. 0:38 So check the teacher's notes for 0:41 instructions on getting started with Heroku. 0:43 Pause now if you need to complete those tasks. 0:45 Now, assuming you have a Heroku account with the Toolbelt downloaded and 0:48 installed, let's head to IntelliJ to alter our code base to get it ready for 0:52 deployment. 0:57 Okay, here I have a fresh copy of the weather app locally. 0:58 You can just clone the repository we used earlier to another directory if you like. 1:01 So the first change we'll need to make to the project is to add what's called 1:05 a Procfile at the root of the project. 1:07 Let's do that now. 1:09 So New > File, and I'll call it Procfile. 1:10 And notice that I'm not using any file extension here. 1:14 The Procfile is what will hold the command that Heroku should 1:18 execute when starting your application. 1:21 Here's how it looks. 1:23 Let me type this first, and then I will explain. 1:24 So web: java -Dserver.port=$PORT 1:28 -jar build/libs/weather-0.0.1-SNAPSHOT.jar and 1:33 let's make sure we get that correct so 1:41 that we don't run into problems when trying to deploy. 1:46 So you can see that this is just like a command you'd use on your local machine to 1:54 execute a runnable jar file created by the Gradle builder jar task. 1:57 This web keyword here tells Heroku that this is a web process and 2:01 as such should be tied to Heroku's HTTP routing. 2:05 Now, when Heroku sees a build.gradle file like we have here, or the Gradle wrapper, 2:09 it grabs all dependencies by running a Gradle task named stage. 2:16 To make sure that an archive is built upon deployment so 2:20 that it'll have this archive, this jar file here, available when it executes this 2:24 command, we need to make sure that the stage task actually produces an archive. 2:28 In order to do that, we can configure the stage task so 2:34 that it dependsOn the build task. 2:39 Now, if you don't want unit tests to run, 2:44 certainly you could change the build task to jar instead. 2:46 At this point, you'll wanna open a terminal in the directory of this project. 2:51 Now, I love to use this terminal right here in IntelliJ, 2:55 because as I'm making code changes, I can commit those tasks to my Git repositories. 2:57 So if this weren't already a Git repository, 3:04 we'd want to initialize it as such using git init. 3:06 This is already initialized as a Git repository, I cloned it from GitHub, 3:10 so I don't need to do that. 3:14 And I can start with this Toolbelt command that is heroku create. 3:15 Now, this command comes from Toolbelt and does a couple things. 3:20 First, it creates an app with an auto generated name, like this one, 3:23 sensationnel-gerard, they're kind of silly names. 3:27 And you could specify a name if you like, 3:31 but it'll have to be unique system-wide on the free tier. 3:32 I'll stick with this given name. 3:35 The other thing this command does is add a remote location to this git repository, 3:37 and you see that remote location specified right here. 3:42 And actually, if you cloned the GitHub repository to your own machine 3:46 before starting this video, you'll have two remotes. 3:50 And if you want to see them both, 3:53 you can use git remote -v, that v stands for verbose. 3:54 And you should see one for GitHub, that's called origin, and one for Heroku, 3:59 which is called heroku. 4:03 All right, now let's run that stage task to build our artifact so 4:05 that we can test it locally. 4:08 So I'll say ./gradlew stage. 4:10 And we should see the project compiling and building, hopefully, 4:18 eventually, successfully. 4:22 I see our unit tests are running. 4:24 Again, if you're gonna run unit tests, 4:28 make sure you have those API keys in the properties file. 4:29 And excellent, it was built successfully. 4:33 Next, if you wanna make sure it runs locally before deploying to Heroku, 4:37 there's a command for that, heroku local web. 4:42 And we see our familiar Spring Boot output there, cool. 4:46 Now, this should result in the same running app as you've seen before. 4:53 The only difference in this locally deployed app is that it's available on 4:56 port 5000 instead of 8080. 4:59 Let's switch to Chrome and check it out. 5:01 So I'm going to go to localhost:5000. 5:03 Excellent, and there is our locally deployed app. 5:10 Looks like it's gonna work in Heroku. 5:13 So let's deploy it to Heroku. 5:16 Let's go back to IntelliJ, to our terminal here. 5:18 I'll go ahead and kill this local server with Ctrl+C. 5:21 Let me clear my console here to give me some space. 5:25 Now I'll add all the repo files, 5:27 since this is a new Heroku repo and commit our changes to the Git repo. 5:30 So let's do this, git add **. 5:35 And if you wanna check on the status of what is added or what was not added, 5:40 you can use the git status command. 5:44 Cool. So we've got all this stuff here. 5:47 All right, now what I want to do is git commit. 5:51 I'll say Added Procfile, 5:55 and stage task to Gradle. 6:00 And then we can push our changes to the Heroku remote, git push heroku master. 6:06 And you'll see a bunch of output for the repo being pushed to Heroku, 6:16 Heroku detecting our Gradle app, and 6:20 dependencies being downloaded from the running of that stage task. 6:23 This might take a few moments here as those dependencies download. 6:27 Looks like plenty of them are indeed downloading. 6:32 And dependencies are done downloading. 6:39 So let's wait for the application to be fully built and deployed. 6:42 There, the build was successful. 6:46 You see it detected that we have a Procfile of type web. 6:49 And it looks like our deployment is done, excellent. 6:53 So to visit the app in your browser, 6:58 you could copy that URL from the final output here, that is, this URL, 7:00 and paste it into a browser, or you can use the heroku open command. 7:05 heroku open. 7:09 Now, the first time you launch your app, the JVM will take a minute to fire up. 7:11 But subsequent visits will see fast response times. 7:14 Also, know that there are limitations in the free tier of Heroku. 7:18 So, hey, look at that! 7:22 Our app is deployed to the cloud and accessible from anywhere. 7:23 Now, there's a lot more you can do with Heroku, 7:27 way too much to cover in this measly workshop. 7:30 Check the Heroku docs for some powerful options. 7:33
You need to sign up for Treehouse in order to download course files.Sign up