How to Deploy a Rails 5 Application to Heroku
This guide takes a step by step approach for deploying a Rails 5 application to Heroku. Additionally, we'll walk through how to securely pass in data such as the secret key base to Heroku without checking our secrets.yml file into version control.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this guide, we're going to walk through some of the configuration items that we need to build out in order to get this working. So the very first thing that we need to do is to be able to go and create our environment variables for Heroku. So just like we have our .env file down here Heroku needs to have a spot where it can securely keep its things such as your AWS keys and any other environment variables that you are going to use. So what we need to do is open up the terminal and you're going to run this command you're going to say heroku config:set and then you're simply going to go and copy over all of the items that you have in your .env folder, so it may be something like this where is, says AWS_BUCKET= and then whatever your bucket name is. So pause the video as you're going through this go grab all of the elements in your .env file and then go and run this exact set of commands and then come back and we'll complete the configuration.

With all the environment variables being set. If you want to verify that they work you can always type

heroku config

and hit return and it's going to show you all of your API keys. Everything like that so with all of that being set we only have one more item that we have to implement and that is to go into our ruby gem file and from there we're going to declare the version of Ruby that we want to use and then we're going to be ready to push our application up to Heroku.

I've opened up our ruby Gemfile so if I come over here and go all the way down to the very bottom of the file I'm going to declare ruby "2.4.0" because that's what I'm using here. If at the time when you're going through this you use a newer version and feel free to use that Heroku accepts most versions of Ruby. This is going to be the first one I've ever pushed up for 2.4, so if it throws any errors or anything like that we can always change back to 2.3.1 or one of the other ones that I've used in the past so let's test this out. If I come to the terminal and type

git status

We've only modified our Gemfile file so lets run

bundle install

just to make sure that everything is up to date. Everything looks good. Now we have two files our Gemfile and our Gemfile.lock that have been updated. Let's add these

git add .

and let's commit it by saying

git commit -m "Updated gemfile and declared ruby version"

Now I can say

git push

because we're on the master branch and now that we have this it's time to push up to Heroku. So I'm going to say

git push heroku master

Now depending on when you're pushing this up, what other files you've pushed up, such as videos and those kinds of things. This is going to take from around a minute or so to a few minutes I have some applications that take even longer than that to deploy but on your very first time it can take a little while so I'm going to pause the video wait until everything is pushed up. And one other little note here if you get an error such as saying that it couldn't precompile the assets which could happen. I haven't pushed this up yet so I'm not sure. That usually means that you have a syntax error in either your CSS files or in your javascript files and it's pretty good about explaining where that is happening. And it could be something as simple as forgetting to put a semicolon in place or something like that so if that happens to you simply go to the file where it found the error, change that up, save it, push to github and then run git push heroku master again and you should be good. So I'm going to pause the video now while all of this is installing and we're going to finish the installation when I come back.

And we're back and it looks like we have a little bit of an error so let's scroll up and see what's causing it. So right here this is where it says rake aborted this tells us that right here is where the bug is. And it says Devise.secret_key was not set. Please add the following to your Devise initializer: where it says config.secret_key and has a long key now do not listen to that advice that'd be a horrible piece of advice because our Devise initializer is set in version control which means that anyone who has that would have your secret key for Devise. Instead, let's take a look at the Devise file. If you just open up your sublime and devise.rb and the line they're talking about is Line 9 right here

large

and you may be tempted to simply uncomment it and then you would be able to have that working. But technically that's not right. The reason for this bug is not due to this line by default Devise is going to look for your applications secret key but remember that we removed the secret key so that it would not be checked into Version control which is definitely the right way to do it. But what the repercussions of that are that in our application. Heroku doesn't actually have a secret key so that's the whole reason why we're having this bug so don't worry about this file there are a couple things that we need to do in order to get this working.

First, let's open up our application.rb file and what we can do now is right under our lib path here. We can actually add in a call to the secret key. So right here I can say

config.secret_key_base = ENV["SECRET_KEY_BASE"]

medium

And so what this is going to do is by default our secrets.yml file is the thing that tries to set this but because we don't have it in version control. That means that Heroku doesn't know about it either. So by placing this in our application.rb file. Now we can call this and what we need to do now is we need to now configure this inside of our terminal so we can do

heroku config:set SECRET_KEY_BASE=

Then go and get the secret key base that you want to use. Now the most common way to do it would be to simply take your development secret key base and then change up a number of the numbers and letters and those things so that it's unique it's not the same one that you have in development. Hit return and then Heroku will then have your secret key base and then when it looks for it right here and also when device looks for it then it'll actually be set and it'll be stored in your configuration. So let's come here. I'm going to pause the video really quick. I'm going to paste this into the terminal I'm going to run it and then come back and we'll push everything back up to github.

OK. I opened up a different terminal and I pasted all of that in. And now if I type

git status

the only file we changed was the application.rb file. Now I can say

git add .
git commit -m "updated secret key base call"

and now run

git push

And now we should be able to say

git push heroku master

Now all of this should be stored there. So we should be able to now have everything on Heroku along with having our secret key base and the correct calls to that. And the nice thing about it is we're still following all of our best practices from a security perspective. You really do not ever want to expose even your development and your test secret key bases to the world. That's the reason why it's considered a best practice to not have that in version control. So I think it's a good idea to not add that in. That would be the easy fix to this but the easy way many times is not the right way. So let's see we have this working and it appears that everything has been pushed up so now you can run

heroku run rake db:migrate

And that will migrate all of the database items which is going to be required in order to get all of this working. And yours, depending on what kind of setup you have, you may see different logs you may see more than I have right now. I have a number of my log items turned to low on Heroku so you don't see all of them. But now if I say

heroku restart

I like to run a restart here just to make sure that it has all of the information that I need. Now with all of this in place we should be good to go. So cross your fingers come up grab your url

large

and come into Chrome paste it in and hit return. And look at that. The application is live on the web. This is no longer local. This is now totally live and if you start clicking around you can see some of the other items like our job history education. That means our javascript is working. Now if you're wondering why we don't have skills here it's because we haven't added those to the database remember those are calls from the database so nothing's going to be showing up on contact. It looks like everything's there is working it has all of our fonts. Our blog is working. Obviously we don't have any blogs yet so we're going to have to add those in, on the portfolio right here it looks like it is properly grabbing our fonts even when they're stored locally and we'll have to add our own portfolio items. But no problem there we already were planning on doing that. So it looks like everything is working properly. Let's try to register a user so I'm going to go with my own email address and verify that this part is working. And this time I'm actually going to go a little bit harder password not my traditional asdfasdf since this is a live site. So if I sign up it appears like everything here is still working. One thing that you should know is this user is a regular user it's just a traditional one so I can't see any of my admin controls or anything like that. And so in the next guide now that we have everything working and it seems like we're good to go in the next guide. I'm going to go and I'm going to show you how to use the rails console to do things such as creating our skills and creating and updating users so we can convert our user to be an admin user. All of that kind of stuff but just really quick I just want to pause and say congratulations. If you'd made it this far you now have your portfolio. It's not just local. You actually have it live on the web. Fantastic job in doing that and coming this far. This is definitely some cool stuff that should be. I hope as fun for you as it is for me. So I will see you in the next guide where we dive into how to work with the Heroku console.

Resources