Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
46 changes: 46 additions & 0 deletions .controlplane/controlplane.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Keys beginning with "cpln_" correspond to your settings in Control Plane.
#
# This demo uses the repository root Dockerfile and mounts `/rails/storage`
# so SQLite, Solid Cache, Solid Queue, and Solid Cable state persist.

allow_org_override_by_env: true
allow_app_override_by_env: true

aliases:
common: &common
cpln_org: my-org-staging
default_location: aws-us-east-2

setup_app_templates:
- app
- storage
- rails

one_off_workload: rails
app_workloads:
- rails
additional_workloads: []

dockerfile: ../Dockerfile
release_script: release_script.sh

stale_app_image_deployed_days: 5
image_retention_days: 7

production: &production
<<: *common
allow_org_override_by_env: false
allow_app_override_by_env: false
cpln_org: my-org-production
upstream: react-on-rails-demo-v16-ssr-staging

apps:
react-on-rails-demo-v16-ssr-staging:
<<: *common

react-on-rails-demo-v16-ssr-review:
<<: *common
match_if_app_name_starts_with: true

react-on-rails-demo-v16-ssr-production:
<<: *production
66 changes: 66 additions & 0 deletions .controlplane/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Control Plane Deployment Notes

This repo now includes `cpflow` scaffolding for:

- opt-in PR review apps
- automatic staging deploys from `main`
- manual promotion from staging to production

## Why This Shape

This app ships a production-ready Dockerfile at the repository root and stores
its production SQLite, Solid Cache, Solid Queue, and Solid Cable databases under
`/rails/storage`.

The Control Plane setup mirrors that:

- `.controlplane/controlplane.yml` points `dockerfile: ../Dockerfile`
- `templates/storage.yml` creates a persistent volume for `/rails/storage`
- `templates/rails.yml` runs the public `rails` workload on port `80`
- `release_script.sh` runs `bin/rails db:prepare` before deploys switch images

The root `Dockerfile` now installs Node.js in both the build and runtime stages,
compiles the auto-generated React on Rails packs plus `server-bundle.js`, and
keeps Node available for ExecJS-based SSR at runtime.

## Required Runtime Secrets

Before the app will boot on Control Plane, populate `SECRET_KEY_BASE` in the
generated secret dictionaries:

- `react-on-rails-demo-v16-ssr-staging-secrets`
- `react-on-rails-demo-v16-ssr-review-secrets`
- `react-on-rails-demo-v16-ssr-production-secrets`

`cpflow setup-app` creates those dictionaries automatically. You only need to
add a `SECRET_KEY_BASE` entry to each one before the first deploy.

## Local cpflow Flow

Typical setup:

```sh
export APP_NAME=react-on-rails-demo-v16-ssr-staging

cpflow setup-app -a "$APP_NAME"
cpflow build-image -a "$APP_NAME"
cpflow deploy-image -a "$APP_NAME" --run-release-phase
cpflow open -a "$APP_NAME"
```

## GitHub Actions Variables And Secrets

Set these in GitHub before enabling the generated `cpflow-*` workflows:

- `CPLN_TOKEN_STAGING`
- `CPLN_TOKEN_PRODUCTION`
- `CPLN_ORG_STAGING`
- `CPLN_ORG_PRODUCTION`
- `STAGING_APP_NAME=react-on-rails-demo-v16-ssr-staging`
- `PRODUCTION_APP_NAME=react-on-rails-demo-v16-ssr-production`
- `REVIEW_APP_PREFIX=react-on-rails-demo-v16-ssr-review`

Optional:

- `STAGING_APP_BRANCH=main`
- `PRIMARY_WORKLOAD=rails`
5 changes: 5 additions & 0 deletions .controlplane/release_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

set -euo pipefail

./bin/rails db:prepare
17 changes: 17 additions & 0 deletions .controlplane/templates/app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
kind: gvc
name: {{APP_NAME}}
spec:
env:
- name: RAILS_ENV
value: production
- name: RAILS_LOG_LEVEL
value: info
- name: RAILS_SERVE_STATIC_FILES
value: "true"
- name: SOLID_QUEUE_IN_PUMA
value: "true"
- name: SECRET_KEY_BASE
value: cpln://secret/{{APP_SECRETS}}.SECRET_KEY_BASE
staticPlacement:
locationLinks:
- {{APP_LOCATION_LINK}}
29 changes: 29 additions & 0 deletions .controlplane/templates/rails.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
kind: workload
name: rails
spec:
type: standard
containers:
- name: rails
cpu: 500m
inheritEnv: true
image: {{APP_IMAGE_LINK}}
memory: 1Gi
ports:
- number: 80
protocol: http
volumes:
- path: /rails/storage
recoveryPolicy: retain
uri: cpln://volumeset/rails-storage
defaultOptions:
autoscaling:
minScale: 1
maxScale: 1
capacityAI: false
timeoutSeconds: 60
firewallConfig:
external:
inboundAllowCIDR:
- 0.0.0.0/0
outboundAllowCIDR:
- 0.0.0.0/0
6 changes: 6 additions & 0 deletions .controlplane/templates/storage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: volumeset
name: rails-storage
spec:
fileSystemType: ext4
initialCapacity: 10
performanceClass: general-purpose-ssd
70 changes: 70 additions & 0 deletions .github/actions/cpflow-build-docker-image/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Build Docker Image
description: Builds and pushes the app image for a Control Plane workload

inputs:
app_name:
description: Name of the application
required: true
org:
description: Control Plane organization name
required: true
commit:
description: Commit SHA to tag the image with
required: true
pr_number:
description: Pull request number for status messaging
required: false
docker_build_extra_args:
description: Optional newline-delimited extra arguments passed through to docker build
required: false
docker_build_ssh_key:
description: Optional private SSH key used for Docker builds that fetch private dependencies with RUN --mount=type=ssh
required: false

outputs:
image_tag:
description: Fully qualified image tag
value: ${{ steps.build.outputs.image_tag }}

runs:
using: composite
steps:
- name: Build Docker image
id: build
shell: bash
run: |
set -euo pipefail

PR_INFO=""
docker_build_args=()

if [[ -n "${{ inputs.pr_number }}" ]]; then
PR_INFO=" for PR #${{ inputs.pr_number }}"
fi

if [[ -n "${{ inputs.docker_build_extra_args }}" ]]; then
while IFS= read -r arg; do
arg="${arg%$'\r'}"
[[ -n "${arg}" ]] || continue
docker_build_args+=("${arg}")
done <<< "${{ inputs.docker_build_extra_args }}"
fi

if [[ -n "${{ inputs.docker_build_ssh_key }}" ]]; then
mkdir -p ~/.ssh
chmod 700 ~/.ssh
ssh-keyscan -H github.com >> ~/.ssh/known_hosts
chmod 600 ~/.ssh/known_hosts

eval "$(ssh-agent -s)"
trap 'ssh-agent -k >/dev/null' EXIT
ssh-add - <<< "${{ inputs.docker_build_ssh_key }}"
docker_build_args+=("--ssh" "default")
fi

echo "🏗️ Building Docker image${PR_INFO} (commit ${{ inputs.commit }})..."
cpflow build-image -a "${{ inputs.app_name }}" --commit="${{ inputs.commit }}" --org="${{ inputs.org }}" "${docker_build_args[@]}"

image_tag="${{ inputs.org }}/${{ inputs.app_name }}:${{ inputs.commit }}"
echo "image_tag=${image_tag}" >> "$GITHUB_OUTPUT"
echo "✅ Docker image build successful${PR_INFO} (commit ${{ inputs.commit }})"
24 changes: 24 additions & 0 deletions .github/actions/cpflow-delete-control-plane-app/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Delete Control Plane App
description: Deletes a Control Plane app and all associated resources

inputs:
app_name:
description: Name of the application to delete
required: true
cpln_org:
description: Control Plane organization name
required: true
review_app_prefix:
description: Prefix used for review app names
required: true

runs:
using: composite
steps:
- name: Delete application
shell: bash
run: ${{ github.action_path }}/delete-app.sh
env:
APP_NAME: ${{ inputs.app_name }}
CPLN_ORG: ${{ inputs.cpln_org }}
REVIEW_APP_PREFIX: ${{ inputs.review_app_prefix }}
37 changes: 37 additions & 0 deletions .github/actions/cpflow-delete-control-plane-app/delete-app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

set -euo pipefail

: "${APP_NAME:?APP_NAME environment variable is required}"
: "${CPLN_ORG:?CPLN_ORG environment variable is required}"
: "${REVIEW_APP_PREFIX:?REVIEW_APP_PREFIX environment variable is required}"

expected_prefix="${REVIEW_APP_PREFIX}-"
if [[ "$APP_NAME" != "${expected_prefix}"* ]]; then
echo "❌ ERROR: refusing to delete an app outside the review app prefix" >&2
echo "App name: $APP_NAME" >&2
echo "Expected prefix: ${expected_prefix}" >&2
exit 1
fi

echo "🔍 Checking if application exists: $APP_NAME"
exists_output=""
if ! exists_output="$(cpflow exists -a "$APP_NAME" --org "$CPLN_ORG" 2>&1)"; then
if [[ -z "$exists_output" ]]; then
echo "⚠️ Application does not exist: $APP_NAME"
exit 0
fi

echo "❌ ERROR: failed to determine whether application exists: $APP_NAME" >&2
printf '%s\n' "$exists_output" >&2
exit 1
fi

if [[ -n "$exists_output" ]]; then
printf '%s\n' "$exists_output"
fi

echo "🗑️ Deleting application: $APP_NAME"
cpflow delete -a "$APP_NAME" --org "$CPLN_ORG" --yes

echo "✅ Successfully deleted application: $APP_NAME"
70 changes: 70 additions & 0 deletions .github/actions/cpflow-setup-environment/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Setup Control Plane Environment
description: Sets up Ruby, installs the Control Plane CLI and cpflow gem, and configures a default profile

inputs:
token:
description: Control Plane token
required: true
org:
description: Control Plane organization
required: true
ruby_version:
description: Ruby version used for cpflow
required: false
default: "3.4.6"
cpln_cli_version:
description: "@controlplane/cli version"
required: false
default: "3.3.1"
cpflow_version:
description: cpflow gem version
required: false
default: "4.2.0"

runs:
using: composite
steps:
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ inputs.ruby_version }}

- name: Install Control Plane CLI and cpflow gem
shell: bash
run: |
set -euo pipefail

sudo npm install -g @controlplane/cli@${{ inputs.cpln_cli_version }}
cpln --version

gem install cpflow -v ${{ inputs.cpflow_version }}
cpflow --version

- name: Setup Control Plane profile and registry login
shell: bash
run: |
set -euo pipefail

TOKEN="${{ inputs.token }}"
ORG="${{ inputs.org }}"

if [[ -z "$TOKEN" ]]; then
echo "Error: Control Plane token not provided" >&2
exit 1
fi

if [[ -z "$ORG" ]]; then
echo "Error: Control Plane organization not provided" >&2
exit 1
fi

create_output=""
if ! create_output="$(cpln profile create default --token "$TOKEN" --org "$ORG" 2>&1)"; then
if ! echo "$create_output" | grep -qi "already exists"; then
echo "$create_output" >&2
exit 1
fi
fi

cpln profile update default --org "$ORG" --token "$TOKEN"
cpln image docker-login --org "$ORG"
13 changes: 13 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ jobs:
ruby-version: .ruby-version
bundler-cache: true

- name: Install JavaScript dependencies
run: npm ci

- name: Generate React on Rails packs
env:
RAILS_ENV: test
run: bundle exec rake react_on_rails:generate_packs

- name: Build test bundles
env:
RAILS_ENV: test
run: bin/shakapacker
Comment thread
cursor[bot] marked this conversation as resolved.

- name: Run tests
env:
RAILS_ENV: test
Expand Down
Loading
Loading