From 4ae6e5137293aef29b0da0255082f4aad47bb3f6 Mon Sep 17 00:00:00 2001 From: Mauricio Fierro Date: Thu, 7 Dec 2023 16:11:30 -0500 Subject: [PATCH 1/8] Add dockerfile-rails --- Gemfile | 2 +- Gemfile.lock | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 78fd709..4c80629 100644 --- a/Gemfile +++ b/Gemfile @@ -73,6 +73,7 @@ group :development do gem "flog", require: false gem "guard-rspec", require: false gem "guard-minitest", "~> 2.4", require: false + gem "dockerfile-rails", ">= 1.5", require: false end group :test do @@ -87,4 +88,3 @@ end gem "devise", "~> 4.9" - diff --git a/Gemfile.lock b/Gemfile.lock index df69564..d12319c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -98,6 +98,8 @@ GEM responders warden (~> 1.2.3) diff-lcs (1.5.0) + dockerfile-rails (1.5.12) + rails (>= 3.0.0) erubi (1.12.0) factory_bot (6.4.0) activesupport (>= 5.0.0) @@ -187,6 +189,8 @@ GEM net-smtp (0.3.3) net-protocol nio4r (2.5.9) + nokogiri (1.15.4-aarch64-linux) + racc (~> 1.4) nokogiri (1.15.4-arm64-darwin) racc (~> 1.4) nokogiri (1.15.4-x86_64-linux) @@ -200,6 +204,7 @@ GEM ast (~> 2.4.1) racc path_expander (1.1.1) + pg (1.5.4) pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) @@ -302,6 +307,11 @@ GEM rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2, < 3.0) websocket (~> 1.0) + sentry-rails (5.15.0) + railties (>= 5.0) + sentry-ruby (~> 5.15.0) + sentry-ruby (5.15.0) + concurrent-ruby (~> 1.0, >= 1.0.2) sexp_processor (4.17.0) shellany (0.0.1) shoulda-matchers (5.3.0) @@ -313,6 +323,7 @@ GEM actionpack (>= 5.2) activesupport (>= 5.2) sprockets (>= 3.0.0) + sqlite3 (1.6.4-aarch64-linux) sqlite3 (1.6.4-arm64-darwin) sqlite3 (1.6.4-x86_64-linux) stimulus-rails (1.2.2) @@ -347,6 +358,7 @@ GEM zeitwerk (2.6.11) PLATFORMS + aarch64-linux arm64-darwin-22 x86_64-linux @@ -355,6 +367,7 @@ DEPENDENCIES capybara debug devise (~> 4.9) + dockerfile-rails (>= 1.5) factory_bot_rails (~> 6.4) faker (~> 3.2.2) flog @@ -363,6 +376,7 @@ DEPENDENCIES importmap-rails jbuilder kaminari + pg (~> 1.5) puma (~> 5.0) rails (~> 7.0.7, >= 7.0.7.2) redis (~> 4.0) @@ -370,6 +384,8 @@ DEPENDENCIES rspec-rails (~> 6.0.0) rubocop selenium-webdriver + sentry-rails (~> 5.15) + sentry-ruby (~> 5.15) shoulda-matchers (~> 5.0) sprockets-rails sqlite3 (~> 1.4) From 463092984e58f62d3c34875cdb0f979f1014e75a Mon Sep 17 00:00:00 2001 From: Mauricio Fierro Date: Thu, 7 Dec 2023 16:11:52 -0500 Subject: [PATCH 2/8] Add Dockerfile and configuration --- .dockerignore | 37 ++++++++++++++++++++++ Dockerfile | 73 +++++++++++++++++++++++++++++++++++++++++++ Gemfile | 1 - Gemfile.lock | 9 ------ bin/docker-entrypoint | 8 +++++ config/dockerfile.yml | 11 +++++++ 6 files changed, 129 insertions(+), 10 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100755 bin/docker-entrypoint create mode 100644 config/dockerfile.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ca0f731 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,37 @@ +# See https://docs.docker.com/engine/reference/builder/#dockerignore-file for more about ignoring files. + +# Ignore git directory. +/.git/ + +# Ignore bundler config. +/.bundle + +# Ignore all default key files. +/config/master.key +/config/credentials/*.key + +# Ignore all environment files. +/.env* +!/.env.example + +# Ignore all logfiles and tempfiles. +/log/* +/tmp/* +!/log/.keep +!/tmp/.keep + +# Ignore pidfiles, but keep the directory. +/tmp/pids/* +!/tmp/pids/.keep + +# Ignore storage (uploaded files in development and any SQLite databases). +/storage/* +!/storage/.keep +/tmp/storage/* +!/tmp/storage/.keep + +# Ignore assets. +/node_modules/ +/app/assets/builds/* +!/app/assets/builds/.keep +/public/assets diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f168dd1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,73 @@ +# syntax = docker/dockerfile:1 + +# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile +ARG RUBY_VERSION=3.2.2 +FROM ruby:$RUBY_VERSION-slim as base + +# Rails app lives here +WORKDIR /rails + +# Set production environment +ENV RAILS_ENV="production" \ + BUNDLE_WITHOUT="development:test" \ + BUNDLE_DEPLOYMENT="1" + +# Update gems and bundler +RUN gem update --system --no-document && \ + gem install -N bundler + + +# Throw-away build stage to reduce size of final image +FROM base as build + +# Install packages needed to build gems +RUN apt-get update -qq && \ + apt-get install --no-install-recommends -y build-essential pkg-config + +# Install application gems +COPY --link Gemfile Gemfile.lock ./ +RUN bundle install && \ + bundle exec bootsnap precompile --gemfile && \ + rm -rf ~/.bundle/ $BUNDLE_PATH/ruby/*/cache $BUNDLE_PATH/ruby/*/bundler/gems/*/.git + +# Copy application code +COPY --link . . + +# Precompile bootsnap code for faster boot times +RUN bundle exec bootsnap precompile app/ lib/ + +# Precompiling assets for production without requiring secret RAILS_MASTER_KEY +RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile + + +# Final stage for app image +FROM base + +# Install packages needed for deployment +RUN apt-get update -qq && \ + apt-get install --no-install-recommends -y curl libsqlite3-0 && \ + rm -rf /var/lib/apt/lists /var/cache/apt/archives + +# Copy built artifacts: gems, application +COPY --from=build /usr/local/bundle /usr/local/bundle +COPY --from=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 && \ + mkdir /data && \ + chown -R rails:rails db log storage tmp /data +USER rails:rails + +# Deployment options +ENV DATABASE_URL="sqlite3:///data/production.sqlite3" \ + RAILS_LOG_TO_STDOUT="1" \ + RAILS_SERVE_STATIC_FILES="true" \ + RUBY_YJIT_ENABLE="1" + +# Entrypoint prepares the database. +ENTRYPOINT ["/rails/bin/docker-entrypoint"] + +# Start the server by default, this can be overwritten at runtime +EXPOSE 3000 +VOLUME /data +CMD ["./bin/rails", "server"] diff --git a/Gemfile b/Gemfile index 4c80629..d5df27b 100644 --- a/Gemfile +++ b/Gemfile @@ -86,5 +86,4 @@ group :test do gem "shoulda-matchers", "~> 5.0" end - gem "devise", "~> 4.9" diff --git a/Gemfile.lock b/Gemfile.lock index d12319c..1410e50 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -204,7 +204,6 @@ GEM ast (~> 2.4.1) racc path_expander (1.1.1) - pg (1.5.4) pry (0.14.2) coderay (~> 1.1) method_source (~> 1.0) @@ -307,11 +306,6 @@ GEM rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2, < 3.0) websocket (~> 1.0) - sentry-rails (5.15.0) - railties (>= 5.0) - sentry-ruby (~> 5.15.0) - sentry-ruby (5.15.0) - concurrent-ruby (~> 1.0, >= 1.0.2) sexp_processor (4.17.0) shellany (0.0.1) shoulda-matchers (5.3.0) @@ -376,7 +370,6 @@ DEPENDENCIES importmap-rails jbuilder kaminari - pg (~> 1.5) puma (~> 5.0) rails (~> 7.0.7, >= 7.0.7.2) redis (~> 4.0) @@ -384,8 +377,6 @@ DEPENDENCIES rspec-rails (~> 6.0.0) rubocop selenium-webdriver - sentry-rails (~> 5.15) - sentry-ruby (~> 5.15) shoulda-matchers (~> 5.0) sprockets-rails sqlite3 (~> 1.4) diff --git a/bin/docker-entrypoint b/bin/docker-entrypoint new file mode 100755 index 0000000..dffd4ba --- /dev/null +++ b/bin/docker-entrypoint @@ -0,0 +1,8 @@ +#!/bin/bash -e + +# If running the rails server then create or migrate existing database +if [ "${*}" == "./bin/rails server" ]; then + ./bin/rails db:prepare +fi + +exec "${@}" diff --git a/config/dockerfile.yml b/config/dockerfile.yml new file mode 100644 index 0000000..1007df7 --- /dev/null +++ b/config/dockerfile.yml @@ -0,0 +1,11 @@ +# generated by dockerfile-rails + +--- +options: + label: + fly_launch_runtime: rails + postgresql: true + prepare: false + sqlite3: true + sentry: true + yjit: true From 296aa7b40b772d61d20abc212a97ac2048f35b80 Mon Sep 17 00:00:00 2001 From: Mauricio Fierro Date: Thu, 7 Dec 2023 16:12:19 -0500 Subject: [PATCH 3/8] Add fly configuration --- fly.toml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 fly.toml diff --git a/fly.toml b/fly.toml new file mode 100644 index 0000000..65d3f6c --- /dev/null +++ b/fly.toml @@ -0,0 +1,33 @@ +# fly.toml app configuration file generated for along on 2023-12-08T10:50:18-05:00 +# +# See https://fly.io/docs/reference/configuration/ for information about how to use this file. +# + +app = "along" +primary_region = "mia" +swap_size_mb = 1024 +console_command = "/rails/bin/rails console" + +[build] + dockerfile = "Dockerfile" + +[env] + DATABASE_URL = "sqlite3:///data/production.sqlite3" + +[[mounts]] + source = "data" + destination = "/data" + auto_extend_size_threshold = 0 + +[http_service] + internal_port = 3000 + force_https = true + auto_stop_machines = true + auto_start_machines = true + min_machines_running = 0 + processes = ["app"] + +[[vm]] + cpu_kind = "shared" + cpus = 1 + memory_mb = 256 From ea5323636f65ef0dd7af3b9b214f0e5e6ad3c145 Mon Sep 17 00:00:00 2001 From: Mauricio Fierro Date: Fri, 8 Dec 2023 10:27:48 -0500 Subject: [PATCH 4/8] Setup email via Sendgrid - Add sendgrid_api_key to credentials - Configure action_mailer smtp settings - Configure default from --- app/mailers/application_mailer.rb | 2 +- config/credentials.yml.enc | 2 +- config/environments/production.rb | 11 +++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 3c34c81..2ad30cc 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,4 +1,4 @@ class ApplicationMailer < ActionMailer::Base - default from: "from@example.com" + default from: "along@mauriciofierro.dev" layout "mailer" end diff --git a/config/credentials.yml.enc b/config/credentials.yml.enc index fef1ee2..bccfdf7 100644 --- a/config/credentials.yml.enc +++ b/config/credentials.yml.enc @@ -1 +1 @@ -GOC1Lj07cHH4WjwXaJ07M+YxXFjTBBrDuitMit6p3bvW8PbKc91aOVEqh6X3728ZOO2JQ5Mx0WptJCkJCsquW/Ug/0I14YT7uHPKfgozTjtqmMFbyIQn0jp36bxHcpvaVJSaUvitbsaizUWduIlutPImzo0N2eZrIBZissbdCoadbUvbaescUEvZrfqg6LxXi0ZqX8HhkXMIPvLIAJhlmECrBSC2n3bM4RTY+1VyKAu3+O7pnfuEw0GFeptrwJ46I2tDldvJKZgtupjkzYrQ4kVnnpSbfUM8a7BJQKG6KblbtipUo/7pyKdbn5bNoxY9z1vLqFacz+DqreqMmSUO1110R5N3dpZYVPUCsc6sBDu5u3ex4A71IGAl5TPT1eqiOZl8ooStGfqXr30pgYMHOjUcj07xtfK0wDEJ--cnvAMQVqO0RplpHk--pDV/e55w3yPjxTjKLQh/8g== \ No newline at end of file +amhyRa5LGqZ3C+qu5WasK3LhzFwVtFyZn7Bt9LQlkiHFn6/YNMiau0svHTbx7JrxSCX0d5pb27wvt/bzYeo34DvtQSDC2gLG3o/+XChVRpAH1HWqhA12w7PPOfWFpdb06ALoTpoqpOPwWPEMFGHd0MR2yWxrjZ28Cb7M2xykS2TIOyjRxGpZ3OLsaq+WC9kQHo/UNB9LoKdWe8IRVbD/MgHnjjQ5AcvuGH/hw2t9woXJG9P3UYEzz37ViaoRxIj7HkdO2fd+FKHXQcpNzNZLQPeZSvMp7B4c6t/e7351WuDFB303G3ZQ/MlfoT/aDXV2tK3gNDE9EnneQJk/tGtTTEApHb9CjI4ulyWqlJ3JfivcM0mlEeUXTYaFw6wLL6yOr0+DZOYLf7d9773uZXk1qBLBGRVsY19ErRvYK46662HQkvUY7YUwdCkyc4zGmf9DzsitBXWct7V5qs8f7c6HrOmHy0NkTCJt88/2WxM/hyhlIHSjMDIPuHKxs3ZfCkNA56CqkxeLE35CbFQk+A==--zYgqCm4mQtsocYj3--Anx2lttZVTqPfO1BzH23wA== \ No newline at end of file diff --git a/config/environments/production.rb b/config/environments/production.rb index 94109d6..0e42107 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -63,6 +63,17 @@ # config.active_job.queue_name_prefix = "along_production" config.action_mailer.perform_caching = false + config.action_mailer.delivery_method = :smtp + + config.action_mailer.smtp_settings = { + :user_name => 'apikey', # This is the string literal 'apikey', NOT the ID of your API key + :password => Rails.application.credentials.sendgrid_api_key, # This is the secret sendgrid API key which was issued during API key creation + :domain => "mauriciofierro.dev", + :address => "smtp.sendgrid.net", + :port => 587, + :authentication => :plain, + :enable_starttls_auto => true + } # Ignore bad email addresses and do not raise email delivery errors. # Set this to true and configure the email server for immediate delivery to raise delivery errors. From 25408e4c6ed4cd279ed4819a74e0d0ebc56e2898 Mon Sep 17 00:00:00 2001 From: Mauricio Fierro Date: Fri, 8 Dec 2023 10:58:20 -0500 Subject: [PATCH 5/8] Set secret_key_base for proudction --- config/environments/production.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/environments/production.rb b/config/environments/production.rb index 0e42107..75b0dfc 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -3,6 +3,7 @@ Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. + config.secret_key_base = ENV["SECRET_KEY_BASE"] # Code is not reloaded between requests. config.cache_classes = true From 2314d8a4c9ddf8970cae3758bfa5246c95db3827 Mon Sep 17 00:00:00 2001 From: Mauricio Fierro Date: Fri, 8 Dec 2023 11:11:35 -0500 Subject: [PATCH 6/8] Setting production database To point to the volume. It doesn't seem to be using the DATABASE_URL variable --- config/database.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/database.yml b/config/database.yml index fcba57f..b080c1c 100644 --- a/config/database.yml +++ b/config/database.yml @@ -22,4 +22,4 @@ test: production: <<: *default - database: db/production.sqlite3 + database: /data/production.sqlite3 From 82fd13509011193e4463c122a919e7b5653c6415 Mon Sep 17 00:00:00 2001 From: Mauricio Fierro Date: Fri, 8 Dec 2023 12:06:20 -0500 Subject: [PATCH 7/8] Add Deploy workflow - Extract tests from the CI workflow into a reusable workflow and call it from the original - Add a deploy workflow to automatically deploy to Fly.io - Call the reusable test workflow from the deploy workflow --- .github/workflows/ci.yml | 28 +++------------------------- .github/workflows/deploy.yml | 17 +++++++++++++++++ .github/workflows/test.yml | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 25 deletions(-) create mode 100644 .github/workflows/deploy.yml create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 31b21fa..19c7d28 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,27 +1,5 @@ name: CI -on: [push, pull_request] +on: pull_request jobs: - test: - runs-on: ubuntu-latest - services: - redis: - image: redis - ports: ['6379:6379'] - options: --entrypoint redis-server - steps: - - uses: actions/checkout@v4 - - name: Setup Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: 3.2.2 - - name: Build and run tests - env: - REDIS_URL: redis://localhost:6379/0 - RAILS_ENV: test - run: | - sudo apt-get -yqq install libpq-dev - gem install bundler - bundle install --jobs 4 --retry 3 - bundle exec rails db:prepare - bundle exec rails test:all - bundle exec rspec + call-test: + uses: ./.github/workflows/test.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..13110c9 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,17 @@ +name: Deploy +on: + push: + branches: + - main +jobs: + call-test: + uses: ./.github/workflows/test.yml + deploy: + name: Deploy app + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: superfly/flyctl-actions/setup-flyctl@master + - run: flyctl deploy --remote-only + env: + FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..89a5179 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,26 @@ +on: workflow_call +jobs: + test: + runs-on: ubuntu-latest + services: + redis: + image: redis + ports: ['6379:6379'] + options: --entrypoint redis-server + steps: + - uses: actions/checkout@v4 + - name: Setup Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: 3.2.2 + - name: Build and run tests + env: + REDIS_URL: redis://localhost:6379/0 + RAILS_ENV: test + run: | + sudo apt-get -yqq install libpq-dev + gem install bundler + bundle install --jobs 4 --retry 3 + bundle exec rails db:prepare + bundle exec rails test:all + bundle exec rspec From 8bc68b97a30a4c38edb30a31e4e13c11c43c2470 Mon Sep 17 00:00:00 2001 From: Mauricio Fierro Date: Fri, 8 Dec 2023 12:14:17 -0500 Subject: [PATCH 8/8] Change to test deployment --- app/views/home/index.html.erb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index e438698..31c9b2e 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -1,5 +1,8 @@

Welcome to Along!

+

+ A web application to make it easier to practice along to YouTube music instrument video lessons (where enabled). +

<% if user_signed_in? %> <%= link_to "Lessons", lessons_path %> <% else %>