Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Download and compile external assets during deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrei committed Aug 23, 2016
1 parent 89d7fcf commit 0859e01
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 4 deletions.
12 changes: 11 additions & 1 deletion Dockerfile
@@ -1,4 +1,8 @@
FROM ruby:2.3.0
ARG CUSTOM_ASSETS_URL
ARG CUSTOM_ASSETS_BRANCH
ARG CUSTOM_ASSETS_CREDENTIALS

# Install system dependencies
RUN apt-get update -qq; apt-get install -y nodejs npm imagemagick netcat && \
update-alternatives --install /usr/bin/node nodejs /usr/bin/nodejs 100
Expand All @@ -13,6 +17,12 @@ RUN bundle install --jobs 4 && npm install && npm install -g phantomjs-prebuilt

EXPOSE 3000
ADD . /myapp
RUN RAILS_ENV=production bundle exec rake assets:precompile
RUN if [ -z "$CUSTOM_ASSETS_URL" ]; then\
echo "Skipping external assets download";\
else\
echo "Downloading external assets";\
CUSTOM_ASSETS_PATHS=$(_URL=$CUSTOM_ASSETS_URL _CREDENTIALS=$CUSTOM_ASSETS_CREDENTIALS _BRANCH=$CUSTOM_ASSETS_BRANCH bundle exec rake custom_assets:download);\
fi;\
RAILS_ENV=production bundle exec rake assets:precompile

CMD bundle exec puma -b tcp://0.0.0.0 -p 3000 -t 5:16
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -23,6 +23,7 @@ gem 'font-awesome-sass'
gem 'money'
gem 'google_currency'
gem 'rack-cors', require: 'rack/cors'
gem 'httparty'

# Sprockets 3 breaks Teaspoon.
# see https://github.com/modeset/teaspoon/issues/443
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Expand Up @@ -528,6 +528,7 @@ DEPENDENCIES
google_currency
guard-rspec
hiredis
httparty
i18n-js (>= 3.0.0.rc12)
jbuilder (~> 2.0)
jquery-rails
Expand Down
7 changes: 5 additions & 2 deletions circle.yml
@@ -1,4 +1,7 @@
machine:
pre:
# Update docker version
- curl -sSL https://s3.amazonaws.com/circle-downloads/install-circleci-docker.sh | bash -s -- 1.10.0
services:
- docker

Expand All @@ -10,7 +13,7 @@ dependencies:
# install AWS CLI
- sudo pip install awscli
# build the image
- docker build -t soutech/champaign_web:$CIRCLE_SHA1 .
- docker build -t soutech/champaign_web:$CIRCLE_SHA1 --build-arg CUSTOM_ASSETS_URL=$CUSTOM_ASSETS_URL --build-arg CUSTOM_ASSETS_CREDENTIALS=$CUSTOM_ASSETS_CREDENTIALS --build-arg CUSTOM_ASSETS_BRANCH=$CIRCLE_BRANCH .

test:
override:
Expand All @@ -32,7 +35,7 @@ deployment:
- docker push soutech/champaign_web
- ./deploy.sh $CIRCLE_SHA1 'champaign' 'env-production' 'champaign-assets-production' 'logs3.papertrailapp.com:44107' 'actions.sumofus.org'
staging:
branch: development
branch: external-assets
commands:
- docker login -e $DOCKER_EMAIL -u $DOCKER_USER -p $DOCKER_PASS
- docker push soutech/champaign_web
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Expand Up @@ -38,7 +38,7 @@ class Application < Rails::Application
config.i18n.enforce_available_locales = true

config.active_record.observers = :liquid_partial_observer
config.assets.paths += (Settings.custom_assets_paths || "").split(":") if Settings.custom_assets_paths.present?
config.assets.paths += Settings.custom_assets_paths.split(":") if Settings.custom_assets_paths.present?

# We're using Redis as our cache. Configure that here.
# we use 'redis' as the host name because that's configured by docker
Expand Down
Empty file removed lib/assets/.keep
Empty file.
57 changes: 57 additions & 0 deletions lib/tasks/deploy.rake
@@ -0,0 +1,57 @@
namespace :custom_assets do
# Downloads a tar from a URL, extracts its contents into ./tmp,
# and returns a colon separated list of paths belonging to the first level directories
# inside the original tar file.
#
# Params:
# * url: any http[s] url. Optionally it can include a tag <branch> that will be replaced with
# the contents of the branch param
# * credentials: http basic auth credentials. The format should be user:password
# * branch: Mainly for Github URLs usage. The content of this param will replace the <branch> tag
# in the url param
desc "Download external assets"
task :download, [:url, :credentials, :branch] do |t, args|
Bundler.require :default, :development

url = args[:url]
credentials = args[:credentials]
branch = args[:branch]
tar_file_path = "./tmp/assets.tar"

raise ArgumentError.new("usage: rake custom_assets:download[<url>,<credentials>,<branch>]") if url.blank?

if branch.present?
url = url.gsub("<branch>", branch)
end

# Download tar file --------------------------
http_options = {}
if credentials.present?
u, p = credentials.split(":")
http_options[:basic_auth] = { username: u, password: p }
end
response = HTTParty.get url, http_options

if !response.success?
raise "HTTP while trying to download assets #{response.inspect}"
end


# Extract tar file --------------------------
File.open(tar_file_path, "w+b") do |file|
file.write response.body
end

untar_cmd = "tar -xf #{tar_file_path} -C ./tmp"
result = system(untar_cmd)
raise "Error running `#{untar_cmd}`" unless result

# Print dir where assets were extracted -----
base_assets_dir = `tar -tf #{tar_file_path} | head -n 1`
base_assets_dir.strip!
base_assets_path = Pathname.new("./tmp").join(base_assets_dir)

assets_dirs = Dir.glob(base_assets_path.join("*")).select { |f| File.directory? f }
puts assets_dirs.join(":")
end
end

0 comments on commit 0859e01

Please sign in to comment.