Skip to content

Commit

Permalink
Use build-arg to set rubygems version
Browse files Browse the repository at this point in the history
Fixes mismatch between expected gem version and installed gem version.
As of now installed version of gem in prod is `3.0.3`, when we expect
it to be 2.6.10.
In Dockerfile in we were copying `/usr/local/bin/gem`, which is just
a loader and doesn't decide rubygems version.
Copying `/usr/local/lib/ruby/2.6.0/rubygem` from builder would most likely
work but this seems unconventional and doesn't really save as much build time.
  • Loading branch information
sonalkr132 committed Nov 9, 2020
1 parent 65c4f4b commit 2b3aa04
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -47,7 +47,7 @@ script:
- script/build_docker.sh

env:
- RUBYGEMS_VERSION=2.6.10
- RUBYGEMS_VERSION=3.0.3
- RUBYGEMS_VERSION=latest

matrix:
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Expand Up @@ -64,7 +64,7 @@ Follow the instructions below on how to install Bundler and setup the database.
#### Environment (OS X)

* Use Ruby 2.6.x (`.ruby-version` is present and can be used)
* Use Rubygems 2.6.10
* Use Rubygems 3.0.3
* Install bundler: `gem install bundler`
* Install Elastic Search:
* Pull ElasticSearch `5.6.16` : `docker pull docker.elastic.co/elasticsearch/elasticsearch:5.6.16`
Expand All @@ -84,7 +84,7 @@ Follow the instructions below on how to install Bundler and setup the database.

* Use Ruby 2.6.x `apt-get install ruby2.6`
* Or install via [alternate methods](https://www.ruby-lang.org/en/downloads/)
* Use Rubygems 2.6.10
* Use Rubygems 3.0.3
* Install bundler: `gem install bundler`
* Install Elastic Search:
* Pull ElasticSearch `5.6.16` : `docker pull docker.elastic.co/elasticsearch/elasticsearch:5.6.16`
Expand Down
9 changes: 7 additions & 2 deletions Dockerfile
@@ -1,5 +1,7 @@
FROM ruby:2.6-alpine as build

ARG RUBYGEMS_VERSION

RUN apk add --no-cache \
nodejs \
postgresql-dev \
Expand All @@ -14,7 +16,7 @@ RUN apk add --no-cache \
RUN mkdir -p /app /app/config /app/log/
WORKDIR /app

RUN gem update --system 2.6.10
RUN gem update --system $RUBYGEMS_VERSION

COPY . /app

Expand All @@ -35,6 +37,8 @@ RUN bundle config set --local without 'development test assets' && \

FROM ruby:2.6-alpine

ARG RUBYGEMS_VERSION

RUN apk add --no-cache \
libpq \
ca-certificates \
Expand All @@ -43,10 +47,11 @@ RUN apk add --no-cache \
xz-libs \
&& rm -rf /var/cache/apk/*

RUN gem update --system $RUBYGEMS_VERSION

RUN mkdir -p /app
WORKDIR /app

COPY --from=build /usr/local/bin/gem /usr/local/bin/gem
COPY --from=build /usr/local/bundle/ /usr/local/bundle/
COPY --from=build /app/ /app/

Expand Down
10 changes: 9 additions & 1 deletion script/build_docker.sh
Expand Up @@ -14,7 +14,7 @@ fi

echo "$TRAVIS_COMMIT" > REVISION

docker build -t quay.io/$TRAVIS_REPO_SLUG:$TRAVIS_COMMIT .
docker build -t quay.io/$TRAVIS_REPO_SLUG:$TRAVIS_COMMIT --build-arg RUBYGEMS_VERSION=$RUBYGEMS_VERSION .

docker run -e RAILS_ENV=production -e SECRET_KEY_BASE=1234 -e DATABASE_URL=postgresql://localhost \
--net host quay.io/$TRAVIS_REPO_SLUG:$TRAVIS_COMMIT \
Expand All @@ -32,6 +32,14 @@ if [ $? -eq 1 ]; then
exit 1
fi

rubygems_version_installed=$(docker run quay.io/$TRAVIS_REPO_SLUG:$TRAVIS_COMMIT -- gem -v)

if [ $rubygems_version_installed != $RUBYGEMS_VERSION ]; then
echo "Installed gem version doesn't match"
echo "expected: $RUBYGEMS_VERSION, found: $rubygems_version_installed"
exit 1
fi

if [ -z "$DOCKER_USERNAME" ] || [ -z "$DOCKER_PASSWORD" ]
then
exit 0
Expand Down

0 comments on commit 2b3aa04

Please sign in to comment.