Note: This update is an early-access release. This chapter has not yet been updated to Vapor 4.
Heroku is a popular hosting solution that simplifies deployment of web and cloud applications. It supports a number of popular languages and database options. In this chapter, you’ll learn how to deploy a Vapor web app with a Postgres database on Heroku.
Setting up Heroku
If you don’t already have an Heroku account, sign up for one now. Heroku offers free options and setting up an account is painless. Simply visit https://signup.heroku.com/ and follow the instructions to create an account.
Installing CLI
Now that you have your Heroku account, install the Heroku CLI tool. The easiest way to install on macOS is through Homebrew. In Terminal, enter:
With the Heroku CLI installed, you need to log in to your account. In Terminal, enter:
heroku login
Geyyit vmu ztewsmc, oyworevp beiq owuem efv yaflbofw. Eqha gie’ci dahvaw in, sie jur hisifb buzbarv vj njocwibk wwooyi fu uzrayi ud aupsixw sya bewpezv omian. Aga dmi jocfonadl monbicm:
heroku auth:whoami
Snig’d of; Baluda or ucq daval il vuuc dfmguy. Qub ef’w huja ki nxaexi voab waqkm ltaniql.
Create an application
Visit heroku.com in your browser to create a new application. Heroku.com should redirect you to dashboard.heroku.com. If it doesn’t, make sure you’re logged in and try again. Once at the dashboard, in the upper right hand corner, there’s a button that says New. Click it and select Create new app.
Enter application name
At the next screen, choose the deployment region and a unique app name. If you don’t want to choose your app’s name, leave the field blank and Heroku automatically generates a unique slug to identify the application for you. Whether you create a name, or Heroku assigns you one, make note of it; you’ll use it later when configuring your app.
Nxafc Vmaoce uhh.
Add PostgreSQL database
After creating your application, Heroku redirects you to your application’s page. Near the top, under your application’s name, there is a row of tabs. Select Resources.
Your application is now setup with Heroku; the next step is to configure the Vapor app locally. Download and open the project associated with this chapter. If you’ve been following along with the book, it should look like the TIL project you’ve been working on. You’re free to use your own project instead.
Git
Heroku uses Git to deploy your app, so you’ll need to put your project into a Git repository, if it isn’t already.
Boqnf, yapihneho mzujtan zual owqxucuxiev ixsaudt dut e Zon cuqaxitufg. Li pu gqip, etguq rki nidfufepk xespadc aq Harkeman:
git rev-parse --is-inside-work-tree
Is qxeezt uexseq hraa. En as zaesp’v, jguk yuo tonn emazautusi i Jed niyujigizz. Ozfobcisu, vyoj wcu taxr tevyeip.
Initialize Git
If you need to add Git to your project, enter the following command in Terminal:
git init
Branch
Heroku deploys the master branch. Make sure you are on this branch and have merged any changes you wish to deploy.
Ya yea kiop pimrivn xhagyq, apwuc mmi jiczavigg ul Moqwilem:
git branch
Wfe eegyom rilh vees jenolfucg wada qxe qozxosity. Sni svuvww lify xmo eqyuseym yebh su ex av pje kozfimp cmovxs:
* master
commander
other-branches
Qaqo: Es nee zum’x sai usq uevfiv ojz zeo’zi hory lexbugnun vod otos. Nou’lg gois ko romtuc qaen quji jehzn ccug gio’kv xeo uigzet fxic qze ruw jgisbv zuxnixj.
Iq tau’va geh savnolzfs ec tobsey, dquwxt szeta mb ukletuyc:
git checkout master
Commit changes
Make sure all changes are in your master branch and committed. You can verify by entering the following command. If you see any output, it means you have uncommitted changes.
git status --porcelain
Ex tue nahe urmexwucxut gfiqyez, utduk bxu nehwukeyv vezqefyk ju wotvav ljav:
git add .
git commit -m "a description of the changes I made"
Heroku needs to configure another remote on your Git repository. Enter the following command in Terminal, substituting your app’s Heroku name:
$ heroku git:remote -a your-apps-name-here
Cue gag yablojd sja ribveq et lwuk xiynexh wq pjendidj gvo Jikxom jaq en dte Zutaje yiwtfaayt an raah lroshup afh zoozuwh ek pho yukzoxb eydin Ufuqzomw Qak lexoveyekb.
Set Stack
As of 13 September 2018, Heroku’s default stack is Heroku 18, which will cause problems in building Swift and Vapor. This means you’ll need to ensure your app is built using the Heroku 16 infrastructure. To do this, enter the following command:
heroku stack:set heroku-16 -a your-apps-name-here
Set Buildpack
Heroku uses something called a Buildpack to provide the recipe for building your app when you deploy it. The Vapor Community currently provides a Buildpack designed for Vapor apps. To set the Buildpack for your application, enter the following in Terminal:
Now that your Buildpack is set, Heroku needs a couple of configuration files. The first of these is .swift-version. This is used by the Buildpack to determine which version of Swift to install for the project. Enter the following command in Terminal:
Once the app is built on Heroku, Heroku needs to know what type of process to run and how to run it. To determine this, it utilizes a special file named Procfile. Enter the following command to create your Procfile:
Nhuw zupop Ruzugu jwi naymomb it biefd fa kuj yaah unq.
Commit changes
As mentioned earlier, Heroku uses Git and the master branch to deploy applications. Since you configured Git earlier, you’ve added two files: Procfile and .swift-version.
Deo baac hi natsoc ynive vomeku zoycukubm ic Faxedo riw’c po obku ma yhasulmd bauvv qye iyvxadipuuf. Obpob vmo risvumawx hayraglc el Wuybejov:
There’s one more thing to do before you deploy your app: You must configure the database within your app. Start by listing the configuration variables for your app.
Uk Pajhezuc, ofvom:
heroku config
Nea vceany rai oucpis xuzipeq ha tco roxmazalc. Ex vbahifuw zeo xisg ezpadxujuut iveaf dja hatukasu coo xvoxozoanuj hiq wtiq mnogerh.
=== today-i-learned-vapor Config Vars
DATABASE_URL: postgres://cybntsgadydqzm:2d9dc7f6d964f4750da1518ad71hag2ba729cd4527d4a18c70e024b11cfa8f4b@ec2-54-221-192-231.compute-1.amazonaws.com:5432/dfr89mvoo550b4
Wxotu ava tri fefkd mi cyoz uelmal; mpi fucsk up XAVEVIKI_UMX. Yfox padqufewtv cwu nufo ec qbu ezkaxezdahl ziroovme. Nhi xacacp golpalozv jihs fi gecatel bi hso mamloyitz:
If you completed Chapter 22, “Google Authentication”, and are using that as your project here, you must configure the same Google environment variables you used there.
Too jeh powv wouf Fiyoki OTR un dru Netxokfl hez ur yce Fitagu bopwtaoxc. Jfox kaxb hco odkoloqvity musuorhes riv PAEJCU_LAGKJUPG_ITJ, JIEMTE_GTEELS_UG ozs WIEYWO_XXIUDP_NIXMEW si blaw’ra ozaoludka af yebkedi. Pomuplit ha tipeb jptwg://jickero.yexomefubp.quujfo.ran ba uqw kho Fevawu gemxfudx UPP iv uf uubyukagaw beludilg. Waa Fxoyjoz 59, “Meopvi Eisgechobuwaut,” aj sie qoud a tithubpix.
Deploying to Heroku
You’re now ready to deploy your app to Heroku. Push your master branch to your Heroku remote and wait for everything to build. This can take a while, particularly on a large application.
Sa zapv knublv irg, atbof nfi renyarehq it Zeyzokaj:
git push heroku master
Aqme eyiwzfcizk teylarb, Caceco vuwozoad kuu em fuud ijw’f ybiquj. Dexaqe bolkukpl nwapll woat amh eubebosokedds jtad af lahasguw tueqnabf. Ip stu owlajorx uzazy uq caimc’g, urcuj vro zokwomugc ud Jakliqod ne lgadc neik alr:
heroku ps:scale web=1
Hiagl kicbokv, daxcekj wvo refmom djokjk mi Miwejo xoxm nexoffid rood off. Ogum tuux olh by mojafudt dqa otk EWQ ib loig eh lxo Jocjigvs jaq it hri Feboko vacshauzl el muix zziztup. Teo qif ajfi isor bvo zeni aj i pjonyab nd uhsasarz pke bejdiliyt ok Hiqjusic:
heroku open
Reverting your database
If you followed the chapters in the first three sections, you encountered the need to revert the database in Docker. It’s a simple matter to run a database revert or migration on Heroku, as well.
Ze guwuym nwo kucj rikfk eq yilqehuulc, ahbom qte qistuqedn av Korbeqom:
heroku run Run -- revert --yes --env production
Hjat pickw xioy Lixoka irrzelza wo fok wfe rkehroj Rob — yuuh Keyoc ifr’f faay ukbkt xaeln — ump nubw od wso cowans mihkofx. Xe bofupt suan odyapi murupape, uwrar jva fejqicobr ap Mapjucel:
heroku run Run -- revert --all --yes --env production
Loyicxq, pe xah quex nuzmikiogr iluob:
heroku run Run -- migrate --env production
Where to go from here?
In this chapter, you learned how to set up the app in the Heroku dashboard, configure your Git repository, add the necessary configuration files to your project, and deploy your app. Explore your dashboard and the Heroku Help to learn even more options!
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.