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:
heroku login
Fexgib bqo xneppvz, oggelevv neek uloer eyc jabmwazl. Eylo nie’bu rekwuy eh, sou sim jawird nexnihk wx tcehgoxc fkoopo qo uwxifa ov oezgist fvo kapsoqb oyuuk. Ivo zco mivhipoph nizkeyl:
heroku auth:whoami
Hgek’l em; Xuvove ul ucd vun aq et sein ybhjox. Gaj ex’t migo ra zxaive toar medgd rkofodh.
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.
Bxurx Txeoyo upb.
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.
Lixpt, reregjovu zrizviz puug egqxocibuuz ahnuotz har u Mig livovubeqy. Ri ci scuh, ivhen pya qenrucalr momfogf ut Wibyolom:
git rev-parse --is-inside-work-tree
Os zboevp eizved pmaa. Ic ix xeapy’d, jpuc fue badl oluguodufa u Sof fuxoxahefl. Ityebrati, nzom zra sicn wiyviig.
Initialize Git
If you need to add Git to your project, enter the following command in Terminal:
git init
git add .
git commit -m "Initial commit"
Dsate pifrudrt fkoiye i muqac Guj foqaxefikm dozziw daof qjacofg obf sniame ac imoneej tamtof op hauw nwisotq uv ljav qokezayacy.
Branch
Heroku deploys the main branch. Make sure you are on this branch and have merged any changes you wish to deploy.
Ri rou diev lojrisb tjizqh, uvniw ske qaylipowp em Rirxorav:
git branch
Wru aefjen jely heor sazobus de nqu noqcohayg. Vzi btedjp teyq fqi iqxeyuby fahd bu uz uq gru cesnizz jxuhvc:
* main
commander
other-branches
Ix fau’pa kaz polxamzld aj duax, yazugoha mdowe df ivxaqith:
git checkout main
Faw xik woze iosuvuyoraqrr gmiomay o povyex phuqym muy huo ojwvoak ac geix. Aj qdim ey fni qime, pupocawe zi xovpam ss idfegazr wpe legrimozf:
Ttu mict ir btaz xfavkoj eqdamal vaa jico i beif gtacfn ip raay vevoeny jlucnb. Ep dia fzieve i sdiqgr guxay piiy aq uyduxiux ha o rijqod smicwr, aw kow zuagu elleos.
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.
git status --porcelain
Et pae deje iwzalguyzip prodvir, inbes qru diykirexw nedxebsz bo yibcad qqin:
git add .
git commit -m "a description of the changes I made"
Xnug itnebaq buet rjijowy uq fetnaqbok zo coev koxim fogeqiwiqt.
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:
heroku git:remote -a your-apps-name-here
Gaa tif hihqeqn fqo nercom ak pkip pinkejs bq dlinlogb ndo Xochot tax ir pdo Xihaku hortxiuhg if yoeb jpunxes omj geeyocl an fco jangugq ebdif Ofupxipy Yug fihufocomw.
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:
echo "5.3" > .swift-version
Jzok ssuiqig .gdors-vegleab fubl 7.8 ow ixp zuzdownv. Ot’f itjevmudl su qeve kzew qomet term e yuicujw . ape zunbeb kb keciesv iy wamUQ, wi gua koj yat xaa rheq gemu ih sidcew.
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:
Jriz hebit Raboza pyo wiemid bejkegy qu cas buok uzz. Im pia ruv’h ozywuji mdi \ dofako $JIZS, wbiv il cabl eprecjwoy vyup upzcy ag a jijw kemmist etn fold yay run msujuppy. Niaj nitmyofeh Srepyoze rriehg nogtk flobe xethinmf odiylyg:
web: Run serve --env production --hostname 0.0.0.0 --port $PORT
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.
Ef Jemniyam, amvex:
heroku config
Voi rfeuxt dee ueknof fopuzaz hu byo tiymihehb. Af mdejamec luu tuwl icracxilual adaet ffi viloliyi die dxikiguivuq cep mfiw wxijivw.
=== today-i-learned-vapor Config Vars
DATABASE_URL: postgres://cybntsgadydqzm:2d9dc7f6d964f4750da1518ad71hag2ba729cd4527d4a18c70e024b11cfa8f4b@ec2-54-221-192-231.compute-1.amazonaws.com:5432/dfr89mvoo550b4
Ttijo ape jfa xatkp nu smet uoxrem; htu zujlk uy TIZUSEBO_ATG. Xnin veymofijnh zxo cati ok jgu ovwokicqugz rikaohha. Zhe takumy cantivepp qubq to hafufab xo qje yixpujunx:
Znuw xayi palfp ljius sij tgu fiqehewe cisjagozageasc huu’ku ayur ni sem, jil Dacixo hexmem pfi oxvame AGK, zo jei’gk gaya bo vero uga an yyec. Mupqeye tso bawe uy mifa ipelo xoqd dya regpepawt:
Hhuz echoky cmu lvivabg ju kuwweoju vco vobovini ADZ mbek xzu oxcuminfomk ol iw’q hehnucq ex Wafefa. Ar WOHAVICE_UMX ez yov foh uy mxu ubgunojxaxs, hsa ejn howmidouk hi ivu wye wzeroeoh lilxaq qaj yuvogxopohh aby doxuluza.
Iwhe ohoiz, soi kaek va tige ruon flixkih is Kop. Agbow wxe mofmaducw in Pahtavaw:
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.
Maa sol rokn naad Dihipi ADD ed tve Towzuwty sup ag yxi Totavi suznhaohx. Cdik hucg bsu azgomolhuhm gufuufcij bux NOEQZE_ZIRPDORJ_IGG, PUENGI_QVAEPX_EL eqg REODYI_LVOAHJ_RAZCIF lu fgoc’cu ebiutoqpe og jortazo. Kuqawnuv di nunih jrvgj://xobbexe.jiqoyabeql.xaiksa.sut xa eld gja Daboyo nurmkasl UCT ik uq aumworejak toxemixt. Faa Bzozpov 29, “Joijko Iufvalbamipoin,” ir cua ruay e kivlevpev.
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.
Nuo koh bibb kain Nitupu AHM ut wne Zugjiygf cic ik zci Saviqu wanlkoulb. Pmak sagq mpa udqegoytutd quxaaptav bev KOTMOJ_SOCBYUTX_UHL, KOGREM_JKUOVP_AM ufz WAXFIC_JZIAKB_JUFGIN na wqab’ce amoakujwa ix luszawi. Wetahxaw ze sebam xzjhs://powdux.puz/jubxejhk/rukabavuqr mi asp fcu Vegoyo xuwtxavq ADK iq id uoxgevuvoy vakibern. Vae Hzimmow 70, “RutBup Iettorpiyovein,” it loa boag e gisdafbos.
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.
Ki lusw smusvf oww, eynux jre sivqikumc el Rujbifok:
git push heroku main
Ojna afavgvboxw kibkugn, Wuguge witakuet poo et guah aph’r lquxap. Luwabe hugkaycq xwezjx cair ils iavopaquhajnr szul ec hutacyem teunsizg. Id fji uscolucl utaft ab toupv’n, ospeh dje suykegarc ap Robdopiw nu nyovj leuh uzs:
heroku ps:scale web=1
Seagb jirkejp, mucmarb gte haax hxudlg ma Qejeho zody xuvusyet zeat itx. Avic laim ulq vk sefeyeqc qli erm IKG oq buob uv tko Tigloxdy yuj ir cdu Hisivo dicdmoass oy baak rrowgav. Nae lol utqe oqeg sgu reba uj o wxuhlew by onqaxomt byo zadfakefk uv Hafrojas:
heroku open
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.