diff --git a/.gitignore b/.gitignore index aed606581..2e9d89df2 100644 --- a/.gitignore +++ b/.gitignore @@ -102,4 +102,5 @@ certbot.log rerun.txt # Ignore VCR -features/support/fixtures/cassettes/** \ No newline at end of file +features/support/fixtures/cassettes/** +/config/master.key diff --git a/.rubocop.yml b/.rubocop.yml index b38447021..7b2c3b332 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -5,7 +5,7 @@ AllCops: - 'node_modules/**/*' - 'server/**/*' - 'vendor/**/*' - TargetRubyVersion: 3.0.4 + TargetRubyVersion: 3.0.5 NewCops: enable Gemspec/DateAssignment: # (new in 1.10) diff --git a/.ruby-version b/.ruby-version index 79bc2c7a9..316881c9f 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -ruby-3.0.4 +ruby-3.0.5 diff --git a/.semaphore/semaphore.yml b/.semaphore/semaphore.yml index 79f9bc96b..3ff7fc6d3 100644 --- a/.semaphore/semaphore.yml +++ b/.semaphore/semaphore.yml @@ -3,7 +3,7 @@ name: WebsiteOne - CI agent: machine: type: e1-standard-2 - os_image: ubuntu1804 + os_image: ubuntu2004 blocks: - name: Test task: @@ -26,11 +26,12 @@ blocks: commands: - checkout - sem-service start postgres 13 - - sem-version ruby 3.0.4 + - sem-version ruby 3.0.5 - sudo -u postgres createuser -s semaphore - createdb -U postgres -h 0.0.0.0 websiteone_test - cache restore - - bundle install --path vendor/bundle + - bundle config set --local path 'vendor/bundle' + - bundle install - mkdir -p tmp/pids - npm install bower - npm install yarn diff --git a/Dockerfile b/Dockerfile index 647dfa5f4..d7138be59 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,21 @@ -FROM ruby:3.0.4 as base +# Use the official Ruby image from Docker Hub +# https://hub.docker.com/_/ruby -RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - -RUN apt-get update -qq && apt-get install -y build-essential \ - libpq-dev nodejs \ - libqt5webkit5-dev dos2unix \ - gstreamer1.0-plugins-base gstreamer1.0-tools gstreamer1.0-x +# [START cloudrun_rails_base_image] +# Pinning the OS to buster because the nodejs install script is buster-specific. +# Be sure to update the nodejs install command if the base image OS is updated. +FROM ruby:3.0-buster as base +# [END cloudrun_rails_base_image] + +RUN (curl -sS https://deb.nodesource.com/gpgkey/nodesource.gpg.key | gpg --dearmor | apt-key add -) && \ + echo "deb https://deb.nodesource.com/node_14.x buster main" > /etc/apt/sources.list.d/nodesource.list && \ + apt-get update && apt-get install -y nodejs lsb-release + +RUN (curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -) && \ + echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \ + apt-get update && apt-get install -y yarn + +RUN apt-get update -qq && apt-get install -y dos2unix postgresql-client RUN mkdir /WebsiteOne WORKDIR /WebsiteOne @@ -12,19 +23,11 @@ WORKDIR /WebsiteOne COPY Gemfile /WebsiteOne/Gemfile COPY Gemfile.lock /WebsiteOne/Gemfile.lock -COPY ./docker-entrypoint.sh / -RUN chmod +x /docker-entrypoint.sh -ENTRYPOINT ["/docker-entrypoint.sh"] - -#OPENSSL_CONF is set to /dev/null since not able to determine how to -#set it "correctly" for now. perhaps replace phantomjs with something else? -ENV BUNDLE_PATH=/bundle \ - BUNDLE_BIN=/bundle/bin \ - GEM_HOME=/bundle -# OPENSSL_CONF=/dev/null -ENV PATH="${BUNDLE_BIN}:${PATH}" - -RUN bundle install +#Production or staging, use middle 2 config lines below when bundling +RUN gem install bundler && \ +# bundle config set --local deployment 'true' && \ +# bundle config set --local without 'development test' && \ + bundle install COPY package.json /WebsiteOne/package.json COPY scripts /WebsiteOne/scripts @@ -32,10 +35,32 @@ COPY vendor/assets/javascripts /WebsiteOne/assets/javascripts FROM base -RUN dos2unix scripts/copy_javascript_dependencies.js -RUN npm install -g yarn +# To execute tests, install chrome below RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \ && echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list RUN apt-get update && apt-get -y install google-chrome-stable + +RUN dos2unix scripts/copy_javascript_dependencies.js RUN yarn install COPY . /WebsiteOne +RUN bundle exec rake assets:precompile + +#Production or staging, take out 'bundle' line above and use the following +# ENV RAILS_ENV=production +# ENV RAILS_SERVE_STATIC_FILES=true +# # Redirect Rails log to STDOUT for Cloud Run to capture +# ENV RAILS_LOG_TO_STDOUT=true +# # [START cloudrun_rails_dockerfile_key] +# ARG MASTER_KEY +# ENV RAILS_MASTER_KEY=${MASTER_KEY} +# # [END cloudrun_rails_dockerfile_key] + +# # pre-compile Rails assets with master key +# RUN bundle exec rake assets:precompile +# EXPOSE 8080 +# CMD ["bin/rails", "server", "-b", "0.0.0.0", "-p", "8080"] + +# Also add lines below to database.yml under 'production:' +# username: av +# password: <%= Rails.application.credentials.gcp[:db_password] %> +# host: /cloudsql/av-wso:us-central1:postgres diff --git a/Gemfile b/Gemfile index 10fc0fd83..3bd0842f5 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,7 @@ source 'https://rubygems.org' -ruby '3.0.4' +ruby '3.0.5' # Rather than loading the entire Rails framework, we charry pick the parts we use gem 'actionmailer', '~> 6.1.7' @@ -86,6 +86,10 @@ gem 'will_paginate-bootstrap' gem 'youtube_rails' gem 'rack-timeout' +group :production do + gem 'mini_racer' # for environment without pre-existing js runtimes +end + group :test do gem 'capybara' gem 'capybara-screenshot' diff --git a/Gemfile.lock b/Gemfile.lock index a20aa729d..b8a6d28ff 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -428,6 +428,8 @@ GEM addressable (~> 2.7) letter_opener (1.7.0) launchy (~> 2.2) + libv8-node (16.10.0.0-x86_64-darwin) + libv8-node (16.10.0.0-x86_64-linux) listen (3.6.0) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) @@ -450,6 +452,8 @@ GEM mime-types-data (3.2021.0704) mini_histogram (0.3.1) mini_mime (1.1.2) + mini_racer (0.6.3) + libv8-node (~> 16.10.0.0) minitest (5.16.3) msgpack (1.6.0) multi_json (1.15.0) @@ -853,6 +857,7 @@ DEPENDENCIES local_time (~> 2.1) mercury-rails! mime-types (~> 3.3, >= 3.3.1) + mini_racer nokogiri (= 1.11.5) octokit omniauth @@ -912,7 +917,7 @@ DEPENDENCIES youtube_rails RUBY VERSION - ruby 3.0.4p208 + ruby 3.0.5p211 BUNDLED WITH 2.2.33 diff --git a/cloudbuild.yaml b/cloudbuild.yaml new file mode 100644 index 000000000..f9e02d50b --- /dev/null +++ b/cloudbuild.yaml @@ -0,0 +1,51 @@ + +# [START cloudrun_rails_cloudbuild] +steps: + - id: "build image" + name: "gcr.io/cloud-builders/docker" + entrypoint: 'bash' + args: ["-c", "docker build --build-arg MASTER_KEY=${_RAILS_KEY} -t gcr.io/${PROJECT_ID}/${_SERVICE_NAME} . "] +# secretEnv: ["_RAILS_KEY"] + + - id: "push image" + name: "gcr.io/cloud-builders/docker" + args: ["push", "gcr.io/${PROJECT_ID}/${_SERVICE_NAME}"] + + - id: "apply migrations" + name: "gcr.io/google-appengine/exec-wrapper" + entrypoint: "bash" + args: + [ + "-c", + "/buildstep/execute.sh -i gcr.io/${PROJECT_ID}/${_SERVICE_NAME} -s ${PROJECT_ID}:${_REGION}:${_INSTANCE_NAME} -e RAILS_MASTER_KEY=${_RAILS_KEY} -- bundle exec rails db:migrate" + ] + + # Use seed step below only on first deploy if you want sample data + # - id: "seed some display data" + # name: "gcr.io/google-appengine/exec-wrapper" + # entrypoint: "bash" + # args: + # [ + # "-c", + # "/buildstep/execute.sh -i gcr.io/${PROJECT_ID}/${_SERVICE_NAME} -s ${PROJECT_ID}:${_REGION}:${_INSTANCE_NAME} -e RAILS_MASTER_KEY=${_RAILS_KEY} -- bundle exec rake db:seed" + # ] + # secretEnv: ["_RAILS_KEY"] + +substitutions: + _REGION: us-central1 + _SERVICE_NAME: av-wso + _INSTANCE_NAME: postgres +# _SECRET_NAME: rails-master-key +# Do not put the key here for any official site as it will expose secrets and passwords. +# Instead, use the availableSecrets section below and put the key in google cloud secrets. + _RAILS_KEY: 79c9bca5ff743a515be994095fd41a3a + + +# availableSecrets: +# secretManager: +# - versionName: projects/${PROJECT_ID}/secrets/${_SECRET_NAME}/versions/latest +# env: RAILS_KEY + +images: + - "gcr.io/${PROJECT_ID}/${_SERVICE_NAME}" +# [END cloudrun_rails_cloudbuild] \ No newline at end of file diff --git a/config/credentials.yml.enc b/config/credentials.yml.enc new file mode 100644 index 000000000..5bd1ee991 --- /dev/null +++ b/config/credentials.yml.enc @@ -0,0 +1 @@ +xxXTi2v2HMRD6dcSX6qpaUByfveL15gulGj/KfDKWfsNwHQXPSObiQqjWgtkWw9gjUhDSK1dJbBR8FA2MtbJJPNSMiSOP7csIFAIvMw/T7dNSQas+ilhGCjYVrnW6sZl4vc+GKKea569HaAuWTuC518pzD8eQI97w9+C0o5P9IpxPXGrLTB3Hm43uEzTxQD35e9mRW0VzSjvDjJCruKx+vbPF2O81Ap3XeWF4oOd2rw9xyPzbbTDVKB8dxApGLvkGtD5jKavS8Ne/+Iny5Miwg81H6wWqAdKrWg9rnZ8k4dkbGg0/7ZU0fFSyM6/UVwr6BbU/TV7U2mTFkLgsB05S4KJgx90pIiAZbTavME9rlPFPI1B5gI2G5JHNWINcuwY7D3WqNNA7Yg2NZMIDeB3qE0w+eSdditHkIfJZI3sPDJz63NVH0EQK135V3iYEaM/sQkz--IwmpHfU6ig9OgiMI--AvQyzBX9AUqeqe6stOPuXA== \ No newline at end of file diff --git a/config/database.yml b/config/database.yml index ad958e09c..10a0f5650 100644 --- a/config/database.yml +++ b/config/database.yml @@ -6,13 +6,13 @@ development: &dev pool: 20 username: <%= ENV["DATABASE_POSTGRESQL_USERNAME"] %> password: <%= ENV["DATABASE_POSTGRESQL_PASSWORD"] %> + test: &test <<: *dev database: websiteone_test production: <<: *dev - database: websiteone_production - + database: websiteone-production cucumber: <<: *test diff --git a/config/storage.yml b/config/storage.yml index d32f76e8f..543fc6aac 100644 --- a/config/storage.yml +++ b/config/storage.yml @@ -14,13 +14,6 @@ local: # region: us-east-1 # bucket: your_own_bucket -# Remember not to checkin your GCS keyfile to a repository -# google: -# service: GCS -# project: your_project -# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> -# bucket: your_own_bucket - # Use rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) # microsoft: # service: AzureStorage diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 283d5304d..db253a9c4 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -2,6 +2,6 @@ set -e - bundle check || bundle install --binstubs="$BUNDLE_BIN" + bundle check || bundle install binstubs --all exec "$@" diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 000000000..0b48e8c00 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +cd /WebsiteOne + +# Create the Rails production DB on first run +RAILS_ENV=production bundle exec rake db:create + +# Make sure we are using the most up to date +# database schema +RAILS_ENV=production bundle exec rake db:migrate + +# Do some protective cleanup +> log/production.log +rm -f tmp/pids/server.pid + +# Run the web service on container startup +# $PORT is provided as an environment variable by Cloud Run +bundle exec rails server -e production -b 0.0.0.0 -p $PORT \ No newline at end of file