Skip to content

Commit

Permalink
Merge pull request #51390 from andrewn617/devcontainer-use-ruby-feature
Browse files Browse the repository at this point in the history
Devcontainer using ruby image
  • Loading branch information
rafaelfranca committed Mar 25, 2024
2 parents cccbedf + 060eb5f commit 98b3183
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 33 deletions.
32 changes: 10 additions & 22 deletions .devcontainer/Dockerfile
@@ -1,31 +1,16 @@
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.191.1/containers/ruby/.devcontainer/base.Dockerfile

# [Choice] Ruby version: 3, 3.3, 3.2, 3.1, 3.0, 2, 2.7, 2.6
ARG VARIANT="3.3"
FROM mcr.microsoft.com/devcontainers/ruby:${VARIANT}

# [Choice] Node.js version: none, lts/*, 16, 14, 12, 10
ARG NODE_VERSION="lts/*"
RUN if [ "${NODE_VERSION}" != "none" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
ARG VARIANT="3.3.0"
FROM ghcr.io/rails/devcontainer/images/ruby:${VARIANT}

# [Optional] Uncomment this section to install additional OS packages.
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
&& apt-get -y install --no-install-recommends \
mariadb-client libmariadb-dev \
postgresql-client postgresql-contrib libpq-dev \
ffmpeg mupdf mupdf-tools libvips poppler-utils


ARG IMAGEMAGICK_VERSION="7.1.0-5"
RUN wget -qO /tmp/im.tar.xz https://imagemagick.org/archive/releases/ImageMagick-$IMAGEMAGICK_VERSION.tar.xz \
&& wget -qO /tmp/im.sig https://imagemagick.org/archive/releases/ImageMagick-$IMAGEMAGICK_VERSION.tar.xz.asc \
&& gpg --batch --keyserver keyserver.ubuntu.com --recv 89AB63D48277377A \
&& gpg --batch --verify /tmp/im.sig /tmp/im.tar.xz \
&& tar xJf /tmp/im.tar.xz -C /tmp \
&& cd /tmp/ImageMagick-$IMAGEMAGICK_VERSION \
&& ./configure --with-rsvg && make -j 9 && make install \
&& ldconfig /usr/local/lib \
&& rm -rf /tmp/*
ffmpeg mupdf mupdf-tools libvips-dev poppler-utils \
libxml2-dev sqlite3 imagemagick

# Add the Rails main Gemfile and install the gems. This means the gem install can be done
# during build instead of on start. When a fork or branch has different gems, we still have an
Expand All @@ -44,8 +29,11 @@ COPY activerecord/activerecord.gemspec /tmp/rails/activerecord/
COPY activestorage/activestorage.gemspec /tmp/rails/activestorage/
COPY activesupport/activesupport.gemspec /tmp/rails/activesupport/
COPY railties/railties.gemspec /tmp/rails/railties/
# Docker does not support COPY as users other than root. So we need to chown this dir so we
# can bundle as vscode user and then remove the tmp dir
RUN chown -R vscode:vscode /tmp/rails
USER vscode
RUN cd /tmp/rails \
&& bundle install \
&& yarn install \
&& /home/vscode/.rbenv/shims/bundle install \
&& rm -rf /tmp/rails
RUN chown -R vscode:vscode /usr/local/rvm

4 changes: 2 additions & 2 deletions .devcontainer/boot.sh
@@ -1,7 +1,7 @@
bundle install
yarn install

sudo chown -R vscode:vscode /usr/local/bundle
. ${NVM_DIR}/nvm.sh && nvm install --lts
yarn install

cd activerecord

Expand Down
3 changes: 3 additions & 0 deletions .devcontainer/devcontainer.json
Expand Up @@ -9,6 +9,9 @@
"features": {
"ghcr.io/devcontainers/features/github-cli:1": {
"version": "latest"
},
"ghcr.io/devcontainers/features/node:1": {
"version": "latest"
}
},

Expand Down
11 changes: 7 additions & 4 deletions railties/lib/rails/generators/devcontainer.rb
Expand Up @@ -4,10 +4,6 @@ module Rails
module Generators
module Devcontainer
private
def devcontainer_ruby_version
gem_ruby_version.to_s.match(/^\d+\.\d+/).to_s
end

def devcontainer_dependencies
return @devcontainer_dependencies if @devcontainer_dependencies

Expand Down Expand Up @@ -63,6 +59,13 @@ def db_volume_name_for_devcontainer(database = options[:database])
end
end

def db_package_for_dockerfile(database = options[:database])
case database
when "mysql" then "default-libmysqlclient-dev"
when "postgresql" then "libpq-dev"
end
end

def devcontainer_db_service_yaml(**options)
return unless service = db_service_for_devcontainer

Expand Down
@@ -1,12 +1,12 @@
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=<%= devcontainer_ruby_version %>
FROM mcr.microsoft.com/devcontainers/ruby:1-$RUBY_VERSION-bookworm
ARG RUBY_VERSION=<%= gem_ruby_version %>
FROM ghcr.io/rails/devcontainer/images/ruby:$RUBY_VERSION

<%- unless options.skip_active_storage -%>
# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y \
libvips \
<%= db_package_for_dockerfile %> libvips \
# For video thumbnails
ffmpeg \
# For pdf thumbnails. If you want to use mupdf instead of poppler,
Expand Down
@@ -1,6 +1,8 @@
# frozen_string_literal: true

require "rails/generators/base"
require "yaml"
require "json"

module Rails
module Generators
Expand Down
9 changes: 7 additions & 2 deletions railties/test/generators/app_generator_test.rb
Expand Up @@ -1011,8 +1011,7 @@ def test_inclusion_of_ruby_version
ruby_version = "#{Gem::Version.new(Gem::VERSION) >= Gem::Version.new("3.3.13") ? Gem.ruby_version : RUBY_VERSION}"

assert_file ".devcontainer/Dockerfile" do |content|
minor_ruby_version = ruby_version.match(/^\d+\.\d+/).to_s
assert_match(/ARG RUBY_VERSION=#{minor_ruby_version}$/, content)
assert_match(/ARG RUBY_VERSION=#{ruby_version}$/, content)
end
assert_file "Dockerfile" do |content|
assert_match(/ARG RUBY_VERSION=#{ruby_version}/, content)
Expand Down Expand Up @@ -1320,6 +1319,9 @@ def test_devonctainer_postgresql
assert_file("config/database.yml") do |content|
assert_match(/host: <%= ENV\["DB_HOST"\] %>/, content)
end
assert_file(".devcontainer/Dockerfile") do |content|
assert_match(/libpq-dev/, content)
end
end

def test_devonctainer_mysql
Expand Down Expand Up @@ -1348,6 +1350,9 @@ def test_devonctainer_mysql
assert_file("config/database.yml") do |content|
assert_match(/host: <%= ENV.fetch\("DB_HOST"\) \{ "localhost" } %>/, content)
end
assert_file(".devcontainer/Dockerfile") do |content|
assert_match(/default-libmysqlclient-dev/, content)
end
end

def test_devonctainer_mariadb
Expand Down

0 comments on commit 98b3183

Please sign in to comment.