Skip to content

Commit

Permalink
Merge pull request #317 from collectiveidea/dg/fly
Browse files Browse the repository at this point in the history
Fly deploy
  • Loading branch information
albus522 authored Aug 2, 2024
2 parents 9ad176b + 4e445f3 commit 65f84d1
Show file tree
Hide file tree
Showing 21 changed files with 478 additions and 176 deletions.
13 changes: 7 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,37 @@ jobs:
environment:
RAILS_ENV: test
docker:
- image: cimg/ruby:3.3.0-node
- image: cimg/ruby:3.3.4-node
environment:
PGHOST: localhost
PGUSER: buildlight
RAILS_ENV: test
- image: postgres:10.1-alpine
- image: postgres:16-alpine
environment:
POSTGRES_USER: buildlight
POSTGRES_DB: buildlight_test
POSTGRES_PASSWORD: ""
POSTGRES_PASSWORD: "correcthorsebatterystaple"
steps:
- checkout
- run: echo -e "export RAILS_ENV=test\nexport RACK_ENV=test" >> $BASH_ENV
- run: 'bundle check --path=vendor/bundle || bundle install --path=vendor/bundle
--jobs=4 --retry=3 '
- run:
name: Install Yarn Dependencies
command: yarn install --cache-folder ~/.yarn-cache
name: Install NPM Dependencies
command: npm install
- run: |-
mkdir -p config && echo 'test:
database: buildlight_test
adapter: postgresql
encoding: unicode
pool: 15
username: buildlight
password: correcthorsebatterystaple
host: localhost
' > config/database.yml
- run:
command: bundle exec bin/setup
- run:
command: yarn build:css
command: npm run build:css
- run:
command: bundle exec rake
37 changes: 37 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# See https://docs.docker.com/engine/reference/builder/#dockerignore-file for more about ignoring files.

# Ignore git directory.
/.git/

# Ignore bundler config.
/.bundle

# Ignore all environment files (except templates).
/.env*
!/.env*.erb

# Ignore all default key files.
/config/master.key
/config/credentials/*.key

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore pidfiles, but keep the directory.
/tmp/pids/*
!/tmp/pids/.keep

# Ignore storage (uploaded files in development and any SQLite databases).
/storage/*
!/storage/.keep
/tmp/storage/*
!/tmp/storage/.keep

# Ignore assets.
/node_modules/
/app/assets/builds/*
!/app/assets/builds/.keep
/public/assets
16 changes: 8 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ jobs:
with:
bundler-cache: true

- uses: actions/cache@v4
- name: Set up Node
uses: actions/setup-node@v4
with:
path: ~/.yarn-cache
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Yarn Install
run: yarn install --cache-folder ~/.yarn-cache
node-version-file: 'package.json'
cache: 'npm'

- name: NPM Install
run: npm ci

- name: App Setup
run: bin/setup

- name: Build CSS
run: yarn build:css
run: npm run build:css

- name: Standard
run: bundle exec rake standard
Expand Down
18 changes: 18 additions & 0 deletions .github/workflows/fly-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# See https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/

name: Fly Deploy
on:
push:
branches:
- main
jobs:
deploy:
name: Deploy app
runs-on: ubuntu-latest
concurrency: deploy-group # optional: ensure only one action runs at a time
steps:
- uses: actions/checkout@v4
- uses: superfly/flyctl-actions/setup-flyctl@master
- run: flyctl deploy --remote-only
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22.4.0
104 changes: 104 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.3.4
FROM --platform=linux/amd64 ruby:$RUBY_VERSION-alpine as base

LABEL fly_launch_runtime="rails"

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development:test" \
RAILS_ENV="production"

# Update gems and bundler
RUN gem update --system --no-document && \
gem install -N bundler

# Install packages
RUN apk update && \
apk add tzdata && \
rm -rf /var/cache/apk/*


# Throw-away build stages to reduce size of final image
FROM base as prebuild

# Install packages needed to build gems and node modules
RUN apk update && \
apk add build-base curl git gyp libpq-dev pkgconfig python3


FROM prebuild as node

# Install Node.js
ARG NODE_VERSION=22.4.0
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://unofficial-builds.nodejs.org/download/release/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64-musl.tar.gz | tar xz -C /tmp/ && \
mkdir /usr/local/node && \
cp -rp /tmp/node-v${NODE_VERSION}-linux-x64-musl/* /usr/local/node/ && \
rm -rf /tmp/node-v${NODE_VERSION}-linux-x64-musl

# Install node modules
COPY --link package.json package-lock.json ./
RUN npm install


FROM prebuild as build

# Install application gems
COPY --link Gemfile Gemfile.lock ./
RUN bundle install && \
bundle exec bootsnap precompile --gemfile && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git

# Copy node modules
COPY --from=node /rails/node_modules /rails/node_modules
COPY --from=node /usr/local/node /usr/local/node
ENV PATH=/usr/local/node/bin:$PATH

# Copy application code
COPY --link . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

# Adjust binfiles to set current working directory
RUN grep -l '#!/usr/bin/env ruby' /rails/bin/* | xargs sed -i '/^#!/aDir.chdir File.expand_path("..", __dir__)'

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile


# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apk update && \
apk add curl gzip jemalloc libpq postgresql-client && \
rm -rf /var/cache/apk/*

# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN addgroup --system --gid 1000 rails && \
adduser --system rails --uid 1000 --ingroup rails --home /home/rails --shell /bin/sh rails && \
chown -R 1000:1000 db log tmp
USER 1000:1000

# Deployment options
ENV LD_PRELOAD="libjemalloc.so.2" \
MALLOC_CONF="dirty_decay_ms:1000,narenas:2,background_thread:true"

# Entrypoint sets up the container.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]
7 changes: 2 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
source "https://rubygems.org"
ruby "3.3.0"
ruby "3.3.4"

gem "rails", "~> 7.1.3"

gem "pg"

gem "bootsnap"
gem "dockerfile-rails"
gem "honeybadger"
gem "particlerb"
gem "puma"
Expand All @@ -18,10 +19,6 @@ gem "propshaft"
# https://github.com/faye/websocket-driver-ruby/pull/85
gem "websocket-driver", github: "danielmorrison/websocket-driver-ruby", branch: "support-frozen-by-default"

group :production do
gem "lograge"
end

group :development, :test do
gem "debug"
gem "factory_bot_rails"
Expand Down
25 changes: 15 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ GEM
irb (~> 1.10)
reline (>= 0.3.8)
diff-lcs (1.5.1)
dockerfile-rails (1.6.17)
rails (>= 3.0.0)
drb (2.2.1)
erubi (1.12.0)
factory_bot (6.4.6)
Expand Down Expand Up @@ -150,11 +152,6 @@ GEM
json (2.7.2)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
lograge (0.14.0)
actionpack (>= 4)
activesupport (>= 4)
railties (>= 4)
request_store (~> 1.0)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand Down Expand Up @@ -183,6 +180,12 @@ GEM
nokogiri (1.16.5)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
nokogiri (1.16.5-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.5-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.5-x86_64-linux)
racc (~> 1.4)
parallel (1.25.1)
parser (3.3.4.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -245,8 +248,6 @@ GEM
regexp_parser (2.9.2)
reline (0.5.8)
io-console (~> 0.5)
request_store (1.6.0)
rack (>= 1.4)
rexml (3.3.2)
strscan
rspec (3.13.0)
Expand Down Expand Up @@ -326,17 +327,21 @@ GEM
zeitwerk (2.6.15)

PLATFORMS
aarch64-linux
arm64-darwin
ruby
x86_64-linux
x86_64-linux-musl

DEPENDENCIES
bootsnap
cssbundling-rails
debug
dockerfile-rails
factory_bot_rails
figaro
honeybadger
importmap-rails
lograge
particlerb
pg
propshaft
Expand All @@ -350,7 +355,7 @@ DEPENDENCIES
websocket-driver!

RUBY VERSION
ruby 3.3.0p0
ruby 3.3.4p94

BUNDLED WITH
2.5.7
2.5.17
2 changes: 1 addition & 1 deletion Procfile.dev
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
web: bin/rails server -p 3000
css: yarn build:css --watch
css: npm run build:css -- --watch
5 changes: 5 additions & 0 deletions bin/docker-entrypoint
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh -e

# Add any container initialization steps here

exec "${@}"
11 changes: 0 additions & 11 deletions bin/yarn

This file was deleted.

15 changes: 14 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
require_relative "boot"

require "rails/all"
require "rails"

# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
# require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
# require "action_mailbox/engine"
# require "action_text/engine"
require "action_view/railtie"
require "action_cable/engine"
# require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Expand Down
18 changes: 18 additions & 0 deletions config/dockerfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# generated by dockerfile-rails

---
options:
alpine: true
bin-cd: true
gemfile-updates: false
jemalloc: true
label:
fly_launch_runtime: rails
parallel: true
platform: linux/amd64
prepare: false
packages:
build:
- git
deploy:
- gzip
Loading

0 comments on commit 65f84d1

Please sign in to comment.