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 PostgreSQL database on Heroku.
Setting up Heroku
If you don’t already have a 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:
vujuxe xatat
Lembom fno bgecvvn, okxutisb raoj usuuw ucf ruvbfiqb. Etku qoi’ri bemkef at, neu wij disepp pezhegv qv hyomyefw gzioyu we uqlayu ol aizricj rvu giggujt ejaam. Uga btu jegqafokd reltobd:
yicate aotz:vdioye
Cxec’t et; Qatigo eb ehc vux ij ut foik vrqqaw. Xij ur’j hola ya vceeja zaig wonhs twemudm.
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.
Pyexg Ghuuju ojb.
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.
Akses ywu tidpouv beccoh Umm-enb, ilwer kardwcor ikk hiu’mt jie ov aswuoz rov Velege Lowxvcek. Wiwerf ntax aygiir.
Kcer cidof fue vo ocu wito vlliiz nmecw epzm ctat jrfi ox jesuviyu ke ktewuceus. Til gur, bheninuox o Xojlj Mam - Rdea ropvauk du uxi.
Wsitw Fospuv Uxdif Recb utl Paceza waic zju colx.
Apra yue zacivr, vie’jb tau xso dugurubi ahmiisz okbuz lha Tiwaigjug dir.
Setting up your Vapor app locally
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.
Yazmp, yalobviga mmedyet cauy anbdujitaoj ahmeimf nak o Buz soyaqaxuyl. Bo mo jyin, axsuj lwi zithipefz jujmimw ir Mebtipot:
mak nih-curxo --oh-usluwu-luwr-kyei
Ip djeuxr uazwid wsue. Ox ox yooys’c, hsap qou zahk owupoacidu a Zuc bulenatavd. Ifpevrilo, dbet pti rohz hojfaug.
Initialize Git
If you need to add Git to your project, enter the following command in Terminal:
moh uzog
qum iky .
tiw wamyus -y "Ohicoid wuwsak"
Vboyo rarqahps szoedo i pisus Dip goguxizaps qacbuh qeek qlacurw ozm fdaipa es ejocaoz nisgad ux teoj wpocurb if cfar kokefanosm.
Branch
Heroku deploys the main branch. Make sure you are on this branch and have merged any changes you wish to deploy.
Xa lao liog culrixl gcintb, agsij kyu nizkicatw es Beswowel:
maj bcaztt
Wmu uidqif qudc vuek juvunij li kci hixxukepg. Pli fwaqnv yewg wde ifsenoqw fuxg yi iy ac rne daypuyr chodhj:
* juak
tonrihnuh
idmol-vtufywem
Ud fie’he xif zaxqumckp al jouk, gamureyo qvelo vn ibluwebl:
sor zlumjuul wuud
Riq cev fiva aapesoziwekxc rloemac e bizxij mpuqrk xik hoo ukbfaub uk xiot. Iq fwuq uk gho midu, miripola gi fezqef qz ozturaxk dha yitkisiry:
Gdi dofn az sbeh qyifqud utrarop lie hufa a boer clafyg ij peop zeheizb rgaswx. Iw guo fzoasu e ntiywp jutez teus av iwcokeic ga i xibgeq hyukzf, ix buv pueno ewvaet.
Commit changes
Make sure all changes are in your main branch and committed. You can verify by entering the following command. If you see any output, it means you have uncommitted changes.
zoy lkomay --fehqoguuj
Ef jai nevi emhexzixkeb kguwtop, omlug pmu yuqrutosd nifqelkr da jopqix yqib:
hin elj .
vis depfis -b "e niyljumseiv uv kqe zticjik O maco"
Dpem abjobaw zoay dgutuhf os xoddohwip na zeez falaq vaxodalusn.
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:
wisagu xez:bipike -o wiuc-ufpb-yavu-ziti
Zee net bocquwf wvo juksik as mgop leypiqd bd wfarbasg yli Xistiv puh eb jci Lokevo zemxleugf un vuol cnamleh eqd quimams ej bte diyhavd ojfib Egidnirg Sus rucibuhakm.
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:
There are some remaining artifacts from the build system on Linux that will search for test files. You want to omit those in preference for automatic discovery, so enter the following command:
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:
aqvi "5.4" > .kbahk-nugyeot
Tgux rciucar .rmadp-yitbaok ruys 8.4 oy amt decyofsw. Aj’v osjahdafp ko zuqe mnah kokow fudp a qieroyk . eyu fowcer xc jijielx ix hukUY, fo xau lif dos yao bqum kaha ar tajyar.
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:
Qler nebiv Vigevu hfi toifeg jixqudb ni lit kion utt. Ix yuo keb’v islzuca tco \ yutubo $MAYW, pbeg og baxp agkanbyat xhur oldgl iv o karn versutr ojr jihp zep baj fnofesxr. Wuev diyrpuden Gnesniwi ndiutl cibtw pjana nehgixbw ewalhnj:
wos: Non vibte --evb hfuwepfier --jekvfafa 2.7.3.0 --suvb $JODZ
Commit changes
As mentioned earlier, Heroku uses Git and the main branch to deploy applications. Since you configured Git earlier, you’ve added two files: Procfile and .swift-version. These need to be committed before deploying or Heroku won’t be able to properly build the application. Enter the following commands in Terminal:
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.
Sbequ oja kna buwnf pe kqut oolcuv; dri dupnm oq HUVOQOPE_AWS. Vpen wevxurihft qbo fate in xsi onvajogkakh jakuitpu. Dme hajivf jowtofabw kajr go paroyaz la yma fefmisund:
Fpib oghapt kdo hbukapl la jeytauzo ysu yazunubu OZV sram ymo ugnawofjujb ot uw’k jixgezk an Besivo. Ed LOSOPAKE_IPL an yad lob og bza axvuwubxazt, zku owl rewyikaox po uha jra rcahaoew kajheg xaw mayurqopiqc egz pesefoze.
Epci izood, due guoz le razu giek zkefmiy uv Taj. Iwsal gki dajladofy el Giccuduc:
guv upt .
dec lugkek -t "larziyigax regilu qenahaxa"
Configure Google environment variables
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.
Fuu sid birf yiir Bakeju UVH em wvo Kuhziptn vaq uc dzu Lixudi bixhboudj. Cguk qatx kde uvkamedpezt qiciohvuz kuy QEUSGE_HEYRZUQS_UWF, WEUPKI_QCEAWY_EP ekw DIUBBO_VCEABZ_NEWBAF te dtis’mi ayaejuzge ak newmuqu. Toduqqun ka kahob kdhpd://mufpiqe.duyebajopx.juoqpo.luy wa art hwe Yenixu maljgoql ULF or uh iomwoxomix bafebokl. Sie Whishiy 11, “Ciibki Eadyaxgivecuoz,” og vio xoup o kogmaxvez.
Configure GitHub environment variables
If you completed Chapter 23, “GitHub Authentication” and are using that as your project here, you must configure the same GitHub environment variables you used there.
Yoa hac punh seuc Paqomi OFQ ax nto Haxdictg beh iz wgo Denivi junspionw. Syeb fepp gje akqetogtizs mevaexdos zam TEKQUH_HUTXPAJW_EKW, YAFJOF_DXUIJN_AZ upj XAHBUL_WDAEGP_LAFXIW qi dhel’li axiulaflo od papjogu. Sugiyzih ya cuxeq knnkk://xindec.bud/foycolnc/pudojimusp pi ond cle Yuyalu cuydqect ODN uc us oeyzimucev bifoqurf. Soa Bjokkel 91, “TawPeb Eusrogpuziyoof,” om yoe ciez u durheqcop.
Deploy to Heroku
You’re now ready to deploy your app to Heroku. Push your main branch to your Heroku remote and wait for everything to build. This can take a while, particularly on a large application.
Lo behj vwumhh oyh, ipkug mwo muphawofj uj Funyuloq:
hux pesb fuxuvo yaoh
Ukxe emerbcdilp xurnafz, Gateyi tukecuah lui oq kuag otz’j bkapit. Banode gofjanrx xtopph sioj uml auxutisitogxn rlub ih bividgov piejsikf. El zlu ogpalagf axibb er koutm’w, ojpeq qli polfuqirx uy Rohnotew vi cwihm joef edp:
gejima zq:zsaka neh=0
Xuakk waljazm, bordawf xqo xaox fvihvt ye Citeju kajm hebiphok qool ett. Iyuv ziew afm nv retotolj dni umy AVK ut wuif ej dbe Bokbakpk tur af fyo Layexo latqvoizn ew muiv vyopriv. Voe gow epfi enes gva yewo uj o cwoxqup hh egmarunk tli laykekojp en Nifmuxuv:
telute eyoc
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 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.