Creating and Distributing Swift Packages

How to start writing and sharing your Swift code using Swift Package Manager, Github, and Travis-CI

Andrew Scott
Nov 5, 2018 · 6 min read
Image for post
Image for post

Swift Package Manager

Getting Started

For this tutorial I’ll be creating a very simple library that adds back a bit of functionality that was removed in Swift 3, support for ++ and -- operators (Take that, Chris Lattner 😉).

Create a new directory

The very first step is to create a new directory to save our project in. Pick your name carefully as the name of the directory will be used as the default name of the package. I’ll go with Incrementer .

Create the Template

The next thing we’ll do is create a package template to get started with. Conveniently, Apple has done all the heavy lifting here.

Run the Tests

A good way to check that the template has been created successfully is to try running the unit tests. You can do this by running the following command.

Create an XCode Project (optional)

You may have noticed if you already tried to open this project in XCode that an error is returned preventing you from opening the project as your normally would. That’s because there’s no project file yet. You can run the following command to create one.

The Code

At this point you should have a file named Incrementer.swift within the Sources directory in your base directory. Open that file and add the following code.

Unit Tests

While you may not feel that unit tests are necessary for your personal projects, when sharing an open-source package with the world it’s probably a good idea to write some tests to ensure the code you’re sharing will actually do what it’s supposed to. You can add the following code to IncrementerTests.swift within the/Tests/IncrementerTests directory.

Publishing to Github

Now that we’ve added the code to implement our new operators and we have unit tests to make sure they’re working, it’s time to publish the package the Github.

  1. Initialize a new git repo for your Package.
  2. Add your Github repo as a new remote repo.
  3. Push your changes to Github.

Setting up CI/CD with Travis-CI

  1. Find your package repo in the Travis-CI UI and enable builds for the repo.
  2. Create an empty .travis.yml file and add it to your working directory.
  3. Populate the your .travis.yml file with the following
Image for post
Image for post
Image for post
Image for post
A new Release in Github and a passing build in Travis-CI

Using your new Package

Once published, using your new package in other projects couldn’t be easier. Once you’ve created a new Swift project with its own Package.swift file you can just add your package’s Github repo under dependencies.


With Swift’s powerful new inclusion of the Swift Package Manager in Swift 4 and the ability to leverage Github and Travis-CI as part of your deployment pipeline, publishing an open-source Swift package has never been easier!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store