Solution for Chapter 11: Deploying APIs

Design and Build Great Web APIs — by Mike Amundsen (122 / 127)

The Pragmatic Programmers
The Pragmatic Programmers
4 min readApr 28, 2021

--

👈 10: APIs | TOC | Appendix 3 API Project Assets Checklist 👉

Most of the work of deploying our API project into production is handled by the Heroku CLI Client we reviewed in the chapter. However, we need to manually deploy the application (in other words, type in the proper command in the command window) in order to complete the deployment process. That means our current level of automation is at continuous delivery. (See Continuous Delivery, for details.)

This exercise challenge is for you to elevate your deployment automation to continuous deployment — to automate that last step of deploying into production. To do that, you were given a hint to modify the norman test runner tool we used in Chapter 9, Testing APIs, to not only automate the testing but also automate the final production deployment. You even got the hint that you only needed to modify a single line of the test runner script to do that.

Another copy of the source code for the norman utility can be found in the code/deploying/exercise/before/norman folder in the code files for this book. If you open the index.js file in that folder, you’ll find the following:

In the current script, when all tests pass successfully, a message appears on the command line (“collection run complete!”) and the script stops. However, as you can see in the source code, you can modify a commented-out line to update the test runner to also deploy the code to production when all tests pass.

That code looks like this:

Note that the only change here is the addition of the command git push heroku master. That’s the same command you type manually to kick off the deployment process on Heroku’s servers. Now we’re just getting the norman utility to do that last step for us!

Once you’ve updated the source code and saved it, you need to run the npm install command again to update your copy of the norman command line utility.

Now you can move to the code/deploying/before/onboarding folder and do the following:

First, use npm to update the local project source code: (Note: Make sure you have adequate access rights to update the local file tree.)

Next, you need to make sure the version of your onboarding-api project is set up to deploy to Heroku. If you’re using the project in the code1 folder, you’ll need to create a new Heroku app. This was covered in detail in Create a New Heroku App.

After you have successfully created a new Heroku app and associated that with your copy of your local git repo, you’re ready to run the test and, if all goes well, automatically deploy it to the cloud.

Next, you can start the local version of your onboarding service:

Once that service is up and running, you can execute the norman test runner to validate your build against your test suite:

Assuming both your norman utility code and your API project code are clean and all tests pass, you’ll see the updated test runner automatically kick off the deployment to Heroku’s servers. If all goes well, you should be able to view your newly deployed app live in the cloud by following the URL supplied by Heroku.

And that’s it. Congratulations! You have moved your API project from the continuous delivery stage to the continuous deployment stage.

Footnotes

[98] https://app.apiary.io

Copyright © 2020, The Pragmatic Bookshelf.

👈 10: APIs | TOC | Appendix 3 API Project Assets Checklist 👉

Design and Build Great Web APIs by Mike Amundsen can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

Unlisted

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.