diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..94268ec --- /dev/null +++ b/.dockerignore @@ -0,0 +1,10 @@ +/.git/ +/.github/ +**/node_modules/ +/frontend/dist/ +/backend/.bundle +/backend/config/master.key +/backend/log/* +/backend/tmp/* +/backend/storage/* +**/.env* diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2664b00..120e51f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 '
' + curl -sf -H 'X-Forwarded-Proto: https' http://localhost:3000/api/scenarios | grep -q github-first-pr + - if: failure() + run: docker logs app || true diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e9a662c --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/README.md b/README.md index 97138a5..d2bf4eb 100644 --- a/README.md +++ b/README.md @@ -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 | @@ -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`. diff --git a/backend/app/controllers/frontend_controller.rb b/backend/app/controllers/frontend_controller.rb new file mode 100644 index 0000000..2c564b1 --- /dev/null +++ b/backend/app/controllers/frontend_controller.rb @@ -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 diff --git a/backend/bin/docker-entrypoint b/backend/bin/docker-entrypoint new file mode 100755 index 0000000..6b4111e --- /dev/null +++ b/backend/bin/docker-entrypoint @@ -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 "${@}" diff --git a/backend/config/cable.yml b/backend/config/cable.yml index 75df6fd..2267834 100644 --- a/backend/config/cable.yml +++ b/backend/config/cable.yml @@ -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 diff --git a/backend/config/database.yml b/backend/config/database.yml index 956ab98..bae2950 100644 --- a/backend/config/database.yml +++ b/backend/config/database.yml @@ -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 diff --git a/backend/config/environments/production.rb b/backend/config/environments/production.rb index d9e3c4f..7b5aaa0 100644 --- a/backend/config/environments/production.rb +++ b/backend/config/environments/production.rb @@ -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 ] diff --git a/backend/config/routes.rb b/backend/config/routes.rb index b6ed199..db029e7 100644 --- a/backend/config/routes.rb +++ b/backend/config/routes.rb @@ -12,4 +12,6 @@ end mount ActionCable.server => "/cable" + + root "frontend#show" end diff --git a/backend/test/integration/frontend_test.rb b/backend/test/integration/frontend_test.rb new file mode 100644 index 0000000..8a3c4ba --- /dev/null +++ b/backend/test/integration/frontend_test.rb @@ -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("DevOps Simulator") + 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 diff --git a/docs/play-screen.png b/docs/play-screen.png new file mode 100644 index 0000000..7e9f6f3 Binary files /dev/null and b/docs/play-screen.png differ diff --git a/render.yaml b/render.yaml new file mode 100644 index 0000000..4889b39 --- /dev/null +++ b/render.yaml @@ -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"