diff --git a/.dockerfiles/build.sh b/.dockerfiles/build.sh new file mode 100755 index 0000000..014ed23 --- /dev/null +++ b/.dockerfiles/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# Install dependencies. +export DEBIAN_FRONTEND=noninteractive +apt-get update +apt-get install -y \ + build-essential \ + ruby-full \ + default-libmysqlclient-dev \ + tzdata \ + nodejs +gem install bundler -v 2.3.26 +bundle install diff --git a/.dockerfiles/run-api.sh b/.dockerfiles/run-api.sh new file mode 100755 index 0000000..2ed22d2 --- /dev/null +++ b/.dockerfiles/run-api.sh @@ -0,0 +1,4 @@ +#!/bin/bash +rm tmp/pids/server.pid -rf +bundle exec rake assets:precompile +rails server \ No newline at end of file diff --git a/.dockerfiles/run-migrations.sh b/.dockerfiles/run-migrations.sh new file mode 100755 index 0000000..7d0a65c --- /dev/null +++ b/.dockerfiles/run-migrations.sh @@ -0,0 +1,2 @@ +#!/bin/bash +rake db:migrate \ No newline at end of file diff --git a/.dockerfiles/run-seed.sh b/.dockerfiles/run-seed.sh new file mode 100755 index 0000000..a2ae0e6 --- /dev/null +++ b/.dockerfiles/run-seed.sh @@ -0,0 +1,2 @@ +#!/bin/bash +rake seed -- --path=/seed \ No newline at end of file diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/.env.example b/.env.example index 04fa664..94cb892 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,18 @@ GITHUB_KEY= GITHUB_SECRET= + SECRET_KEY_BASE= -ELASTICSEARCH_URL=http://localhost:9200 \ No newline at end of file + +ELASTICSEARCH_URL=http://localhost:9200 + +DATABASE_USERNAME= +DATABASE_PASSWORD= +DATABASE_HOST=127.0.0.1 +DATABASE_PORT=3306 +DATABASE_NAME= + +DEV_DATABASE_NAME= +TEST_DATABASE_NAME= + +RAILS_LOG_TO_STDOUT= +RAILS_LOG_LEVEL=info \ No newline at end of file diff --git a/.github/workflows/build-and-publish.yml b/.github/workflows/build-and-publish.yml new file mode 100644 index 0000000..890fe4b --- /dev/null +++ b/.github/workflows/build-and-publish.yml @@ -0,0 +1,30 @@ +name: Build and Publish +on: + push: + branches: + - main + pull_request: +jobs: + build-and-push-docker-image: + name: Build Docker image and publish + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 + # - name: Set up Docker Buildx + # id: buildx + # uses: docker/setup-buildx-action@v2 + - name: Login to Github Packages + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ secrets.PUBLISHING_GITHUB_USERNAME }} + password: ${{ secrets.PUBLISHING_GITHUB_TOKEN }} + - name: Build image and push to Docker Hub and GitHub Container Registry + uses: docker/build-push-action@v2 + with: + context: ./ + tags: | + ghcr.io/madgrades/api.madgrades.com:latest + # push: true + push: ${{ github.ref == 'refs/heads/main' }} diff --git a/.gitignore b/.gitignore index 93bec22..ce3de5d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ .idea -database.yml .env public/assets @@ -16,3 +15,6 @@ public/assets /yarn-error.log .byebug_history + +# Ignore master key for decrypting credentials and more. +/config/master.key diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3ca6957 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM ubuntu:bionic AS build +WORKDIR /build +COPY . . +RUN .dockerfiles/build.sh + +FROM build AS app +LABEL org.opencontainers.image.source https://github.com/Madgrades/api.madgrades.com +WORKDIR /app +COPY --from=build /build . +# Default to running app. This can be overriden from docker cli. +CMD .dockerfiles/run-app.sh \ No newline at end of file diff --git a/Gemfile b/Gemfile index 533549d..cd68070 100644 --- a/Gemfile +++ b/Gemfile @@ -7,8 +7,7 @@ end gem 'rack-throttle' gem 'rufus-scheduler' -gem 'dotenv' -gem 'dotenv-rails' +gem 'dotenv-rails', require: 'dotenv/rails-now' gem 'rails-html-sanitizer' # api auth diff --git a/Gemfile.lock b/Gemfile.lock index 038385d..34b5517 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -272,7 +272,6 @@ PLATFORMS DEPENDENCIES byebug capybara (~> 2.13) - dotenv dotenv-rails font-awesome-rails jbuilder diff --git a/app/assets/config/manifest.js b/app/assets/config/manifest.js index b16e53d..21a7880 100644 --- a/app/assets/config/manifest.js +++ b/app/assets/config/manifest.js @@ -1,3 +1,2 @@ -//= link_tree ../images //= link_directory ../javascripts .js //= link_directory ../stylesheets .css diff --git a/config/database.yml b/config/database.yml new file mode 100644 index 0000000..7f60f4a --- /dev/null +++ b/config/database.yml @@ -0,0 +1,20 @@ +default: &default + adapter: mysql2 + encoding: utf8 + pool: 10 + username: <%= ENV['DATABASE_USERNAME'] %> + password: <%= ENV['DATABASE_PASSWORD'] %> + host: <%= ENV['DATABASE_HOST'] %> + port: <%= ENV['DATABASE_PORT'] %> + +development: + <<: *default + database: <%= ENV['DEV_DATABASE_NAME'] %> + +test: + <<: *default + database: <%= ENV['TEST_DATABASE_NAME'] %> + +production: + <<: *default + database: <%= ENV['DATABASE_NAME'] %> diff --git a/config/database.yml.example b/config/database.yml.example deleted file mode 100644 index ab35c65..0000000 --- a/config/database.yml.example +++ /dev/null @@ -1,21 +0,0 @@ -default: &default - adapter: mysql2 - encoding: utf8 - pool: 10 - username: root - password: password -# host: server.madgrades.com -# port: 3306 - socket: /var/run/mysqld/mysqld.sock - -development: - <<: *default - database: madgrades_dev - -test: - <<: *default - database: madgrades_test - -production: - <<: *default - database: madgrades diff --git a/config/environments/production.rb b/config/environments/production.rb index bebf7f5..1283fe3 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -50,7 +50,7 @@ # Use the lowest log level to ensure availability of diagnostic information # when problems arise. - config.log_level = :debug + config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info").downcase.strip.to_sym # Prepend all log lines with the following tags. config.log_tags = [ :request_id ] diff --git a/config/puma/production.rb b/config/puma/production.rb deleted file mode 100644 index 17a00ee..0000000 --- a/config/puma/production.rb +++ /dev/null @@ -1,3 +0,0 @@ -bind 'unix:///var/run/madgrades_api.sock' -pidfile '/var/run/madgrades_api.pid' -Process.daemon(true) diff --git a/drop_migrate.sh b/drop_migrate.sh deleted file mode 100755 index c398291..0000000 --- a/drop_migrate.sh +++ /dev/null @@ -1,3 +0,0 @@ -rake db:drop -rake db:create -rake db:migrate diff --git a/lib/tasks/seed.rake b/lib/tasks/seed.rake index 27f9c87..29b5b65 100644 --- a/lib/tasks/seed.rake +++ b/lib/tasks/seed.rake @@ -12,16 +12,16 @@ namespace :seed do end puts 'Deleting base tables (this may take a while)...' - CourseOffering.delete_all - Course.delete_all - GradeDistribution.delete_all - Instructor.delete_all - Room.delete_all - Schedule.delete_all - Section.delete_all - SubjectMembership.delete_all - Subject.delete_all - Teaching.delete_all + ActiveRecord::Base.connection.truncate(CourseOffering.table_name) + ActiveRecord::Base.connection.truncate(Course.table_name) + ActiveRecord::Base.connection.truncate(GradeDistribution.table_name) + ActiveRecord::Base.connection.truncate(Instructor.table_name) + ActiveRecord::Base.connection.truncate(Room.table_name) + ActiveRecord::Base.connection.truncate(Schedule.table_name) + ActiveRecord::Base.connection.truncate(Section.table_name) + ActiveRecord::Base.connection.truncate(SubjectMembership.table_name) + ActiveRecord::Base.connection.truncate(Subject.table_name) + ActiveRecord::Base.connection.truncate(Teaching.table_name) glob = Dir.glob("#{options[:path]}/*.sql") puts "Seeding tables from: #{options[:path]}/*.sql (#{glob})"