Deploying Swift application to Heroku with ease.

Jan Vojáček
4 min readDec 17, 2018

--

Swift is loved and versatile language to write software with, and it is becoming increasingly popular for writing back-end applications as well, mainly thanks to some popular frameworks like Perfect, Vapor or IBM’s Kitura.

Heroku is one of the most popular and easy-to-use PaaS (Platform-as-a-service) cloud service, and thus in this short tutorial, we will take a look on how to deploy your Swift application to Heroku easily using build-pack i have written for that purpose!

In this tutorial, we will use a basic Kitura project, because i use Kitura regularly and it is my favourite back-end framework for Swift, but you may use whichever you like.

Prerequisities.

We will need to have an already working Swift back-end application. We won't focus on how to build an app using any of the previously mentioned frameworks. Our focus will rather remain on deploying process itself to keep this tutorial on topic.

You will also need to install Heroku's CLI tools, as described here, it you don't already.

It is also necessary to use Swift Package Manager in order to deploy and use our Swift app in Heroku, thus we need to include Package.swift file in the project.

Getting started

First, let’s take a look on how the project structure and Package.swift file content might look like:

Folder structure.
Package.swift content.

Port resolution

Heroku needs our app to run on specific port to ensure it can direct incoming web traffic correctly to our app. It is available as an environment variable. We need to handle that somehow, so below is an exemplary function of how to achieve that in Swift:

Port resolution.

Execution endpoint

In order to deploy our app, we must create a Procfile, which is basically a way, in which we can specify process types and commands how to start them for Heroku. We will only need “web type” process, thus the file should have only following content: web: ./ExampleApp.

Notice that the endpoint name must match target name from our Package.swift file, because when our app is build, a binary file named after the target specified there shall be created.

Let's take a look at the Procfile creation process below in more detail:

Procfile creation process.

Specifying Swift version

We can specify an exact Swift release we would like to be installed and used to build our app. If we don't, default version will be used, which is 4.2.1 as of the day of writing this tutorial.

We can specify it by creating .swift-version file in the root directory of our project with content of 4.2 to install Swift version 4.2 for instance.

Note that only Swift 4.2 and newer releases are possible to install, because Swift is released for Ubuntu 18.04 (which Heroku uses in its dynos by default) just since version 4.2.

Deployment process

Every app, which is to be deployed to Heroku must use git as it's version-control system, thus if you app doesn't use git yet, you should init it first before proceeding any further.

To create a new Heroku app, we can easily do that by using Heroku CLI tools, which we were discussing before in prerequisities section. Type in following command in the root directory of your app: heroku create

We then need to set our Heroku app to use custom build-pack for Swift which i have written. You can easily do so by typing in: heroku buildpacks:set https://github.com/thevojacek/HerokuSwiftBuildpack.git

Then, we only need to push our app to Heroku using git! Let's do it by typing: git push heroku master

This will automatically start deployment and build of our Swift app, build-pack will take care of all the necessary steps to successfully build and run the app.

You can see all the steps below in order:

Deployment process.

This should be everything and the app should be up and running. Enjoy using your Swift app in Heroku!

--

--