Skip to content

Commit

Permalink
Customize rails application (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketnova authored May 23, 2024
1 parent 0c04e00 commit 2c8c47f
Show file tree
Hide file tree
Showing 143 changed files with 6,459 additions and 156 deletions.
37 changes: 37 additions & 0 deletions .github/ISSUE_TEMPLATE/bug.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Bug report
description: File a bug report
labels: ["bug", "triage"]
projects: ["navapbc/4"]
body:
- type: markdown
attributes:
value: |
Thanks for taking the time to fill out this bug report!
- type: textarea
attributes:
label: What happened?
description: Also tell us, what did you expect to happen?
placeholder: Tell us what you see!
validations:
required: true
- type: textarea
attributes:
label: Steps to reproduce
description: Share the steps or a link to a repository we can use to reproduce the problem.
- type: dropdown
id: browsers
attributes:
label: What browsers are you seeing the problem on?
multiple: true
options:
- Not applicable
- Firefox
- Chrome
- Safari
- Microsoft Edge
- type: textarea
id: logs
attributes:
label: Relevant log output
description: Please copy and paste any relevant log output. This will be automatically formatted into code, so no need for backticks.
render: shell
32 changes: 32 additions & 0 deletions .github/ISSUE_TEMPLATE/feature.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Feature request
description: Suggest an idea for this project
labels: ["triage", "feature"]
projects: ["navapbc/4"]
body:
- type: textarea
attributes:
label: Describe the problem and the solution you'd like
description: A clear and concise description of what the problem is and what you want to happen.
value: |
**Is your feature request related to a problem? Please describe.**
<!-- A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] -->
**Describe the solution you'd like**
<!-- A clear and concise description of what you want to happen. -->
**Describe alternatives you've considered**
<!-- A clear and concise description of any alternative solutions or features you've considered. -->
- type: textarea
attributes:
label: Additional context
description: Add any other context or screenshots about the feature request here.
- type: dropdown
attributes:
label: Priority
description: How impactful would this be for your project?
multiple: false
options:
- "My project needs this now"
- "I anticipate needing this soon"
- "Nice to have"
- "Just want to discuss"
9 changes: 9 additions & 0 deletions .github/ISSUE_TEMPLATE/task.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: Task
description: Create a task for the team
projects: ["navapbc/4"]
body:
- type: textarea
attributes:
label: What's the task?
validations:
required: true
46 changes: 46 additions & 0 deletions .github/workflows/ci-app-rails.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI app-rails

on:
push:
branches:
- main
paths:
- app-rails/**
- .github/workflows/ci-app-rails.yml
pull_request:
paths:
- app-rails/**
- .github/workflows/ci-app-rails.yml

defaults:
run:
working-directory: ./app-rails

jobs:
# As an enhancement, it is possible to share the built docker image and share
# it across jobs as described in:
# https://docs.github.com/en/actions/using-workflows/storing-workflow-data-as-artifacts#passing-data-between-jobs-in-a-workflow
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
# https://github.com/docker/compose/issues/1973
- name: Create required env files
working-directory: ./
run: |
touch ./app-rails/.env
- run: make lint

test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- run: make init-container
- run: make precompile-assets

- name: Start tests
run: make test
74 changes: 72 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,72 @@
# template-application-rails
Ruby on Rails with USWDS template, including CI/CD, for teams building web applications
# Template Ruby on Rails application

This is a template repository for a Ruby on Rails application.

See [`navapbc/platform`](https://github.com/navapbc/platform) for other template repos.

## Features

- [U.S. Web Design System](https://designsystem.digital.gov/) for themeable styling and a set of common components
- Integration with AWS services, including:
- Database integration with AWS RDS Postgresql using UUIDs
- Active Storage configuration with AWS S3
- Action Mailer configuration with AWS SES
- Authentication with [devise](https://github.com/heartcombo/devise) and AWS Cognito
- Internationalization (i18n)
- Authorization using [pundit](https://github.com/varvet/pundit)
- Linting and code formatting using [rubocop](https://rubocop.org/)
- Testing using [rspec](https://rspec.info)

<img width="1023" alt="CleanShot 2024-05-22 at 16 35 53@2x" src="https://github.com/navapbc/template-application-rails/assets/67701/fb291a98-7dfa-429e-91e2-30beacf58b9e">

## Repo structure

```text
├── .github # GitHub workflows and repo templates
├── docs # Project docs and decision records
├── app-rails # Web application
├── template-only-bin # Scripts for managing this template; not copied into your project
├── template-only-docs # Documentation for this template; not copied into your project
```

## Installation

To get started using the template application on your project:

1. Run the [download and install script](./template-only-bin/download-and-install-template.sh) in your project's root directory.

```bash
curl https://raw.githubusercontent.com/navapbc/template-application-rails/main/template-only-bin/download-and-install-template.sh | bash -s
```

This script will:

1. Clone the template repository
2. Copy the template files into your project directory
3. Remove any files specific to the template repository, like this README.

You can optionally pass in a branch, commit hash, or release that you want to install. For example:

```bash
curl https://raw.githubusercontent.com/navapbc/template-application-rails/main/template-only-bin/download-and-install-template.sh | bash -s -- <commit_hash>
```
2. [Follow the steps in `app-rails/README.md`](./app-rails/README.md) to set up the application locally.
3. Optional, if using the Platform infrastructure template: [Follow the steps in the `template-infra` README](https://github.com/navapbc/template-infra#installation) to set up the various pieces of your infrastructure.

## Updates

If you have previously installed this template and would like to update your project to use a newer version of this application:

1. Run the [download and install script](./template-only-bin/download-and-install-template.sh) in your project's root directory and pass in the branch, commit hash, or release that you want to update to, followed by the name of your application directory (e.g. `app-rails`).
```bash
curl https://raw.githubusercontent.com/navapbc/template-application-rails/main/template-only-bin/download-and-install-template.sh | bash -s -- <commit_hash> <app_name>
```
This script will:
1. Clone the template repository
2. Copy the template files into your project directory
3. Remove any files specific to the template repository, like this README.
⚠️ Warning! This will modify existing files. Review all changes carefully after executing the script by running `git diff`.
15 changes: 15 additions & 0 deletions app-rails/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
# Ignore bundler config.
/.bundle

# Ignore installed gems.
/vendor/bundle/*
!/vendor/bundle/.keep

# Ignore all environment files (except templates).
/.env*
/*.env*
!/.env*.erb

# Ignore all logfiles and tempfiles.
Expand All @@ -33,6 +38,16 @@

# Ignore master key for decrypting credentials and more.
/config/master.key
/config/credentials/*

# Ignore development log.
/log/development.log

/app/assets/builds/*
!/app/assets/builds/.keep

/node_modules/*
!/node_modules/.keep

# Testing
coverage/*
1 change: 1 addition & 0 deletions app-rails/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
8 changes: 8 additions & 0 deletions app-rails/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require:
- rubocop-rspec
inherit_gem:
pundit: config/rubocop-rspec.yml
rubocop-rails-omakase: rubocop.yml
AllCops:
Exclude:
- lib/templates/**/*
94 changes: 74 additions & 20 deletions app-rails/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.3.0


##########################################################################################
# BASE: Shared base docker image
##########################################################################################
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base

# Rails app lives here
Expand All @@ -10,53 +15,102 @@ WORKDIR /rails
# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle" \
BUNDLE_WITHOUT="development"
BUNDLE_PATH="/usr/local/bundle"

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000


# Throw-away build stage to reduce size of final image
##########################################################################################
# BUILD: Throw-away build stage
##########################################################################################
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 libpq-dev libvips pkg-config
apt-get install --no-install-recommends -y build-essential git libpq-dev libvips pkg-config npm

# Install npm packages
COPY package.json package-lock.json ./

# Install npm packages
RUN npm install


##########################################################################################
# DEV: Used for development and test
##########################################################################################
FROM build as dev

ENV RAILS_ENV="development"

# Install packages needed for development
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y postgresql-client graphviz && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Install application gems for development
COPY Gemfile Gemfile.lock ./
RUN bundle config set --local without production && \
bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git

# Install application gems
# Copy application code
COPY . .

CMD ["./bin/dev"]


##########################################################################################
# RELEASE-BUILD: Throw-away build stage for RELEASE
##########################################################################################
FROM build as release-build

# Install application gems for production
COPY Gemfile Gemfile.lock ./
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
bundle exec bootsnap precompile --gemfile
RUN bundle config set --local without development test && \
bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git

# Copy application code
COPY . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/
RUN bundle exec bootsnap precompile --gemfile app/ lib/

# 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
##########################################################################################
# RELEASE: Used for production
##########################################################################################
FROM base as release

# Set production environment
ENV RAILS_ENV="production" \
BUNDLE_DEPLOYMENT="1" \
BUNDLE_PATH="/usr/local/bundle"

# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libvips postgresql-client && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives
apt-get install -y --no-install-recommends unzip python3-venv python-is-python3 curl libvips postgresql-client && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives && \
curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip" && \
unzip awscli-bundle.zip && \
./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws && \
rm -rf ./awscli-bundle awscli-bundle.zip

# Install custom db migrate script
COPY bin/db-migrate /usr/bin/

# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails
COPY --from=release-build /usr/local/bundle /usr/local/bundle
COPY --from=release-build /rails /rails

# 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 storage tmp
USER rails:rails

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

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "server"]
Loading

0 comments on commit 2c8c47f

Please sign in to comment.