Saturday, March 11, 2023

Continuous Integration for Rails 6 App

 To run a Ruby on Rails app on Ubuntu, you will need to install several packages:

  1. build-essential: provides essential tools like GCC and make for compiling and building C/C++ applications.
  2. curl: used to transfer data from or to a server.
  3. libssl-dev: provides the SSL development libraries used by some gems.
  4. libreadline-dev: provides the readline development libraries used by some gems.
  5. zlib1g-dev: provides the zlib development libraries used by some gems.
  6. libxml2-dev: provides the XML development libraries used by some gems.
  7. libxslt1-dev: provides the XSLT development libraries used by some gems.
  8. imagemagick: used to process images uploaded to your application.
  9. nodejs: provides the JavaScript runtime environment required by Rails for asset compilation and JavaScript execution.
  10. yarn: package manager used for JavaScript dependencies in the Rails asset pipeline.

To install all of these packages on Ubuntu, you can run the following command:

sudo apt-get update && sudo apt-get install -y build-essential curl libssl-dev libreadline-dev zlib1g-dev libxml2-dev libxslt1-dev imagemagick nodejs yarn

# Use the latest Alpine image as the base
FROM alpine:latest

# Install required packages for Ruby on Rails
RUN apk add --no-cache \
  build-base \
  nodejs \
  yarn \
  tzdata \
  git \
  postgresql-dev \
  imagemagick \
  openssl \
  less \
  file

# Set the working directory to /app
WORKDIR /app

# Copy the Gemfile and Gemfile.lock into the container
COPY Gemfile Gemfile.lock ./

# Install RubyGems and Bundler
RUN apk add --no-cache ruby ruby-dev ruby-rdoc ruby-irb ruby-bigdecimal && \
    gem install bundler --no-document

# Install the RubyGems specified in the Gemfile
RUN bundle install

# Copy the rest of the application code into the container
COPY . .

# Expose port 3000
EXPOSE 3000

# Start the Rails server
CMD ["rails", "server", "-b", "0.0.0.0"]

Save this as a file named Dockerfile. You can then build the Docker image by running the following command in the same directory as the Dockerfile:

docker build -t myapp .

This will build a Docker image tagged as myapp using the instructions in the Dockerfile.


To install Ruby 3.0 on Alpine, you can use the following Dockerfile:


FROM alpine:latest RUN apk update && \ apk add --no-cache \ build-base \ nodejs \ yarn \ tzdata \ postgresql-dev \ postgresql-client \ ruby \ ruby-dev \ ruby-json \ ruby-etc \ ruby-bigdecimal \ ruby-webrick \ libffi-dev \ openssl-dev \ libc-dev \ curl && \ rm -rf /var/cache/apk/* RUN gem install bundler -v '>= 2.1.4' RUN apk add --update --no-cache --virtual=.build-dependencies curl && \ apk add --update --no-cache ruby ruby-dev ruby-irb ruby-rdoc ruby-etc ruby-bigdecimal && \ curl -sSL https://rvm.io/mpapis.asc | gpg --import - && \ curl -sSL https://rvm.io/pkuczynski.asc | gpg --import - && \ curl -sSL https://get.rvm.io | bash -s stable --ruby=3.0 && \ apk del .build-dependencies && \ rm -rf /var/cache/apk/* ENV PATH="/usr/local/rvm/gems/ruby-3.0.0/bin:/usr/local/rvm/gems/ruby-3.0.0@global/bin:/usr/local/rvm/rubies/ruby-3.0.0/bin:${PATH}" RUN gem install bundler -v '>= 2.1.4' WORKDIR /app COPY Gemfile Gemfile.lock ./ RUN bundle install COPY . . EXPOSE 3000 CMD ["rails", "server", "-b", "0.0.0.0"]


docker build -t <image-name> .


docker run -it <image-name>

Run tests:

FROM alpine:3.15 # Install packages RUN apk add --update --no-cache \ build-base \ git \ nodejs \ yarn \ postgresql-dev \ tzdata \ ruby=3.0.3-r0 \ ruby-dev=3.0.3-r0 \ ruby-json=3.0.3-r0 \ ruby-bundler=2.2.29-r0 # Set timezone RUN ln -sf /usr/share/zoneinfo/UTC /etc/localtime && \ echo "UTC" > /etc/timezone # Set working directory WORKDIR /app # Copy Gemfile and Gemfile.lock COPY Gemfile Gemfile.lock ./ # Install gems RUN bundle install --jobs "$(nproc)" --retry 5 # Copy application files COPY . . # Run tests CMD ["rspec"]

$ docker build -t myapp . $ docker run myapp

See the output of running the tests:

docker run -it <image-name> rspec

FROM alpine:latest RUN apk update && apk add --no-cache \ build-base \ libxml2-dev \ libxslt-dev \ postgresql-dev \ tzdata \ nodejs \ yarn \ ruby=3.0.0-r0 \ ruby-dev \ ruby-etc \ ruby-io-console \ ruby-bigdecimal \ openssl \ ca-certificates \ curl \ git WORKDIR /app COPY . . RUN gem install bundler RUN bundle install CMD ["bin/server", "-f"]


FROM ruby:3.0-alpine RUN apk add --update --no-cache \ build-base \ postgresql-dev \ tzdata \ nodejs \ yarn WORKDIR /app COPY Gemfile* ./ RUN bundle install COPY . . RUN bundle exec rails assets:precompile CMD ["bin/server", "-f"] RUN bundle exec RAILS_ENV=test rails db:seed


FROM alpine:latest # Install system dependencies RUN apk add --update --no-cache \ bash \ build-base \ ca-certificates \ curl \ git \ libffi-dev \ libxml2-dev \ libxslt-dev \ linux-headers \ mariadb-dev \ nodejs \ openssl-dev \ ruby \ ruby-bigdecimal \ ruby-bundler \ ruby-dev \ ruby-io-console \ tzdata \ redis # Install MySQL client RUN apk add --no-cache mysql-client # Install Ruby 3.0 RUN apk add --update --no-cache \ --virtual .ruby-builddeps \ autoconf \ bison \ bzip2 \ bzip2-dev \ ca-certificates \ coreutils \ dpkg-dev dpkg \ gcc \ gdbm-dev \ glib-dev \ libc-dev \ libffi-dev \ libressl \ libressl-dev \ libxml2 \ libxml2-dev \ libxslt \ libxslt-dev \ linux-headers \ make \ ncurses-dev \ openssl \ openssl-dev \ procps \ readline-dev \ ruby \ tar \ xz \ yaml-dev \ zlib-dev && \ git clone https://github.com/rbenv/ruby-build.git /usr/src/ruby-build && \ cd /usr/src/ruby-build && \ ./install.sh && \ rm -rf /usr/src/ruby-build && \ ruby-build 3.0.4 /usr/local && \ gem update --system && \ gem install bundler && \ apk del .ruby-builddeps # Set the working directory WORKDIR /app # Copy the application files COPY . . # Install the application dependencies RUN bundle config set deployment 'true' RUN bundle install --without development test # Start Redis and MySQL RUN redis-server --daemonize yes && \ /usr/bin/mysqld_safe --datadir='/var/lib/mysql' & \ sleep 5 # Run database migrations and seed the database RUN bundle exec rake db:create && \ bundle exec rake db:migrate && \ bundle exec rake db:seed # Start the Rails server CMD ["bundle", "exec", "bin/rails", "server", "-b", "0.0.0.0"]


# Use Alpine as the base image FROM alpine:3.14 # Install packages required for Ruby and Rails RUN apk add --update --no-cache \ bash \ build-base \ git \ nodejs \ nodejs-npm \ openssl-dev \ postgresql-dev \ readline-dev \ tzdata \ yaml-dev \ yarn \ zlib-dev # Install Redis and MySQL RUN apk add --update --no-cache redis mysql mysql-client # Install Ruby 3.0 RUN apk add --update --no-cache ruby ruby-dev && \ gem install bundler --no-document && \ bundle config --global frozen 1 # Set the working directory WORKDIR /app # Copy the Gemfile and Gemfile.lock into the container COPY Gemfile Gemfile.lock ./ # Install gems RUN bundle install --jobs=4 --retry=3 # Copy the application code into the container COPY . . # Run database migrations and seed data RUN bundle exec rails db:migrate && \ bundle exec rails db:seed RAILS_ENV=test # Run tests CMD ["bundle", "exec", "rspec"]