Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/.git/
/.github/
**/node_modules/
/frontend/dist/
/backend/.bundle
/backend/config/master.key
/backend/log/*
/backend/tmp/*
/backend/storage/*
**/.env*
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,20 @@ jobs:
run: npm run lint
- name: Type check and build
run: npm run build

docker:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7

- name: Build the production image
run: docker build -t devops-sim .

- name: Start it and check that it serves the app
run: |
docker run -d --name app -p 3000:3000 -e SECRET_KEY_BASE=ci-only-secret devops-sim
for i in $(seq 1 30); do curl -sf http://localhost:3000/up && break; sleep 1; done
curl -sf -H 'X-Forwarded-Proto: https' http://localhost:3000/ | grep -q '<div id="root">'
curl -sf -H 'X-Forwarded-Proto: https' http://localhost:3000/api/scenarios | grep -q github-first-pr
- if: failure()
run: docker logs app || true
67 changes: 67 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# syntax=docker/dockerfile:1
# check=error=true

# One image for the whole app: Vite builds the React frontend, and Rails serves
# it next to the API and Action Cable. Render builds this (see render.yaml).
#
# docker build -t devops-sim .
# docker run -p 3000:3000 -e SECRET_KEY_BASE=$(openssl rand -hex 64) devops-sim

# Keep in step with backend/.ruby-version. Declared before the first FROM so
# every stage can use it.
ARG RUBY_VERSION=3.3.14

# --- frontend ---------------------------------------------------------------
FROM docker.io/library/node:22-slim AS frontend
WORKDIR /frontend
COPY frontend/package.json frontend/package-lock.json ./
RUN npm ci
COPY frontend/ ./
RUN npm run build

# --- backend ----------------------------------------------------------------
FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base
WORKDIR /rails

RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libjemalloc2 sqlite3 && \
ln -s /usr/lib/$(uname -m)-linux-gnu/libjemalloc.so.2 /usr/local/lib/libjemalloc.so && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development:test" \
LD_PRELOAD="/usr/local/lib/libjemalloc.so"

FROM base AS build
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git libyaml-dev pkg-config && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

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

COPY backend/ ./
RUN bundle exec bootsnap precompile -j 1 app/ lib/

# The built frontend goes into public/. Its index.html is served by
# FrontendController so it isn't cached; the hashed assets are.
COPY --from=frontend /frontend/dist/ ./public/
RUN mv public/index.html public/app.html && mkdir -p storage log tmp

FROM base
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash
COPY --chown=rails:rails --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --chown=rails:rails --from=build /rails /rails
USER 1000:1000

# SQLite lives in storage/. On Render's free plan it starts empty on every deploy.
VOLUME /rails/storage

ENTRYPOINT ["/rails/bin/docker-entrypoint"]
EXPOSE 3000
CMD ["./bin/rails", "server"]
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ terminal, logs and dashboards for a small online shop, and things go wrong:
the disk fills up, memory leaks, a deploy breaks the site. Nothing is real,
so you can't break anything.

![The play screen in chapter 5: a system map of the shop with the background worker using too much memory, a memory chart, the chapter's steps, and a terminal](docs/play-screen.png)

## Chapters

| # | Topic | Chapter | Status |
Expand Down Expand Up @@ -47,3 +49,17 @@ curl -s -X POST localhost:3000/api/runs/1/tick
```

Live updates for a run are broadcast on the `RunChannel` (Action Cable, `/cable`, param `run_id`).

## Deploying to Render

The repo has a `Dockerfile` that builds the frontend and serves it from Rails, so
the whole app is one web service. `render.yaml` describes it as a Render Blueprint:

1. In Render, choose **New > Blueprint** and pick this repository.
2. Render reads `render.yaml`, creates the `devops-sim` web service on the free plan
and generates `SECRET_KEY_BASE`. Every push to `main` deploys again.

The app runs as one Puma process, so Action Cable uses its built-in async adapter
and no Redis is needed. Runs are saved in SQLite under `storage/`. The free plan has
no persistent disk, so saved runs are lost on each deploy or restart. To keep them,
use a paid plan and attach a disk at `/rails/storage`.
11 changes: 11 additions & 0 deletions backend/app/controllers/frontend_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Serves the built React app (see the Dockerfile). Its assets are fingerprinted and
# cached for a year by the static file server, so the page itself must not be cached.
class FrontendController < ActionController::API
def show
page = Rails.public_path.join("app.html")
return head :not_found unless page.exist?

response.headers["Cache-Control"] = "no-cache"
send_file page, type: "text/html", disposition: "inline"
end
end
8 changes: 8 additions & 0 deletions backend/bin/docker-entrypoint
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash -e

# Create or migrate the database before starting the server.
if [ "${@: -2:1}" == "./bin/rails" ] && [ "${@: -1:1}" == "server" ]; then
./bin/rails db:prepare
fi

exec "${@}"
5 changes: 2 additions & 3 deletions backend/config/cable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ test:
adapter: test

production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
channel_prefix: backend_production
# One server process (see render.yaml), so no Redis is needed.
adapter: async
2 changes: 1 addition & 1 deletion backend/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ test:
# ensure the database is located in a persisted volume.
production:
<<: *default
# database: path/to/persistent/storage/production.sqlite3
database: storage/production.sqlite3
5 changes: 2 additions & 3 deletions backend/config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = true

# Skip http-to-https redirect for the default health check endpoint.
# config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } }
# Skip the http-to-https redirect for the health check, which Render calls over plain HTTP.
config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } }

# Log to STDOUT with the current request id as a default log tag.
config.log_tags = [ :request_id ]
Expand Down
2 changes: 2 additions & 0 deletions backend/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
end

mount ActionCable.server => "/cable"

root "frontend#show"
end
20 changes: 20 additions & 0 deletions backend/test/integration/frontend_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "test_helper"

class FrontendTest < ActionDispatch::IntegrationTest
PAGE = Rails.public_path.join("app.html")

teardown { PAGE.delete if PAGE.exist? }

test "serves the built frontend at the root without caching it" do
PAGE.write("<!doctype html><title>DevOps Simulator</title>")
get "/"
assert_response :success
assert_equal "no-cache", response.headers["Cache-Control"]
assert_includes response.body, "DevOps Simulator"
end

test "answers 404 when the frontend has not been built" do
get "/"
assert_response :not_found
end
end
Binary file added docs/play-screen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions render.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Render Blueprint: one free web service built from the Dockerfile.
# The free plan has no persistent disk, so saved runs are lost on each deploy or
# restart. That's fine for a simulator, since every run starts fresh anyway.
services:
- type: web
name: devops-sim
runtime: docker
plan: free
healthCheckPath: /up
envVars:
- key: SECRET_KEY_BASE
generateValue: true
# One Puma process: Action Cable's async adapter only reaches clients
# connected to the same process, and the free plan has 512 MB.
- key: WEB_CONCURRENCY
value: "0"
Loading