From zero to Heroku with Vapor
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:
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/vaporThen bootstrap a new project (e.g. a new website, using the available templates):
vapor new --template=web MyAmazingProject
cd MyAmazingProject
vapor xcodeTest the new website by running the generated Xcode project and navigating to http://localhost:8080:
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.gitCommit and push all your changes:
git add . && git commit -m "MyAwesomeProject bootstrap"
git push origin masterSet 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.ymlNow 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:
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/herokuLog 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/vaporSet the swift version you’re currently using (e.g., using the current stable Xcode 11.4 version):
echo “5.2” > .swift-versionThen 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" > ProcfileAt the time of this writing, you might need also to run:
swift package updateto 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=1Your new app is now finally available at https://my-amazing-project.herokuapp.com:
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 domainsto 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








