Skip to content

Commit

Permalink
add some deployment stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
estiens committed Oct 7, 2022
1 parent beafc0d commit 950dd29
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 152 deletions.
16 changes: 16 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.git
tmp
!tmp/pids
log
public/assets
public/packs
.bundle

db/*.sqlite3
db/*.sqlite3-*

storage
config/master.key
config/credentials/*.key

node_modules
135 changes: 135 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# syntax = docker/dockerfile:experimental

# Dockerfile used to build a deployable image for a Rails application.
# Adjust as required.
#
# Common adjustments you may need to make over time:
# * Modify version numbers for Ruby, Bundler, and other products.
# * Add library packages needed at build time for your gems, node modules.
# * Add deployment packages needed by your application
# * Add (often fake) secrets needed to compile your assets

#######################################################################

# Learn more about the chosen Ruby stack, Fullstaq Ruby, here:
# https://github.com/evilmartians/fullstaq-ruby-docker.
#
# We recommend using the highest patch level for better security and
# performance.

ARG RUBY_VERSION=3.1.1
ARG VARIANT=jemalloc-slim
FROM quay.io/evl.ms/fullstaq-ruby:${RUBY_VERSION}-${VARIANT} as base

LABEL fly_launch_runtime="rails"

ARG NODE_VERSION=18.9.0
ARG BUNDLER_VERSION=2.3.7

ARG RAILS_ENV=production
ENV RAILS_ENV=${RAILS_ENV}

ENV RAILS_SERVE_STATIC_FILES true
ENV RAILS_LOG_TO_STDOUT true

ARG BUNDLE_WITHOUT=development:test
ARG BUNDLE_PATH=vendor/bundle
ENV BUNDLE_PATH ${BUNDLE_PATH}
ENV BUNDLE_WITHOUT ${BUNDLE_WITHOUT}

RUN mkdir /app
WORKDIR /app
RUN mkdir -p tmp/pids

RUN curl https://get.volta.sh | bash
ENV VOLTA_HOME /root/.volta
ENV PATH $VOLTA_HOME/bin:/usr/local/bin:$PATH
RUN volta install node@${NODE_VERSION} yarn

#######################################################################

# install packages only needed at build time

FROM base as build_deps

ARG BUILD_PACKAGES="git build-essential libpq-dev wget vim curl gzip xz-utils libsqlite3-dev"
ENV BUILD_PACKAGES ${BUILD_PACKAGES}

RUN --mount=type=cache,id=dev-apt-cache,sharing=locked,target=/var/cache/apt \
--mount=type=cache,id=dev-apt-lib,sharing=locked,target=/var/lib/apt \
apt-get update -qq && \
apt-get install --no-install-recommends -y ${BUILD_PACKAGES} \
&& rm -rf /var/lib/apt/lists /var/cache/apt/archives

#######################################################################

# install gems

FROM build_deps as gems

RUN gem update --system --no-document && \
gem install -N bundler -v ${BUNDLER_VERSION}

COPY Gemfile* ./
RUN bundle install && rm -rf vendor/bundle/ruby/*/cache

#######################################################################

# install node modules

FROM build_deps as node_modules

COPY package*json ./
COPY yarn.* ./
RUN yarn install

#######################################################################

# install deployment packages

FROM base

ARG DEPLOY_PACKAGES="postgresql-client file vim curl gzip libsqlite3-0"
ENV DEPLOY_PACKAGES=${DEPLOY_PACKAGES}

RUN --mount=type=cache,id=prod-apt-cache,sharing=locked,target=/var/cache/apt \
--mount=type=cache,id=prod-apt-lib,sharing=locked,target=/var/lib/apt \
apt-get update -qq && \
apt-get install --no-install-recommends -y \
${DEPLOY_PACKAGES} \
&& rm -rf /var/lib/apt/lists /var/cache/apt/archives

# copy installed gems
COPY --from=gems /app /app
COPY --from=gems /usr/lib/fullstaq-ruby/versions /usr/lib/fullstaq-ruby/versions
COPY --from=gems /usr/local/bundle /usr/local/bundle

# copy installed node modules
COPY --from=node_modules /app/node_modules /app/node_modules

#######################################################################

# Deploy your application
COPY . .

# Adjust binstubs to run on Linux and set current working directory
RUN chmod +x /app/bin/* && \
sed -i 's/ruby.exe/ruby/' /app/bin/* && \
sed -i '/^#!/aDir.chdir File.expand_path("..", __dir__)' /app/bin/*

# The following enable assets to precompile on the build server. Adjust
# as necessary. If no combination works for you, see:
# https://fly.io/docs/rails/getting-started/existing/#access-to-environment-variables-at-build-time
ENV SECRET_KEY_BASE 1
# ENV AWS_ACCESS_KEY_ID=1
# ENV AWS_SECRET_ACCESS_KEY=1

# Run build task defined in lib/tasks/fly.rake
ARG BUILD_COMMAND="bin/rails fly:build"
RUN ${BUILD_COMMAND}

# Default server start instructions. Generally Overridden by fly.toml.
ENV PORT 8080
ARG SERVER_COMMAND="bin/rails fly:server"
ENV SERVER_COMMAND ${SERVER_COMMAND}
CMD ${SERVER_COMMAND}
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ gem 'rack-attack'
gem 'rack-cors', require: 'rack/cors'
gem 'redis-rails'

gem 'cssbundling-rails'

group :production do
gem 'scout_apm'
end
Expand Down
3 changes: 0 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ GEM
coderay (1.1.3)
concurrent-ruby (1.1.10)
crass (1.0.6)
cssbundling-rails (1.1.1)
railties (>= 6.0.0)
dead_end (4.0.0)
debug_inspector (1.1.0)
derailed_benchmarks (2.1.2)
Expand Down Expand Up @@ -300,7 +298,6 @@ DEPENDENCIES
binding_of_caller
chronic
clockwork
cssbundling-rails
derailed_benchmarks
foundation-rails
haml-rails
Expand Down
1 change: 0 additions & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
web: bundle exec puma -C config/puma.rb
clock: bundle exec clockwork lib/new_scraper_clock.rb
1 change: 1 addition & 0 deletions Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
web: bin/rails server -p 3000
css: yarn build:css --watch
clock: bundle exec clockwork lib/new_scraper_clock.rb
3 changes: 0 additions & 3 deletions app.json

This file was deleted.

7 changes: 6 additions & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,10 @@ test:
host: localhost

production:
url: <%= ENV['DATABASE_URL'] %>
adapter: postgresql
encoding: unicode
database: <%= ENV['DATABASE_NAME'] %>
user: <%= ENV['DATABASE_USER'] || 'postgres' %>
host: <%= ENV['DATABASE_HOST'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
pool: <%= ENV['DB_POOL'] || ENV['RAILS_MAX_THREADS'] || 5 %>
51 changes: 51 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# fly.toml file generated for world-cup-json-2022 on 2022-10-07T00:53:06-05:00

app = "world-cup-json-2022"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[build]
[build.args]
BUILD_COMMAND = "bin/rails fly:build"
SERVER_COMMAND = "bin/rails fly:server"

[deploy]
release_command = "bin/rails fly:release"

[env]
PORT = "8080"

[experimental]
allowed_public_ports = []
auto_rollback = true

[[services]]
http_checks = []
internal_port = 8080
processes = ["app"]
protocol = "tcp"
script_checks = []
[services.concurrency]
hard_limit = 25
soft_limit = 20
type = "connections"

[[services.ports]]
force_https = true
handlers = ["http"]
port = 80

[[services.ports]]
handlers = ["tls", "http"]
port = 443

[[services.tcp_checks]]
grace_period = "1s"
interval = "15s"
restart_limit = 0
timeout = "2s"

[[statics]]
guest_path = "/app/public"
url_prefix = "/"
2 changes: 0 additions & 2 deletions lib/tasks/2022/get_team_match_info.rake
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'HTTParty'

BASE_URL = 'https://api.fifa.com/api/v3/calendar/matches'.freeze
BASE_PARAMS = '?from=2022-11-19T00%3A00%3A00Z&to=2022-12-31T23%3A59%3A59Z&language=en&count=500&idCompetition=17'.freeze

Expand Down
37 changes: 37 additions & 0 deletions lib/tasks/fly.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# commands used to deploy a Rails application
namespace :fly do
# BUILD step:
# - changes to the filesystem made here DO get deployed
# - NO access to secrets, volumes, databases
# - Failures here prevent deployment
task :build => 'assets:precompile'

# RELEASE step:
# - changes to the filesystem made here are DISCARDED
# - full access to secrets, databases
# - failures here prevent deployment
task :release => 'db:migrate'

# SERVER step:
# - changes to the filesystem made here are deployed
# - full access to secrets, databases
# - failures here result in VM being stated, shutdown, and rolled back
# to last successful deploy (if any).
task :server => :swapfile do
sh 'bin/rails server'
end

# optional SWAPFILE task:
# - adjust fallocate size as needed
# - performance critical applications should scale memory to the
# point where swap is rarely used. 'fly scale help' for details.
# - disable by removing dependency on the :server task, thus:
# task :server do
task :swapfile do
sh 'fallocate -l 512M /swapfile'
sh 'chmod 0600 /swapfile'
sh 'mkswap /swapfile'
sh 'echo 10 > /proc/sys/vm/swappiness'
sh 'swapon /swapfile'
end
end
11 changes: 0 additions & 11 deletions package.json

This file was deleted.

Loading

0 comments on commit 950dd29

Please sign in to comment.