In this second part of this tutorial series, we’re going to learn how to deploy to the Vapor Cloud. This means we can get deploy the code that we can access from anywhere in the world not just our local machine.
In the first part of this tutorial, we built an API that could return random names from a list and accept data.
You will need the following to complete this tutorial.
Vapor Cloud has a generous free tier and is easy to get started with. As well as scaling up your hosting you can add databases and caching in just a few simple clicks. More info on pricing can be found here.
Visit the signup page and create an account here.
Once you are on the dashboard create a project and assign it to this organization. Name it Vapor-Hello-World. You do this by selecting the projects tab from the navigation menu and pressing the plus button.
Go to the working directory of your Vapor project in terminal and run the following command and enter your Vapor Cloud credentials.
vapor cloud login
Now you are logged in to Vapor Cloud you can run the following command to being the deployment process.
vapor cloud deploy
This will begin the deployment process. If you haven’t created a git repo for this project yet you will be asked if you would like to. If you already have a git repo added you can ignore the next step. However you need to make sure your repo is public.
Enter y and press Enter. This will open GitHub. From here you need to create a new public repository. Copy the HTTPS or SSH URL and paste it into terminal and into terminal and press Enter.
When asked if you would like to create an application. Enter y and press Enter. Assign it to the project we created earlier.
You will now be asked to give the application a name. Previously we created a project, a project can have multiple applications such as:
Name ours Vapor-Hello-World-API-IOS
.
Next you will be asked to name a slug. Slugs are unique identifiers like bundle identifiers in iOS. I will be using push-vapor-hello-world-api-ios
. You will need to use a different one. You will then be asked to confirm if the above information is correct. Enter y and press Enter.
You’ll next be asked if you want to add hosting, approve this by entering y and pressing Enter. Followed by this it will detect your git repo, select the URL and confirm the information in the usual manner.
You’ll next be asked if you want to set up an environment. Confirm that you wish to do this. Applications can have many environments, such as a testing or staging environment as well as a production environment. We’ll name this one production
. You’ll then be asked which git branch you want to build this against, enter master
(assuming this is your production branch). Next you’ll be asked what tier of hosting you would like, select the free tier for this tutorial and confirm the information is correct.
We don’t want to add a database so enter n and press Enter. Finally we select how we want to build the application. There are three different types of builds:
We want to run the clean build so 3 and press Enter. Then confirm the above information is correct. Vapor cloud will now deploy your project from the project git repo and branch you selected.
It will become available at: http://<your-slug>.vapor.cloud
.
You can also visit http://<your-slug>.vapor.cloud/api/name
or post data using postman to this URL.
We’ve learnt how to deploy our application onto Vapor Cloud using the free tier and the Vapor toolbox.