Asset Pipeline with Heroku Continued

This is a follow up post to yesterday’s article on how I work with asset pipeline for a Heroku hosted app.

While the setup described from the previous article works. It leaves a few sticking points.

  1. Before I do assets precompile, I have to either export the necessary environment variables manually, or set them up in my .zshrc globally.
  2. It’s hard to precompile assets for different deployment targets, e.g. staging and production.
  3. I have to remember to run the assets clean rake task to keep my local dev env sane.

So … I did some follow up work. The README I wrote for the specific project is as follow.

Asset Compiling

Assets are precompiled locally and automatically uploaded to S3 with the help from the asset_sync gem.

After step into the project root directory, simply calling a custom ap() function, such as

⚡ ap staging

to compile the assets and upload the compiled assets to S3.

For this to work, RVM on local development machine is required. The handy function ap() is defined in .rvmrc under the project root.

The argument passed into the ap() function should be a valid Heroku Git remote name.

For example, we have staging setup on Heroku because of the following git remote output.

⚡ git remote -v
origin  git@bitbucket.org:xxxxx/xxxxx.git (fetch)
origin  git@bitbucket.org:xxxxx/xxxxx.git (push)
staging git@heroku.com:xxxxx.git (fetch)
staging git@heroku.com:xxxxx.git (push)

This means we will need to create a .projectrc.staging file under the project root. A projectrc template is provided, .projectrc.sample. 3 config variables need to be filled in.

Note that FOG_DIRECTORY is the Amazon S3 bucket name. To mute fog’s noisy output during assets precompile, name the S3 bucket using only alphabets and . is recommended.

The same 3 variables are also set on Heroku and can be found by doing

⚡ heroku config

The ap() function does the following

  1. precompile assets locally
  2. upload the compiled assets under /public/assets to the corresponding S3 buckets
  3. purge local compiled assets from /public/assets, with the exception of /public/assets/manifest.xml, which is pushed to Heroku.

Now to finish it up, here’s my .rvmrc

rvm ruby-1.9.2-p290@my_project --create

ap() {
  local projectrc=".projectrc.$1"

  if [ -f "$projectrc" ]; then
    source "$projectrc"

    bundle exec rake assets:precompile
    git add public/assets/manifest.yml -f
    git commit -m 'updated asset manifest'
    bundle exec rake assets:clean && git checkout public/assets/manifest.yml
  else
    echo "Missing '$projectrc'. Check README for details."
  fi
}

And here’s the .projectrc.sample

export FOG_DIRECTORY=XXXXXXXXXXXXX
export AWS_ACCESS_KEY_ID=XXXXXXXXXXXXX
export AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXX

Hope this makes sense and helps.

Published: 2012-07-12
blog comments powered by Disqus