Guide

January 26, 2024

Launch a Rails App

Build and deploy a Rails app. This Template comes configured with a Postgres database and a Redis cache

Overview

Ruby development and deployment on Noop should be familiar to anyone that’s leveraged containers for project isolation and local-cloud parity. For everyone else, it’s straighforward and has advantages over development directly on a developer host machine.

If you’re interested in starting a Ruby on Rails app from scratch, install Workshop and select the Ruby Rails Template. The create-Template process in Workshop will automate everything outlined in this guide.

If, on the other hand, you want to modify an existing project to use Noop, the guide here can be used as reference.

The full source code constructed for this guide, including the Noop configuration, is available in the Template repository.

Setup

Noop dependencies

Make sure you have Workshop installed locally or a Github account connected to Noop Cloud. The remaining setup is identical in both.

Project setup

The Rails Template code is based on the examples created in the Turbo Rails Tutorial. For a complete understanding of the project setup, refer to that guide.

Template Configuration

After you have the Rails project set up, you need to add the Noop Template configuration, called a Blueprint, to create a Workshop or Cloud Deployment.

Create a .noop directory in your project root.

mkdir .noop

Next create a blueprint.yaml file to define the application setup.

The content below is the necessary config for our app:

---
components: 
  - name: RailsApp
    type: service
    image: ruby:3.2.2-slim
    port: 3000
    build: 
      steps: 
        - directory: /rails
        - run: apt-get update
        - run: apt-get install --no-install-recommends -y build-essential curl git libpq-dev libvips node-gyp pkg-config python-is-python3 postgresql-client
        - run: curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/
        - run: /tmp/node-build-master/bin/node-build 20.10.0 /usr/local/node
        - run: /usr/local/node/bin/npm install -g yarn@1.22.19
        - run: rm -rf /tmp/node-build-master
        - copy: [Gemfile, Gemfile.lock]
          destination: ./
        - run: bundle install
        - run: rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git
        - run: bundle exec bootsnap precompile --gemfile
        - copy: [package.json, yarn.lock]
          destination: ./
        - run: yarn install --frozen-lockfile
        - copy: .
          destination: ./
        - run: bundle exec bootsnap precompile app/ lib/
        - run: SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
    runtime: 
      entrypoint: "/rails/bin/docker-entrypoint"
      command: "./bin/rails server -b 0.0.0.0"
      resources:
        - PgDatabase
        - RecordCache
      variables:
        DATABASE_URL:
          $resources: PgDatabase.url
        REDIS_URL:
          $resources: RecordCache.url
        SERVER_ADDR: "noop-rails.local.noop.app"

  - name: Migrate
    type: task
    image: ruby:3.2.2-slim
    build:
      steps:
        - directory: /rails
        - run: apt-get update
        - run: apt-get install --no-install-recommends -y build-essential curl git libpq-dev libvips node-gyp pkg-config python-is-python3 postgresql-client
        - copy: [Gemfile, Gemfile.lock]
          destination: ./
        - run: bundle install
        - run: rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git
        - run: bundle exec bootsnap precompile --gemfile
        - copy: .
          destination: ./
    runtime:
      command: "./bin/rails db:migrate"
      resources:
        - PgDatabase
      variables:
        DATABASE_URL:
          $resources: PgDatabase.url
lifecycles:
  - event: BeforeServices
    components:
    - Migrate
routes:
  - target:
      component: RailsApp
resources:
  - name: PgDatabase
    type: postgresql
  - name: RecordCache
    type: redis

Launch on Noop

There are a few slight differences between launching on Workshop and Cloud. Details for each are outlined below.

Workshop Setup

Select your Application, which should have been automatically detected by Workshop, or select the appropriate project directory (one that includes a .noop directory).

Workshop will automatically create a default Environment, called Main with all Resources launched.

Once done, you should see your application running with an associated endpoint.

Cloud Setup

In order to automate most of the Application setup in Noop Cloud, including Environment creation, database provisioning, building, deploying, etc., we’re going to create a Runbook. Create a file called quickstart.yml in the .noop/runbooks directory.

Here is the Runbook content:

name: Quickstart Setup

description: Demo full stack setup of this Django app with internet endpoint

workflow:
  inputs:
    - name: EnvironmentName
      description: What should we name your new environment?
      type: string
      required: true
      default: Quickstart Demo

    - name: Cluster
      type: Cluster
      description: Which cluster should the environment launch on?
      required: true

  timeout: 300

  steps:
    - name: Environment
      action: EnvironmentCreate
      params:
        name:
          $inputs: EnvironmentName
        production: false
        appId:
          $runbook: Application.id
        clusterId:
          $inputs: Cluster.id

    - name: Build
      action: BuildExecute
      params:
        sourceCodeId:
          $runbook: SourceCode.id
        appId:
          $runbook: Application.id

    - name: Resources
      action: ResourceLaunch
      params:
        envId:
          $steps: Environment.id
        sourceCodeId:
          $runbook: SourceCode.id

    - name: Deploy
      action: DeploymentExecute
      params:
        envId:
          $steps: Environment.id
        buildId:
          $steps: Build.id

    - name: Endpoint
      action: InternetEndpointRandom
      params:
        orgId:
          $runbook: Organization.id
        routes:
          - name: 'Demo Environment'
            target:
              environments:
                - $steps: Environment.id

Make sure the above configuration is pushed to the linked source repository.

In Cloud, once the Application is created you will be asked if you want to execute a Runbook. The name of the Runbook for this Template is “Quick Start”.

Choose the default cluster and run it. Behind the scenes this Runbook is:

  • Creating an Environment
  • Launching the database
  • Building the source code
  • Deploying the build
  • And finally, creating a public-facing endpoint to serve the application from

Once complete, visit the Application page to get the generated endpoint. Your Rails app is now running at that URL.

The Noop Template Blueprint contains all the details about the application. Including build process, required resources, application routing rules, runtime configuration, etc.


This guide was written with:

SoftwareVersion
Noopv2.0.110
Rubyv3.2.2
Railsv7.1.2