From 953d426bd326896dcb7d37f735738cf6d309acc7 Mon Sep 17 00:00:00 2001 From: Mattia Roccoberton Date: Fri, 28 Mar 2025 00:24:45 +0100 Subject: [PATCH] build: Update Docker dev setup (2) --- .dockerignore | 15 +++++++++ .env | 2 -- .github/workflows/specs_rails70.yml | 3 ++ .github/workflows/specs_rails71.yml | 3 ++ .github/workflows/specs_rails72.yml | 3 ++ .github/workflows/specs_rails80.yml | 3 ++ Gemfile | 17 +++++++++-- Makefile | 43 ++++++++++++-------------- README.md | 45 +-------------------------- Rakefile | 11 +++++++ activeadmin_dynamic_fields.gemspec | 3 +- bin/rails | 2 +- docker-compose.yml | 25 --------------- extra/.bashrc | 3 ++ extra/.env | 7 +++++ extra/Dockerfile | 26 +++++++++++----- extra/Dockerfile_alpine | 18 ----------- extra/development.md | 47 +++++++++++++++++++++++++++++ extra/docker-compose.yml | 20 ++++++++++++ extra/entrypoint.sh | 8 ----- extra/init.sh | 7 +++++ spec/dummy/db/schema.rb | 2 +- spec/dummy/db/schema_development.rb | 2 +- 23 files changed, 178 insertions(+), 137 deletions(-) create mode 100644 .dockerignore delete mode 100644 .env delete mode 100644 docker-compose.yml create mode 100644 extra/.bashrc create mode 100644 extra/.env delete mode 100644 extra/Dockerfile_alpine create mode 100644 extra/development.md create mode 100644 extra/docker-compose.yml delete mode 100644 extra/entrypoint.sh create mode 100755 extra/init.sh diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ee8871e --- /dev/null +++ b/.dockerignore @@ -0,0 +1,15 @@ +/Makefile +/LICENSE.txt +/Gemfile.lock +/extra/init.sh + +/app +/bin +/coverage +/gemfiles +/spec/dummy/db/*.sqlite3 +/spec/dummy/log +/spec/dummy/tmp +/spec/dummy/**/*_spec.rb + +/**/*.md diff --git a/.env b/.env deleted file mode 100644 index 3247105..0000000 --- a/.env +++ /dev/null @@ -1,2 +0,0 @@ -UID=1000 -GID=1000 diff --git a/.github/workflows/specs_rails70.yml b/.github/workflows/specs_rails70.yml index 0dfd188..3d0d665 100644 --- a/.github/workflows/specs_rails70.yml +++ b/.github/workflows/specs_rails70.yml @@ -29,6 +29,9 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true + - name: Database setup + run: bin/rails db:reset db:test:prepare + - name: Run tests run: bundle exec rspec --profile diff --git a/.github/workflows/specs_rails71.yml b/.github/workflows/specs_rails71.yml index 6f30065..30bc0e1 100644 --- a/.github/workflows/specs_rails71.yml +++ b/.github/workflows/specs_rails71.yml @@ -29,6 +29,9 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true + - name: Database setup + run: bin/rails db:reset db:test:prepare + - name: Run tests run: bundle exec rspec --profile diff --git a/.github/workflows/specs_rails72.yml b/.github/workflows/specs_rails72.yml index 8549078..8eb9c19 100644 --- a/.github/workflows/specs_rails72.yml +++ b/.github/workflows/specs_rails72.yml @@ -29,6 +29,9 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true + - name: Database setup + run: bin/rails db:reset db:test:prepare + - name: Run tests run: bundle exec rspec --profile diff --git a/.github/workflows/specs_rails80.yml b/.github/workflows/specs_rails80.yml index 31b6a3b..551138c 100644 --- a/.github/workflows/specs_rails80.yml +++ b/.github/workflows/specs_rails80.yml @@ -29,6 +29,9 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true + - name: Database setup + run: bin/rails db:reset db:test:prepare + - name: Run tests run: bundle exec rspec --profile diff --git a/Gemfile b/Gemfile index 969a73c..1db4a16 100644 --- a/Gemfile +++ b/Gemfile @@ -3,10 +3,21 @@ source 'https://rubygems.org' if ENV['DEVEL'] == '1' - rails_ver = ENV.fetch('RAILS_VERSION') - gem 'rails', rails_ver + rails_ver = ENV.fetch('RAILS_VERSION', '') + activeadmin_ver = ENV.fetch('ACTIVEADMIN_VERSION', '') + + if rails_ver.empty? + gem 'rails' + else + gem 'rails', "~> #{rails_ver}" + end + + if activeadmin_ver.empty? + gem 'activeadmin' + else + gem 'activeadmin', "~> #{activeadmin_ver}" + end - gem 'activeadmin', ENV.fetch('ACTIVEADMIN_VERSION') gem 'activeadmin_dynamic_fields', path: './' gem 'appraisal', '~> 2.4' diff --git a/Makefile b/Makefile index a6fc275..6290043 100644 --- a/Makefile +++ b/Makefile @@ -1,38 +1,33 @@ +include extra/.env + help: - @echo "Main targets: up / down / console / shell" + @echo "Main targets: build / specs / up / server / specs / shell" # Docker commands -down: - docker compose down - -up: - docker compose up -attach: - docker compose attach app +build: + @rm -f Gemfile.lock + @docker compose -f extra/docker-compose.yml build -up_attach: - docker compose up -d && docker compose attach app +up: build + @docker compose -f extra/docker-compose.yml up cleanup: - docker container rm -f activeadmin_dynamic_fields_app && docker image rm -f activeadmin_dynamic_fields-app + @docker compose -f extra/docker-compose.yml down --volumes --rmi local --remove-orphans -# Rails specific commands -console: - docker compose exec -e "PAGER=more" app bin/rails console +# App commands -routes: - docker compose exec app bin/rails routes +server: + @docker compose -f extra/docker-compose.yml exec app bin/rails s -b 0.0.0.0 -p ${SERVER_PORT} specs: - docker compose exec app bin/rspec --fail-fast + @docker compose -f extra/docker-compose.yml exec app bin/rspec --fail-fast + +lint: + @docker compose -f extra/docker-compose.yml exec app bin/rubocop -# Other commands -bundle: - docker compose exec app bundle +appraisal_update: + @docker compose -f extra/docker-compose.yml exec app bin/appraisal update shell: - docker compose exec -e "PAGER=more" app bash - -lint: - docker compose exec app bin/rubocop + @docker compose -f extra/docker-compose.yml exec app bash diff --git a/README.md b/README.md index d9a5efa..d93c21f 100644 --- a/README.md +++ b/README.md @@ -239,50 +239,7 @@ The link url is loaded via AJAX before opening the dialog. Project created by [Mattia Roccoberton](http://blocknot.es), thanks also to the good guys that opened issues and pull requests from time to time. -There 3 ways to interact with this project: - -1) Using Docker: - -```sh -# Run rails server on the dummy app (=> http://localhost:3000 to access to ActiveAdmin): -make up -# Enter in a Rails console (with the dummy app started): -make console -# Enter in a shell (with the dummy app started): -make shell -# Run the linter on the project (with the dummy app started): -make lint -# Run the test suite (with the dummy app started): -make specs -# Remove container and image: -make cleanup -# To try different versions of Ruby/Rails/ActiveAdmin edit docker-compose.yml -# For more commands please check the Makefile -``` - -2) Using Appraisal: - -```sh -export RAILS_ENV=development -# Install dependencies: -bin/appraisal -# Run server (or any command): -bin/appraisal rails s -# Or with a specific configuration: -bin/appraisal rails80-activeadmin rails s -``` - -3) With a local setup: - -```sh -# Dev setup (set the required envs): -source extra/dev_setup.sh -# Install dependencies: -bundle update -# Run server (or any command): -bin/rails s -# To try different versions of Rails/ActiveAdmin edit extra/dev_setup.sh -``` +For development information please check [this document](extra/development.md). ## Do you like it? Star it! diff --git a/Rakefile b/Rakefile index 33129e7..dbb5eaf 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,16 @@ # frozen_string_literal: true +begin + require 'bundler/setup' +rescue LoadError + puts 'You must `gem install bundler` and `bundle install` to run rake tasks' +end + +APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__) +load 'rails/tasks/engine.rake' + +load 'rails/tasks/statistics.rake' + require 'bundler/gem_tasks' begin diff --git a/activeadmin_dynamic_fields.gemspec b/activeadmin_dynamic_fields.gemspec index 62a63cb..83adb71 100644 --- a/activeadmin_dynamic_fields.gemspec +++ b/activeadmin_dynamic_fields.gemspec @@ -1,7 +1,6 @@ # frozen_string_literal: true -lib = File.expand_path('lib', __dir__) -$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) +$:.push File.expand_path('lib', __dir__) require 'activeadmin/dynamic_fields/version' Gem::Specification.new do |spec| diff --git a/bin/rails b/bin/rails index 61289fe..c6b771f 100755 --- a/bin/rails +++ b/bin/rails @@ -4,7 +4,7 @@ # This command will automatically be run when you run "rails" with Rails gems # installed from the root of your application. -# ENV['RAILS_ENV'] ||= 'test' +ENV['RAILS_ENV'] ||= 'development' ENGINE_ROOT = File.expand_path('..', __dir__) ENGINE_PATH = File.expand_path('../lib/activeadmin/dynamic_fields/engine', __dir__) diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 8896ffd..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,25 +0,0 @@ -services: - app: - container_name: 'activeadmin_dynamic_fields_app' - build: - context: . - dockerfile: ./extra/Dockerfile - # dockerfile: ./extra/Dockerfile_alpine - args: - # Debian-based Ruby image: - RUBY_IMAGE: ruby:3.4-slim - UID: ${UID} - environment: - ACTIVEADMIN_VERSION: ~> 3.3 - RAILS_VERSION: ~> 8.0 - user: "${UID}:${GID}" - ports: - - '3000:3000' - working_dir: '/app' - volumes: - - '.:/app' - stdin_open: true - tty: true - entrypoint: - - /bin/sh - - ./extra/entrypoint.sh diff --git a/extra/.bashrc b/extra/.bashrc new file mode 100644 index 0000000..78ebf4f --- /dev/null +++ b/extra/.bashrc @@ -0,0 +1,3 @@ +alias ls='ls --color' +alias ll='ls -l' +alias la='ls -la' diff --git a/extra/.env b/extra/.env new file mode 100644 index 0000000..16eb445 --- /dev/null +++ b/extra/.env @@ -0,0 +1,7 @@ +COMPOSE_PROJECT_NAME=activeadmin_dynamic_fields + +BUNDLER_VERSION=2.5.23 +SERVER_PORT=4000 + +UID=1000 +GID=1000 diff --git a/extra/Dockerfile b/extra/Dockerfile index ca9287b..3fd30ae 100644 --- a/extra/Dockerfile +++ b/extra/Dockerfile @@ -1,21 +1,31 @@ ARG RUBY_IMAGE=ruby:3 FROM ${RUBY_IMAGE} -ARG UID - +ENV DEBIAN_FRONTEND=noninteractive ENV DEVEL=1 ENV LANG=C.UTF-8 -ENV RAILS_ENV=development RUN apt-get update -qq -RUN DEBIAN_FRONTEND=noninteractive apt-get install -yqq --no-install-recommends build-essential chromium libyaml-dev nano +RUN apt-get install -yqq --no-install-recommends build-essential chromium libyaml-dev nano netcat-traditional pkg-config -RUN gem install bundler +ARG BUNDLER_VERSION +RUN gem install bundler -v ${BUNDLER_VERSION} RUN echo 'gem: --no-document' > /etc/gemrc -RUN useradd -u ${UID} --shell /bin/bash app +ARG UID +RUN useradd -u $UID --shell /bin/bash app + RUN mkdir -p /home/app && chown -R app:app /home/app -RUN chown -R app /usr/local/bundle -USER ${UID} +ARG RAILS_VERSION +ENV RAILS_VERSION=$RAILS_VERSION + +ARG ACTIVEADMIN_VERSION +ENV ACTIVEADMIN_VERSION=$ACTIVEADMIN_VERSION + +WORKDIR /app COPY . /app +RUN bundle install +RUN chown -R app:app /app/spec/dummy/db /usr/local/bundle + +RUN ln -s /app/extra/.bashrc /home/app/.bashrc diff --git a/extra/Dockerfile_alpine b/extra/Dockerfile_alpine deleted file mode 100644 index e960879..0000000 --- a/extra/Dockerfile_alpine +++ /dev/null @@ -1,18 +0,0 @@ -FROM ruby:3.3-alpine - -ARG UID - -ENV DEVEL=1 -ENV LANG=C.UTF-8 -ENV RAILS_ENV=development - -RUN apk update && apk add --no-cache build-base chromium tzdata yaml-dev - -RUN gem install bundler -RUN echo 'gem: --no-document' > /etc/gemrc - -RUN adduser -u ${UID} -D app -RUN mkdir -p /home/app && chown -R app:app /home/app -RUN chown -R app /usr/local/bundle - -COPY . /app diff --git a/extra/development.md b/extra/development.md new file mode 100644 index 0000000..30d67e2 --- /dev/null +++ b/extra/development.md @@ -0,0 +1,47 @@ +## Development + +There 3 ways to interact with this project: + +1) Using Docker: + +```sh +# Run rails server on the dummy app (=> http://localhost:3000 to access to ActiveAdmin): +make up +# Enter in a Rails console (with the dummy app started): +make console +# Enter in a shell (with the dummy app started): +make shell +# Run the linter on the project (with the dummy app started): +make lint +# Run the test suite (with the dummy app started): +make specs +# Remove container and image: +make cleanup +# To try different versions of Ruby/Rails/ActiveAdmin: +RUBY=3.2 RAILS=7.1.0 ACTIVEADMIN=3.2.0 make up +# For more commands please check the Makefile +``` + +2) Using Appraisal: + +```sh +export RAILS_ENV=development +# Install dependencies: +bin/appraisal +# Run server (or any command): +bin/appraisal rails s +# Or with a specific configuration: +bin/appraisal rails80-activeadmin rails s +``` + +3) With a local setup: + +```sh +# Dev setup (set the required envs): +source extra/dev_setup.sh +# Install dependencies: +bundle update +# Run server (or any command): +bin/rails s +# To try different versions of Rails/ActiveAdmin edit extra/dev_setup.sh +``` diff --git a/extra/docker-compose.yml b/extra/docker-compose.yml new file mode 100644 index 0000000..999255b --- /dev/null +++ b/extra/docker-compose.yml @@ -0,0 +1,20 @@ +services: + app: + build: + context: .. + dockerfile: extra/Dockerfile + args: + ACTIVEADMIN_VERSION: ${ACTIVEADMIN:-} + BUNDLER_VERSION: ${BUNDLER_VERSION} + RAILS_VERSION: ${RAILS:-} + RUBY_IMAGE: ruby:${RUBY:-3.4}-slim + UID: ${UID} + user: ${UID}:${GID} + ports: + - ${SERVER_PORT}:${SERVER_PORT} + working_dir: /app + volumes: + - ..:/app + stdin_open: true + tty: true + command: ${COMMAND:-extra/init.sh} diff --git a/extra/entrypoint.sh b/extra/entrypoint.sh deleted file mode 100644 index 0c9b41c..0000000 --- a/extra/entrypoint.sh +++ /dev/null @@ -1,8 +0,0 @@ -echo "> Install dependencies" -rm -f Gemfile.lock && bundle install - -echo "> Run pending migrations" -cd spec/dummy && bundle exec rails db:migrate - -echo "> Start Rails server" -cd /app && rm -f spec/dummy/tmp/pids/server.pid && bundle exec rails s -b 0.0.0.0 diff --git a/extra/init.sh b/extra/init.sh new file mode 100755 index 0000000..a17f21e --- /dev/null +++ b/extra/init.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +# Setup database +bin/rails db:reset db:test:prepare + +# +sh diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 6b551eb..18edf98 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2018_06_07_053739) do +ActiveRecord::Schema.define(version: 2018_06_07_053739) do create_table "active_admin_comments", force: :cascade do |t| t.string "namespace" t.text "body" diff --git a/spec/dummy/db/schema_development.rb b/spec/dummy/db/schema_development.rb index b55247a..18edf98 100644 --- a/spec/dummy/db/schema_development.rb +++ b/spec/dummy/db/schema_development.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2018_06_07_053739) do +ActiveRecord::Schema.define(version: 2018_06_07_053739) do create_table "active_admin_comments", force: :cascade do |t| t.string "namespace" t.text "body"