Deploying Kitura Swift App to IBM Bluemix
When looking for options to deploy your Kitura Swift app to Bluemix, you may run across IBM Cloud Application Tools. IBM Cloud Application Tools provides a Mac app that is supposed to help you deploy your app to the Bluemix server. Unfortunately, I did not have a positive experience with IBM Cloud Tools as it kept freezing and was unable to upload my app to the server.
After getting dissapointed with IBM Cloud Application Tools I turned to command line tools. IBM has provided a starter project on Github where they discuss the process of deployment using terminal. Unfortunately, they missed a lot of steps and it is not clear how to do a successful deployment. Hopefully, this post will list all the required steps to perform a deployment to IBM Bluemix.
Let’s start with a simple Kitura project called “Hello-Pokemon”. The Package.swift file has a single dependency on the Kitura repository as shown below:
In the main.Swift project create a single route which returns the name of the Pokemon as shown below:
If you visit the URL http://localhost:8090/pokemon you should see “Pickachu” displayed as text being returned. That’s pretty much it for our implementation. Let’s deploy it to Bluemix.
The first step is to create an account on Bluemix. You can visit the Sign Up for IBM Bluemix page to create a free account. Please note that Bluemix is free for upto 30 days. After creating the amount “Login” using your credentials.
Successful login will take you to the Dashboard screen. Click on the “Create App” button to get started.
The next screen is a template selection screen. Scroll down and select “Runtime for Swift” under Cloud Foundry Apps.
Provide the name of the app and then click on the “Create” button as shown below:
After you click the “Create” button it will take you to a “Getting Started” screen, where it will list all the steps you need to follow to deploy the app.
In order to deploy the app using command line, make sure you have Cloud Foundry CLI installed. You can install the Cloud Foundry CLI using brew as shown below:
brew install cloudfoundry/tap/cf-cli
The next step is to create a configuration file for deployment. This configuration file is called “manifest.yml”. Create “manifest.yml” in the root directory for your application and add the following content.
— name: Hello-Pokemon
The value for the command is “Hello-Pokemon” which is the name for your project and it comes from the Package.swift file as shown below:
Next step is to setup the API endpoint. Run the following command from the terminal.
cf api https://api.ng.bluemix.net
You should see a similar result.
Next you need to login to CloudFoundry using your Bluemix credentials that you created earilier.
Finally push your app.
Brew coffee or something because this is going to take a while!
Aaaaand we are back …. with errors :(
Let the fun begin! Run the following command to see the logs.
cf logs Hello-Pokemon --recent
Here is the result, which shows the error log.
Looks like our app never passed the physical exam! :) Let’s fix this. Run the following command.
cf push Hello-Pokemon -u process
You can also add the following attribute in your manifest.yml to always perform the health check.
Start brewing coffee again because this is going to take some time!
If everything went correctly you will see the following message.
Congratulations you just deployed your first Swift Kitura app. Visit Hello-Pokemon.mybluemix.net/pokemon to run your app.
We configured our Kitura app to run on port 8090. This is implemented in the main.swift file. Unfortunately, that is not the default port on which IBM Bluemix runs the app. So, what is the port on which the app run on Bluemix.. well thats not so easy to find out.
Digging into the source code you will find out that the server will revert to 8090 if the environment variables are not set. Let’s jump into our main.swift file and change the server from 8090 to 8080 as shown below.
After making the change build the app and push to the server again.
cf push Hello-Pokemon -u process
Let’s brew another hot coffee!
After the push is completed. Go to the URL http://hello-pokemon.mybluemix.net/pokemon.
Yes!!! Finally!!! It worked!!
On the Kitura Slack channel some users listed few more steps to perform a successful deployment but in my experience the above steps were enough to get the API working.
Awesome work! Go get a coffee you deserve it!
As Alex pointed out, you can allow the code to figure out which port to use.