Building web applications with Swift using Vapor is super simple. Thankfully, deploying them is also a breeze. In just a few steps we’ll go from localhost to Heroku with Vapor’s CLI.
We need a few things before we get started:
- A working Vapor web application. If you don’t have one, you can follow this tutorial to get started.
- A free Heroku account
- Heroku Command Line Interface – Make sure to run
heroku loginonce the CLI is installed to authenticate your account.
Deploying to Heroku
Vapor supports deployment to any server that can run Swift. The Vapor CLI tool makes deployment to Heroku super simple.
If your Vapor app is not already in a Git repository we’ll need to add one. Run the following commands from your project directory:
git add .
git commit -m "Initial commit"
Now that we have a Git repo we can use Vapor’s CLI to deploy our app to Heroku. Run this command to start the process:
vapor heroku init
Here’s how to answer the questions the CLI asks you:
- Choose whether you want to give your app a custom name on Heroku. This one’s up to you.
nto the custom executable name if you have not modified Vapor’s defaults or
yif you have. Answer
nif you followed the previously mentioned tutorial.
yto deploy now.
Grab some coffee because this will take 5-10 minutes. The reason it takes so long is Swift will be installed on your Heroku instance using swiftenv before building your app.
Once the process is done launch your browser to the Heroku URL provided by the output you should see your app running in the cloud. Here’s some sample output from the tutorial app mentioned above:
If you need to push new changes up to Heroku just commit those changes using
git and then use Vapor CLI to push the new code:
git add -A
git commit -m "Shiny new features!"
vapor heroku push
That’s all there is to deploying Vapor apps to Heroku!
Now that you’ve written some Vapor apps and deployed them to the cloud, what are you going to do next? Here’s some things you might consider:
- Check out the newly formed Swift Server APIs project
- Learn how to authenticate users in Vapor apps
- Try another Swift web framework such as IBM’s Kitura