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
- Optimize Sass
- 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-angular 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 angular-step-5 upstream/angular-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):
npm install
Then, start the app:
npm 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.
Optimize Sass
So far we’ve been developing in a, well, development environment where static asset optimization hasn’t been a priority. If you reference
/src/styles.scss
@import
@import "~carbon-components/scss/globals/scss/styles";
To give you an idea of what’s all included, open up
node_modules/carbon-components/scss/globals/scss/styles.scss
$feature-flags
carbon-overrides.scss
styles.scss
// Feature flags$css--font-face: true;$css--plex: true;// Global styles@import "~carbon-components/scss/globals/scss/css--font-face";@import "~carbon-components/scss/globals/grid/grid";// Carbon components
Looking at the new
styles.scss
_vars.scss
_colors.scss
_theme.scss
node_modules/carbon-components/scss/components/button/_button.scss
styles.scss
You can read more about optimizing Carbon’s Sass in the Carbon Design System publication on Medium.
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
npm run build
Looking at
package.json
ng build
dist
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
cf --help
With the Cloud Foundry CLI installed, next, we need to create a
manifest.yml
carbon-tutorial-angular
USERNAME
---applications:- name: carbon-tutorial-angular-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
dist
root: dist
Cloud Foundry ignore
After telling Cloud Foundry what to include, we can also specify what to ignore. Create a top-level
.cfignore
400
node_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. For example, in the case of serving static files, you can ignore
node_modules/
src/
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
manifest.yml
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
"scripts"
package.json
"deploy": "rm -rf ./dist && ng build --prod --aot && cf push -f manifest.yml"
Next time you want to deploy, you can simply run
npm run-script 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.
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 angular-step-5
Pull request (PR)
Finally, visit carbon-tutorial-angular to “Compare & pull request”. In doing so, make sure that you are comparing to
angular-step-5
base: angular-step-5