5. Deploying to IBM Cloud
This step takes what we’ve built so far and optimizes the app for a production environment. We’ll be deploying the production build to IBM Cloud.
- Fork, clone and branch
- Create IBM Cloud account
- Build for production
- Create manifest file
- Create static file
- Deploy app
- Submit pull request
Preview
A preview of what you’ll build (visually no different, but built for production):
Fork, clone and branch
This tutorial has an accompanying GitHub repository called carbon-tutorial that we’ll use as a starting point for each step. If you haven’t forked and cloned that repository yet, and haven’t added the upstream remote, go ahead and do so by following the step 1 instructions.
Branch
With your repository all set up, let’s check out the branch for this tutorial step’s starting point.
git fetch upstreamgit checkout -b react-step-5 upstream/react-step-5
Build and start app
Install the app’s dependencies (in case you’re starting fresh in your current directory and not continuing from the previous step):
yarn
Then, start the app:
yarn start
You should see something similar to where the previous step left off.
Create IBM Cloud account
Before we get started, create an IBM Cloud account if you don’t already have one, as we’ll be deploying there in a bit.
Build for production
Before we deploy our app, we need to create an optimized production build with
this command. You may need to CTRL-C
to stop the development environment
first.
yarn build
Looking at package.json
, you’ll find yarn build
to run
react-scripts build
. This builds the app for production to the build
folder.
It bundles React in production mode and optimizes the build for the best
performance. It even goes so far to minify files and include hashes in filenames
for caching.
As a lot of this may seem like magic since the build configuration came from Create React App, go ahead and check out their production build guidelines for a full description of what’s happening.
Create manifest file
Now that we have a production build, let’s get it on the cloud. We’re going to
use
staticfile-buildpack
to deploy our webapp. Since this is a Cloud Foundry buildpack, we’ll be using
the cf
command line interface (CLI). If running cf --help
doesn’t work for
you, chances are you need to
install the CLI.
With the Cloud Foundry CLI installed, next, we need to create a manifest.yml
file in the root of the project. To prevent multiple apps trying to use the
carbon-tutorial
name, replace USERNAME
with your GitHub username below to
make sure our apps are uniquely named.
manifest.yml---applications:- name: carbon-tutorial-USERNAMEmemory: 64Mbuildpack: https://github.com/cloudfoundry/staticfile-buildpack.git
Create static file
Create a new static file in the root of the project named Staticfile
. This
tells the app to deploy from the build
folder and not the root of the project.
Staticfileroot: build
Cloud Foundry ignore
After telling Cloud Foundry what to include, we can also specify what to ignore.
Create a top-level .cfignore
file. Cloud Foundry doesn’t let you push
read-only files (specifically, files with permissions < 400
), so to prevent
issues with the deploy, add:
.cfignorenode_modules/.cache
You can speed up deploys by decreasing the files uploaded through cloud foundry. To accomplish this, ignore any folder not required by the production application on IBM Cloud.
In our case we can ignore everything except the build/
directory and
Staticfile
. To speed up our deploys change .cfignore
to:
.cfignore*!Staticfile!build
Deploy app
Login to IBM Cloud with:
cf login -a https://api.us-south.cf.cloud.ibm.com --sso
Deploy app using the cf push
command. Since manifest.yml
is in our root
directory, we don’t need to specify it in the push command. But, if you have
multiple manifest files that target different environments, it’s good practice
to specify the file.
cf push -f manifest.yml
To make it easy on ourselves by not needing to remember that command, let’s add
a script in package.json
. We can combine the build and deploy steps to make
sure we only deploy immediately after running the build. In the "scripts"
object in package.json
, add:
package.json"deploy": "rm -rf ./build && yarn build && cf push -f manifest.yml"
Next time you want to deploy, you can simply run yarn deploy
.
Submit pull request
That does it! We’re going to submit a pull request to verify completion of this tutorial step. In doing so, please include the mybluemix.net URL for your deployed app in your pull request description.
Continuous integration (CI) check
Run the CI check to make sure we’re all set to submit a pull request.
yarn ci-check
Git commit and push
Before we can create a pull request, stage and commit all of your changes:
git add --all && git commit -m "feat(tutorial): complete step 5"
Then, push to your repository:
git push origin react-step-5
Pull request (PR)
Finally, visit
carbon-tutorial to
“Compare & pull request”. In doing so, make sure that you are comparing to
react-step-5
into base: react-step-5
.