Sitemap
Mattia Campolese

Mattia Campolese

Engineering Manager, programming aficionado since the early '00

From zero to Heroku with Vapor

4 min readApr 13, 2020

--

Create, test and deploy your dynamic website with Swift in a few steps.

This is a beginner guide designed for Swift developers who want to create a scalable dynamic website with Continuous Integration of code and in Continuous Delivery.

At the end of this tutorial, you will be able to deploy your “hello world” website in production in just a few minutes:

Press enter or click to view image in full size
MyAwesomeProject deployed on Heroku

The tools that have been chosen are:

  • Vapor 3.x, one of the most elegant web frameworks for Swift
  • Git as the version control system and GitHub as the remote storing platform
  • CircleCI to set up the Continuous Integration pipeline
  • Heroku as the deployment platform (since vapor.cloud has been officially demised)

Note: Heroku provides an all-in-one solution for git repo hosting and CI, but this guide intends to use different tools to not depend on a single provider. Besides, GitHub and CircleCI provide more advanced features and are widely used in the industry.

Bootstrap a new Vapor project

Reference guide:

Install Vapor through Homebrew:

brew tap vapor/tap
brew install vapor/tap/vapor

Then bootstrap a new project (e.g. a new website, using the available templates):

vapor new --template=web MyAmazingProject
cd MyAmazingProject
vapor xcode

Test the new website by running the generated Xcode project and navigating to http://localhost:8080:

Hello world rendition of the Vapor website template
http://localhost:8080. Time for your first cup of ☕️

Push changes on GitHub

Create a new empty GitHub repository to host your code, then set it as your default remote for the Vapor project:

git remote add origin git@github.com:mygithubaccount/my-awesome-project.git

Commit and push all your changes:

git add . && git commit -m "MyAwesomeProject bootstrap"
git push origin master

Set up the CI pipeline with CircleCI

Sign up with your GitHub account on the CircleCI website:

Then set up a new project by selecting the newly created repo. Ignore the templates offered and click on Start building, then choose to add the configuration manually.

Get Mattia Campolese’s stories in your inbox

Join Medium for free to get updates from this writer.

From the root of your repo, move the generated CircleCI configuration file:

mkdir .circleci && mv config.yml .circleci/config.yml

Now you can commit and push the changes. Go back to the CircleCI dashboard and then click on Start Building. After a while, you should get a successful build, and you can also verify the integration of CircleCI on Github:

Press enter or click to view image in full size
Successfully built commit on the MyAmazingProject master branch.

Deploy the website with Heroku

Everything it’s ready to deploy your website to the public.

Create a Free Heroku account:

Then Sign in, Create a new app and choose a region. Then from the app dashboard go to Deploy, Deployment method, select GitHub and connect your account and repository.

Choose automatic deploys from the master branch and enable the Wait for CI to pass before deploy option.

Install now the Heroku CLI:

brew install heroku/brew/heroku

Log in with your email address and install the Vapor buildpack (scripts that are run when the app is deployed):

heroku login
heroku buildpacks:set vapor/vapor

Set the swift version you’re currently using (e.g., using the current stable Xcode 11.4 version):

echo “5.2” > .swift-version

Then create the Procfile (a file that specifies the commands that are executed by the app on startup):

echo "web: Run serve --env production" \
"--hostname 0.0.0.0 --port \$PORT" > Procfile

At the time of this writing, you might need also to run:

swift package update

to update all the dependencies and avoid potential issues with the buildpack scripts.

Commit all changes and push them to master. A build will start and you can see the deployment progress in the Activity tab of the app dashboard.

Once the build is successful, you can finally scale the website by running

heroku ps:scale web=1

Your new app is now finally available at https://my-amazing-project.herokuapp.com:

Press enter or click to view image in full size
MyAwesomeProject deployed on Heroku
Your website has now finally being deployed. Time to celebrate 🍺

Point to your custom domain

If you’ve purchased a domain for your website, it’s now time to point it to your Heroku application:

After verifying your account you have to execute:

heroku domains:add my-amazing-project.com
heroku domains

to get the DNS Target to use in your hostname provider.

Conclusion

Setting up a production CI/CD pipeline it’s a fundamental step for accelerated delivery. You can now focus on writing production code that can be automatically tested and deployed in a few minutes by embracing GitOps:

GitOps: versioned CI/CD on top of declarative infrastructure. Stop scripting and start shipping. — Kelsey Hightower

You can find me on Twitter & LinkedIn.

--

--

Mattia Campolese
Mattia Campolese

Written by Mattia Campolese

Engineering Manager, programming aficionado since the early '00