Skip to content

Commit

Permalink
Setup Kamal deployment (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillplatonov authored Mar 4, 2024
1 parent fe6e637 commit 4d1cf21
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 36 deletions.
69 changes: 69 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# syntax = docker/dockerfile:1

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

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development" \
WEB_CONCURRENCY="2" \
RAILS_MAX_THREADS="5"

# Throw-away build stage to reduce size of final image
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 libvips pkg-config

# Copy application code
COPY . .

# Install application gems
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git

# Rails app lives here
WORKDIR /rails/demo

# Install application gems
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git

# 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 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 /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

WORKDIR /rails/demo

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
chown -R rails:rails db log tmp
USER rails:rails

RUN pwd

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

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]
25 changes: 25 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ GEM
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
base64 (0.2.0)
bcrypt_pbkdf (1.1.0)
bigdecimal (3.1.6)
builder (3.2.4)
capybara (3.40.0)
Expand All @@ -104,7 +105,9 @@ GEM
css_parser (1.16.0)
addressable
date (3.3.4)
dotenv (2.8.1)
drb (2.2.1)
ed25519 (1.3.0)
erubi (1.12.0)
foreman (0.87.2)
globalid (1.2.1)
Expand All @@ -122,6 +125,17 @@ GEM
rdoc
reline (>= 0.4.2)
json (2.7.1)
kamal (1.3.1)
activesupport (>= 7.0)
base64 (~> 0.2)
bcrypt_pbkdf (~> 1.0)
concurrent-ruby (~> 1.2)
dotenv (~> 2.8)
ed25519 (~> 1.2)
net-ssh (~> 7.0)
sshkit (~> 1.21)
thor (~> 1.2)
zeitwerk (~> 2.5)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
loofah (2.22.0)
Expand Down Expand Up @@ -157,8 +171,13 @@ GEM
net-protocol
net-protocol (0.2.2)
timeout
net-scp (4.0.0)
net-ssh (>= 2.6.5, < 8.0.0)
net-sftp (4.0.0)
net-ssh (>= 5.0.0, < 8.0.0)
net-smtp (0.4.0.1)
net-protocol
net-ssh (7.2.1)
nio4r (2.7.0)
nokogiri (1.16.2-aarch64-linux)
racc (~> 1.4)
Expand Down Expand Up @@ -261,6 +280,11 @@ GEM
sqlite3 (1.7.2-arm64-darwin)
sqlite3 (1.7.2-x86_64-darwin)
sqlite3 (1.7.2-x86_64-linux)
sshkit (1.22.0)
mutex_m
net-scp (>= 1.1.2)
net-sftp (>= 2.1.2)
net-ssh (>= 2.8.0)
standard (1.34.0)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
Expand Down Expand Up @@ -317,6 +341,7 @@ DEPENDENCIES
capybara
foreman
importmap-rails
kamal
lookbook (~> 2.0)
minitest (~> 5.0)
polaris_view_components!
Expand Down
4 changes: 2 additions & 2 deletions bin/release
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ rm "polaris_view_components-$VERSION.gem"
echo "Need to publish NPM package too?"

while true; do
read -p "Deploy new version to Render? [Yn] " yn
read -p "Deploy new version of polarisviewcomponents.org? [Yn] " yn
case $yn in
[Yy]* )
git push origin main:render
kamal deploy
break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
Expand Down
11 changes: 0 additions & 11 deletions bin/render-build.sh

This file was deleted.

37 changes: 37 additions & 0 deletions config/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
service: polaris_view_components
image: kirillplatonov/polaris_view_components

registry:
username: kirillplatonov
password:
- KAMAL_REGISTRY_PASSWORD

env:
secret:
- RAILS_MASTER_KEY

servers:
web:
hosts:
- 195.201.128.126
labels:
traefik.http.routers.polaris_view_components.entrypoints: websecure
traefik.http.routers.polaris_view_components.rule: "Host(`polarisviewcomponents.org`) || Host(`www.polarisviewcomponents.org`)"
traefik.http.routers.polaris_view_components.tls.certresolver: letsencrypt

traefik:
options:
publish:
- "443:443"
volume:
- "/letsencrypt/acme.json:/letsencrypt/acme.json"
args:
entryPoints.web.address: ":80"
entryPoints.websecure.address: ":443"
entryPoints.web.http.redirections.entryPoint.to: websecure # We want to force https
entryPoints.web.http.redirections.entryPoint.scheme: https
entryPoints.web.http.redirections.entrypoint.permanent: true
certificatesResolvers.letsencrypt.acme.email: "[email protected]"
certificatesResolvers.letsencrypt.acme.storage: "/letsencrypt/acme.json" # Must match the path in `volume`
certificatesResolvers.letsencrypt.acme.httpchallenge: true
certificatesResolvers.letsencrypt.acme.httpchallenge.entrypoint: web
8 changes: 8 additions & 0 deletions demo/bin/docker-entrypoint
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash -e

# If running the rails server then create or migrate existing database
if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then
./bin/rails db:prepare
fi

exec "${@}"
1 change: 1 addition & 0 deletions polaris_view_components.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "minitest", "~> 5.0"
spec.add_development_dependency "pry"
spec.add_development_dependency "sprockets-rails"
spec.add_development_dependency "kamal"
end
23 changes: 0 additions & 23 deletions render.yaml

This file was deleted.

0 comments on commit 4d1cf21

Please sign in to comment.