DEV Community

Cover image for Swift on Netlify
Simon Rice
Simon Rice

Posted on

Swift on Netlify

Since it was open sourced in December 2015, there have been a number of efforts in bringing Apple's Swift Programming Language to run on a variety of operating systems, servers and even embedded systems.

In December 2019, prominent Swift & iOS developer John Sundell open sourced Publish, a production-ready static site generator package written in Swift. Its purpose is given a set of markdown and Swift files, you can generate your HTML and other assets by running swift run - this will fetch your Swift Package Manager (or SPM) dependencies, build a command line app and run it, putting your generated website in a directory (which, by default, is Output).

Since I’m primarily an iOS developer by trade who previously tended to opt for Ruby / Middleman to publish content on the web, and given I’ve used Netlify for hosting said websites, I had a lightbulb moment 💡 What if I could get Swift working with full SPM caching on Netlify, and so allow a seamless build pipeline for Publish? After all, Netlify’s build image is open source & is welcoming contributions.

Well, I'm happy to say not only is it possible, but as of build image v3.3.8, Swift is now supported on Netlify as a first party language.

How to create a static website written in Swift that’s hosted on Netlify

  1. Build your website using Publish locally, and ensure you’ve checked in your Package.swift file.
  2. After pushing your website to a remote repository, set it up on Netlify.
  3. When prompted on Netlify for your Basic Build Settings (or in Your Site’s Settings -> Build & Deploy -> Continuous Deployment -> Build Settings if you're migrating an existing website), use these settings to deploy your website:
Build Command: swift run
Publish Directory: Output
Enter fullscreen mode Exit fullscreen mode

...and then deploy 🚀 that’s all there is to it 🎉

Note that your first deploy will be a bit slower as there’s no SPM cache to start off with, but this will be used for subsequent builds.

Optional, but recommended: Include a .swift-version file in the root of your repository stating which version of Swift you’re targeting. By default, Netlify runs on Swift 5.2, which at the time of writing is the current, readily-available stable version - even if you are using Swift 5.2 yourself, including this file is a good habit. However, Netlify supports any version of Swift from 5.0 and above that swiftenv supports. If you’re not using Netlify’s default version of Swift then, in addition to your SPM dependencies, your Swift version will also be cached at the end of your first build, so your build time isn’t sucked up downloading your selected version of Swift. You can equally configure a SWIFT_VERSION environment variable on Netlify, but if you have swiftenv installed locally then a .swift-version file will work for you locally too.


While Publish is currently the most popular static site generator that’s written in Swift, in practice Netlify can run any SPM-based Swift command line app that generates content of some form, so long as it’s not dependant on any proprietary framework from Apple (such as UIKit or AppKit). You just need to ensure your publish directory is pointing to the right place.

I’ve enjoyed playing a small role in bringing the Swift Programming Language to another new environment, via a small bit of Docker & shell scripting that I might document on here if anyone’s interested. And if Swift is your thing, I hope you find this little guide useful.

Top comments (2)

Collapse
 
thisdotmedia_staff profile image
This Dot Media

Yes! This is awesome 🙌

Collapse
 
pamnovalli profile image
Pamela Ianovalli

Thanks. Your article helped me a lot.