Skip to content

Commit 434b414

Browse files
committed
Deploy with Kamal & Github Actions
1 parent 6812276 commit 434b414

File tree

6 files changed

+111
-6
lines changed

6 files changed

+111
-6
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: CI
33
on:
44
pull_request:
55
push:
6-
branches: [ main ]
6+
branches: [main]
77

88
jobs:
99
scan_ruby:

.kamal/secrets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
# Grab the registry password from ENV
1414
KAMAL_REGISTRY_PASSWORD=$KAMAL_REGISTRY_PASSWORD
1515

16-
# Improve security by using a password manager. Never check config/master.key into git!
17-
RAILS_MASTER_KEY=$(cat config/master.key)
16+
# Grab the master key from ENV
17+
RAILS_MASTER_KEY=${RAILS_MASTER_KEY}

Dockerfile-ssr

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# syntax=docker/dockerfile:1
2+
# check=error=true
3+
4+
# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand:
5+
# docker build -t inertia_rails_shadcn_starter .
6+
# docker run -d -p 80:80 -e RAILS_MASTER_KEY=<value from config/master.key> --name inertia_rails_shadcn_starter inertia_rails_shadcn_starter
7+
8+
# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html
9+
10+
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
11+
ARG RUBY_VERSION=3.4.5
12+
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
13+
14+
# Rails app lives here
15+
WORKDIR /rails
16+
17+
# Install base packages
18+
RUN apt-get update -qq && \
19+
apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 && \
20+
rm -rf /var/lib/apt/lists /var/cache/apt/archives
21+
22+
# Install JavaScript dependencies
23+
ARG NODE_VERSION=22.13.1
24+
ENV PATH=/usr/local/node/bin:$PATH
25+
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
26+
/tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
27+
rm -rf /tmp/node-build-master
28+
29+
# Set production environment
30+
ENV RAILS_ENV="production" \
31+
BUNDLE_DEPLOYMENT="1" \
32+
BUNDLE_PATH="/usr/local/bundle" \
33+
BUNDLE_WITHOUT="development:test"
34+
35+
# Throw-away build stage to reduce size of final image
36+
FROM base AS build
37+
38+
# Install packages needed to build gems and node modules
39+
RUN apt-get update -qq && \
40+
apt-get install --no-install-recommends -y build-essential git libyaml-dev node-gyp pkg-config python-is-python3 && \
41+
rm -rf /var/lib/apt/lists /var/cache/apt/archives
42+
43+
# Install application gems
44+
COPY Gemfile Gemfile.lock vendor ./
45+
46+
RUN bundle install && \
47+
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
48+
bundle exec bootsnap precompile --gemfile
49+
50+
# Install node modules
51+
COPY package.json package-lock.json ./
52+
RUN npm ci && \
53+
rm -rf ~/.npm
54+
55+
# Copy application code
56+
COPY . .
57+
58+
# Precompile bootsnap code for faster boot times
59+
RUN bundle exec bootsnap precompile app/ lib/
60+
61+
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
62+
RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile && \
63+
./bin/vite build --ssr
64+
65+
RUN rm -rf node_modules
66+
67+
68+
# Final stage for app image
69+
FROM base
70+
71+
# Run and own only the runtime files as a non-root user for security
72+
RUN groupadd --system --gid 1000 rails && \
73+
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash
74+
75+
USER 1000:1000
76+
77+
# Copy built artifacts: gems, application
78+
COPY --chown=rails:rails --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
79+
COPY --chown=rails:rails --from=build /rails /rails
80+
81+
# Entrypoint prepares the database.
82+
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
83+
84+
# Start server via Thruster by default, this can be overwritten at runtime
85+
EXPOSE 80
86+
CMD ["./bin/thrust", "./bin/rails", "server"]

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ gem "solid_cable"
2828
gem "bootsnap", require: false
2929

3030
# Deploy this application anywhere as a Docker container [https://kamal-deploy.org]
31-
gem "kamal", require: false
31+
gem "kamal", require: false, group: [:development, :deploy]
3232

3333
# Add HTTP asset caching/compression and X-Sendfile acceleration to Puma [https://github.com/basecamp/thruster/]
3434
gem "thruster", require: false

config/deploy.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ image: your-user/svelte_starter_kit
88
servers:
99
web:
1010
- 192.168.0.1
11+
12+
# Uncomment to enable SSR:
13+
# vite_ssr:
14+
# hosts:
15+
# - 192.168.0.1
16+
# cmd: bundle exec vite ssr
17+
# options:
18+
# network-alias: vite_ssr
19+
1120
# job:
1221
# hosts:
1322
# - 192.168.0.1
@@ -40,6 +49,10 @@ env:
4049
# When you start using multiple servers, you should split out job processing to a dedicated machine.
4150
SOLID_QUEUE_IN_PUMA: true
4251

52+
# Uncomment to enable SSR:
53+
# INERTIA_SSR_ENABLED: true
54+
# INERTIA_SSR_URL: "http://vite_ssr:13714"
55+
4356
# Set number of processes dedicated to Solid Queue (default: 1)
4457
# JOB_CONCURRENCY: 3
4558

@@ -71,11 +84,17 @@ volumes:
7184
# Bridge fingerprinted assets, like JS and CSS, between versions to avoid
7285
# hitting 404 on in-flight requests. Combines all files from new and old
7386
# version inside the asset_path.
74-
asset_path: /rails/public/assets
87+
asset_path: /rails/public
7588

7689
# Configure the image builder.
7790
builder:
7891
arch: amd64
92+
# Uncomment to enable SSR:
93+
# dockerfile: Dockerfile-ssr
94+
cache:
95+
type: registry
96+
image: your-user/vue_starter_kit-build-cache
97+
options: mode=max
7998

8099
# # Build image via remote server (useful for faster amd64 builds on arm64 computers)
81100
# remote: ssh://docker@docker-builder-server

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
# get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
1313

1414
# Defines the root path route ("/")
15-
# root "posts#index"
15+
root "inertia_example#index"
1616
end

0 commit comments

Comments
 (0)