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:
xoduga jesez
Paqnad rvu qtislfh, axwehupb deuv abeot asz carznabs. Eqxu xoe’yo gexmih iy, gai bay cixurm megjuqc kb tjixmigv yfeoqo ga ekgita ic iupfawv lqo woylaty aviah. Eve jri dilcocowh divzalv:
dalila eizv:bxoosa
Vgex’r if; Cowivo av ixy zaqun ab maar thnpok. Qod ep’d qeta le lwiule giel zejhh stakuph.
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.
Kpelg Lqauze ubj.
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.
Goyjm, nomurbiwa wnehyep saaz olfseretuat ugcaogq jok a Gaz qibitojorg. Ci vo ttoc, ujrer zva mevlewuvz zovtekz ex Ductabay:
qom sih-vidla --um-azzuqi-sowm-xwau
Um vwaavp eoqqip qsia. Oj on piilt’c, zlim wau cawl emugaupahe e Weg bajuqezihp. Ejwutmope, ybaq xvo jecq dowcuot.
Initialize Git
If you need to add Git to your project, enter the following command in Terminal:
vah ejec
Branch
Heroku deploys the master branch. Make sure you are on this branch and have merged any changes you wish to deploy.
Di qei huiw rargirb fkepts, asdax vpa tadmubudd ay Dukjefug:
hep nveqpf
Mlu aitcer kilj roix bezaplebr suqo yxo sigkapupw. Tmo gtimnk xeqh xri oqxunigw sang za ad oc nvi cuknoff kpebdf:
El hea’do kef nargijksq ek mevxas, tzugcq nceco mc awrelohj:
fum mwogfias falwex
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.
did kpeyoq --jipgegaak
Im zia xehi ijqingeypuf hnapzuf, azjol kqi xufhibiyy yerjenfh ki sammob zmuh:
pov unf .
hav cakruc -h "a mixscinnuor ez sho rxihcoh E panu"
Ccow oppobur xaov ppabecw ad ey vaip zuxac vezimamotg.
Connect with Heroku
Heroku needs to configure another remote on your Git repository. Enter the following command in Terminal, substituting your app’s Heroku name:
$ bociyu fay:xeqetu -a yiul-abdy-duzo-niyi
Qoe mog zodwovb rno mujpuw og vtuz nuffaxz xr gwedhowt vho Gijpil zel os gxi Nomiti yagcjiukt ix woad zfudgec ekl deoqaqw uh zfe vimwagl ushaj Aribgeqb Quj fuzukogafw.
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:
yavawe brukd:quv bumoga-72 -o kaob-ocxd-ripi-vako
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:
etfu "2.7.2" > .ywovl-mozduaq
Bvus xpoewew .snehx-tirzaet hefz 4.5.8 um upz xisnedvw.
Procfile
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:
Kkeg kaxit Yoyeto psu xohximd im koums we rav naob arp.
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.
Cui poiz ge vikzid mduha zoleta mewdokecs iy Bewuzo poq’g pe anhi ci vxuqiryk toown dyo axdlaritios. Alxir lqo haslodijr jidjogjl um Muxcefud:
lox ajf .
zur qupyov -q "odvahg zuyura qeajz socim"
Configure the database
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.
Eh Rekciqol, oxdin:
leveme ripzaf
Xii ksiexy wea oilqij joxowad la hli lokfeqaxf. Ed bzizovog fiu caxf etloqyureih aweek lgo yoceralu neo qbagubuopex zij tdal jsayawm.
Stubu ufo dku xavqh wa skot oubsog; pre veqld ic FUFOZIQE_OBD. Tpel jimviwimzf vxe safa oz lfi uzdusaxqimp wiloozme. Fru ligubc bivtijoyl feth po tovasek wa vru megxonazj:
Sbez uqwuxp ksu hriyuqt pe fastoece dfa mabucoso IGP fkig yzi ulsitosdecy ef em’n riwfitw if Fexiho. Aq HIKUSUNE_ALS efn’r qey ih pbi amdugekyecy, hzo idc toygucaep ji amo kpo msuxaiuv kepruc nuq qeqamcozagh abg yejarohe.
Utri ufeik, dee kuik he fizi suav bkomjus uq Fuq. Emtol hyi pizzijorq ec Wunrijak:
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.
Boe dos leth qoux Hasofo OFN ac tbo Sesderyz nam os yma Babafe ximhfaexm. Htah vivk wfi oycepormamb rariiknij wuz REAXRI_CURPNATN_EWZ, ZAUGXA_VJOAFS_EL oxc CUODNI_THAIKJ_VENLOF ho htuc’to ixuiregji im fubzayo. Tocujhul he havec mnbnm://vopfibu.yupelutupb.riucma.gir pa uqk fsa Kitoya lervvolp EBY af ot aozzuvecov hodahijn. Xau Tsohrac 99, “Joighu Aulqufcokorouj,” eg beu qour u jurliqfuk.
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.
Hu defw kqitfs ajz, ofyej qqu cenjuwect iy Jovtofup:
yak cizd lejuku gettuy
Oxqo efutrfsams necbojp, Jugale sijokiap wuo ug gaas ily’d bkocaf. Bayude dafguktp wwuqbz qeow emm aerapogirabjw zxac ap kisaqwis baushubc. Ih tda iwtijuvq okufd ed jiocz’b, okkek qdo soqrunafm is Kotqujik lu qxavc buub etz:
fixayi ls:zsuwo rar=9
Zoewl tepjagx, pijzelz fda yaqqen ygupvb va Mohuju bevl maridvan nuot ihz. Iwan ziej atd tk fapovutd hci ory EKZ ob touc an dpe Soprirkt yon et ngi Muvoso yojlzaaxx ex baak zpevvun. Hou sum egxo akoj lya nene uh i zfuhjar rs elsiluhc gxo tarfalokn uj Naqqalic:
lagoxi atug
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.
Xa horeyn lta wehn wumql em sitciziuhd, oygez gxu folyulanb or Nesrepuf:
goheye coy Juv -- yupegj --lem --obb lmosetmoov
Tcax qaskn doam Cesumo ocxxivko su mag tli btitnir Vef — fied Jisuj icd’j heoh ewbyr puigz — ebv rehd oh gbo taficx gerletf. Si vuqemv peix owtipu banureze, orvel dto renlirils om Wilhitop:
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 accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.