Skip to content

Commit

Permalink
Add fly.io deploy files and replace yarn with bun
Browse files Browse the repository at this point in the history
* Removes /builds from manifest.js because Vite handles this now
  • Loading branch information
davidalejandroaguilar committed Sep 17, 2024
1 parent 1bce140 commit 0280fbc
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 1,253 deletions.
15 changes: 5 additions & 10 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# Ignore git directory.
/.git/
/.gitignore

# Ignore bundler config.
/.bundle
Expand Down Expand Up @@ -37,12 +36,8 @@
!/app/assets/builds/.keep
/public/assets

# Ignore CI service files.
/.github

# Ignore development files
/.devcontainer

# Ignore Docker-related files
/.dockerignore
/Dockerfile*
# Vite Ruby
/public/vite*
# Vite uses dotenv and suggests to ignore local-only env files. See
# https://vitejs.dev/guide/env-and-mode.html#env-files
*.local
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 @@
18.15.0
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ruby 3.2.4
nodejs 18.15.0
66 changes: 40 additions & 26 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
# syntax = docker/dockerfile:1

# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
# docker build -t my-app .
# docker run -d -p 80:80 -p 443:443 --name my-app -e RAILS_MASTER_KEY=<value from config/master.key> my-app

# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.4
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
FROM ruby:$RUBY_VERSION-slim as base

LABEL fly_launch_runtime="rails"

# Rails app lives here
WORKDIR /rails

# Install base packages
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libjemalloc2 libsqlite3-0 libvips && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

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

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


# Throw-away build stage to reduce size of final image
FROM base AS build
FROM base as build

# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git pkg-config && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
apt-get install --no-install-recommends -y build-essential curl git libvips pkg-config unzip

# Install Bun
ARG BUN_VERSION=1.1.27
ENV BUN_INSTALL=/usr/local/bun
ENV PATH=/usr/local/bun/bin:$PATH
RUN curl -fsSL https://bun.sh/install | bash -s -- "bun-v${BUN_VERSION}"

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

# Install node modules
COPY --link package.json bun.lockb ./
RUN bun install --frozen-lockfile

# Copy application code
COPY . .
COPY --link . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/
Expand All @@ -48,24 +54,32 @@ RUN bundle exec bootsnap precompile app/ lib/
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile




# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libsqlite3-0 libvips && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# 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 groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
chown -R rails:rails db log storage tmp
mkdir /data && \
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
VOLUME /data
CMD ["./bin/rails", "server"]
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ end
group :development do
gem "standard", ">= 1.35.1"
gem "annotate"
gem "dockerfile-rails", ">= 1.6"
end

11 changes: 11 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ GEM
irb (~> 1.10)
reline (>= 0.3.8)
diff-lcs (1.5.1)
dockerfile-rails (1.6.17)
rails (>= 3.0.0)
dotenv (3.1.2)
drb (2.2.1)
dry-cli (1.1.0)
Expand All @@ -121,13 +123,17 @@ GEM
railties (>= 5.0.0)
faker (3.4.2)
i18n (>= 1.8.11, < 2)
ffi (1.17.0-aarch64-linux-gnu)
ffi (1.17.0-arm64-darwin)
ffi (1.17.0-x86_64-darwin)
ffi (1.17.0-x86_64-linux-gnu)
friendly_id (5.5.1)
activerecord (>= 4.0.0)
globalid (1.2.1)
activesupport (>= 6.1)
google-protobuf (4.27.3-aarch64-linux)
bigdecimal
rake (>= 13)
google-protobuf (4.27.3-arm64-darwin)
bigdecimal
rake (>= 13)
Expand Down Expand Up @@ -183,6 +189,8 @@ GEM
net-smtp (0.5.0)
net-protocol
nio4r (2.7.3)
nokogiri (1.16.7-aarch64-linux)
racc (~> 1.4)
nokogiri (1.16.7-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.7-x86_64-darwin)
Expand Down Expand Up @@ -308,6 +316,7 @@ GEM
actionpack (>= 6.1)
activesupport (>= 6.1)
sprockets (>= 3.0.0)
sqlite3 (1.7.3-aarch64-linux)
sqlite3 (1.7.3-arm64-darwin)
sqlite3 (1.7.3-x86_64-darwin)
sqlite3 (1.7.3-x86_64-linux)
Expand Down Expand Up @@ -360,6 +369,7 @@ GEM
zeitwerk (2.6.17)

PLATFORMS
aarch64-linux
arm64-darwin-23
x86_64-darwin-18
x86_64-linux
Expand All @@ -370,6 +380,7 @@ DEPENDENCIES
brakeman
capybara
debug
dockerfile-rails (>= 1.6)
dotenv
factory_bot_rails
faker
Expand Down
1 change: 0 additions & 1 deletion app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
//= link_tree ../images
//= link_directory ../stylesheets .css
//= link_tree ../builds
5 changes: 0 additions & 5 deletions bin/docker-entrypoint
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#!/bin/bash -e

# Enable jemalloc for reduced memory usage and latency.
if [ -z "${LD_PRELOAD+x}" ] && [ -f /usr/lib/*/libjemalloc.so.2 ]; then
export LD_PRELOAD="$(echo /usr/lib/*/libjemalloc.so.2)"
fi

# If running the rails server then create or migrate existing database
if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then
./bin/rails db:prepare
Expand Down
Binary file added bun.lockb
Binary file not shown.
6 changes: 6 additions & 0 deletions config/dockerfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# generated by dockerfile-rails

---
options:
label:
fly_launch_runtime: rails
49 changes: 49 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# fly.toml app configuration file generated for phlexy-ui-docs on 2024-09-18T00:37:00+02:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'phlexy-ui-docs'
primary_region = 'lax'
console_command = '/rails/bin/rails console'

[build]

[env]
DATABASE_URL = 'sqlite3:///data/production.sqlite3'

[[mounts]]
source = 'data'
destination = '/data'

[http_service]
internal_port = 3000
force_https = true
auto_stop_machines = 'stop'
auto_start_machines = true
min_machines_running = 0
processes = ['app']

[checks]
[checks.status]
port = 3000
type = 'http'
interval = '10s'
timeout = '2s'
grace_period = '5s'
method = 'GET'
path = '/up'
protocol = 'http'
tls_skip_verify = false

[checks.status.headers]
X-Forwarded-Proto = 'https'

[[vm]]
memory = '2gb'
cpu_kind = 'shared'
cpus = 1

[[statics]]
guest_path = '/rails/public'
url_prefix = '/'
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@
"vite": "^5.0.0",
"vite-plugin-ruby": "^5.0.0",
"vite-plugin-stimulus-hmr": "^3.0.0"
},
"packageManager": "yarn@1.22.22",
"scripts": {
"build": "vite build --outDir public"
}
}
}
Loading

0 comments on commit 0280fbc

Please sign in to comment.