Skip to content

tedconf/front_end_builds

Repository files navigation

Deprecation Notice

TED has shifted to React and will no longer maintain this application/library. If you wish to continue using this application/library, please create a pull request and repo ownership can be transferred. This repository will be archived at the end of 2022.

You can read documentation on how we curently use FrontEndBuilds in Confluence.

SSH Key Warning

Only RSA keys are supported for authentication. This means you cannot and should not use your regular TED SSH public key for front end builds deployments. Make sure you're checking your FEB_DEPLOY_KEY value in the environment and that it is pointing to an RSA key. You can't generate the keys using ssh-add you'll need the use something like this.

FrontEndBuilds

Front-End Builds (FEB) lets you easily serve remotely-hosted static (JS) applications from your Rails apps. For example, you can host a Rails backend on Heroku, an Ember.js frontend on S3, and use FEB to connect the two.

Benefits:

Features:

  • Admin interface lets you easily view, rollback and activate different app versions

The motivation for this gem came from Luke Melia's RailsConf2014 talk.

Installation

Add this line to your application's Gemfile:

gem 'front_end_builds'

And then execute:

$ bundle

Front-End Builds brings some migrations along with it. To run, execute

rake front_end_builds:install:migrations
rake db:migrate

Usage

First, mount the admin interface in routes.rb:

Rails.application.routes.draw do

  mount FrontEndBuilds::Engine, at: '/frontends'

end

You should mount this under an authenticated route using your application's auth strategy, as anyone with access to the admin will be able to affect the production builds of your front end apps.a

If you don't want to set up an HTML auth strategy, you can do something like this:

# routes.rb
protected_app = Rack::Auth::Basic.new(FrontEndBuilds::Engine) do |username, password|
  username == 'admin' && password == (Rails.env.production? ? ENV['FEB_ADMIN_PASSWORD'] : '')
end
mount protected_app, at: '/frontends'

This will use basic HTTP auth to secure access to your admin ui. Just set the ENV variable in production, and use it to gain access. If you're deploying to Heroku, use Config Vars.

Now, to create a new app, first add a front_end route pointing to your app in routes.rb:

Rails.application.routes.draw do

  front_end 'app-name', '/app-route'

end

Visit the admin (at whatever URL you mounted the engine above), create a new app named app-name, and you'll receive instructions on how to start pushing builds.

Note: If you're using this engine to serve an ember app at the Root, be sure to put all other Rails routes above the front_end route - as this takes priority over all routes below it!

Rails.application.routes.draw do
  # All other Rails routes here

  front_end 'app-name', '/'
end

At this point you should be able to test the setup in dev by running

bin/rails server

Visit /frontends to access the Admin interface, and visit the front_end route, which will initially return 404 Not found since you haven't configured and deployed any front-end builds yet.

Example Next Steps with Heroku and Ember.js

A common configuration is to deploy your FEB-enabled Rails app to Heroku, and deploy your Ember.js frontend to S3:

  1. Deploy your Rails app to Heroku
  2. Configure your frontend app with ember-cli-deploy-front-end-builds-pack
  3. Access your Rails app's FEB Admin interface, add an app, and configure a public SSH key that corresponds to the private key you plan on using to sign your Ember.js builds
  4. Deploy your frontend app. If all goes well, it should build the Ember app, push the static assets to S3, then POST to your Rails app. You'll see the build in the Admin interface, and should be able to access your frontend at the front_end route you specified.

Development

Admin

The Admin interface is an Ember CLI app within feb. A distribution is kept within the gem, and must be updated whenever admin code is updated.

After changing the admin app, run

rake admin:build

to store a fresh distribution.

Running tests

# Rails tests
rspec

# Admin tests, from /admin dir
ember test
  • Auto live setting
  • make posts idempotent (i think they are), but dont insert a new row if it already exists.