Skip to content

Commit

Permalink
Changes for building Docker images in Circle CI as part of the CI pro…
Browse files Browse the repository at this point in the history
…cess (#133)
  • Loading branch information
bhanutejags authored Mar 8, 2021
1 parent bd473b3 commit fe2409a
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 19 deletions.
99 changes: 92 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
# Check https://circleci.com/docs/2.0/language-ruby/ for more details
#
version: 2.1
executors:
docker-publisher:
environment:
IMAGE_NAME: treatment-database-app
docker:
- image: docker:20.10.2-git

jobs:
build:
docker:
Expand Down Expand Up @@ -48,7 +55,7 @@ jobs:
- run:
name: Install Dependencies
command: |
bundle install --jobs=4 --retry=3 --path vendor/bundle
bundle install
sudo apt-get update
sudo apt-get install xvfb libfontconfig wkhtmltopdf
Expand Down Expand Up @@ -81,7 +88,7 @@ jobs:
- store_artifacts:
path: /tmp/test-results
destination: test-results


- store_artifacts:
path: coverage
Expand All @@ -90,10 +97,88 @@ jobs:
name: Update Coveralls
command: |
curl -k https://coveralls.io/webhook?repo_token=$COVERALLS_REPO_TOKEN -d "payload[build_num]=$CIRCLE_BUILD_NUM&payload[status]=done"
build-image:
working_directory: /app/treatment_database
executor: docker-publisher

steps:
- checkout
- setup_remote_docker

- restore_cache:
keys:
- v1-{{ .Branch }}
paths:
- /caches/image.tar

- run:
name: Load Docker image layer cache
command: |
set +o pipefail
docker load -i /caches/image.tar | true
- run:
name: Build application Docker image
command: |
docker build --cache-from=${IMAGE_NAME} -t ${IMAGE_NAME}:latest --build-arg RAILS_MASTER_KEY -f docker/app/Dockerfile .
- run:
name: Save Docker image layer cache
command: |
mkdir -p /caches
docker save -o /caches/image.tar ${IMAGE_NAME}:latest
- save_cache:
key: v1-{{ .Branch }}-{{ epoch }}
paths:
- /caches/image.tar

- persist_to_workspace:
root: /caches
paths:
- ./image.tar

publish-image:
executor: docker-publisher

environment:
AWS_DEFAULT_REGION: us-east-2

steps:
- attach_workspace:
at: /tmp/workspace

- setup_remote_docker

- run:
name: Load archived Docker image
command: docker load -i /tmp/workspace/image.tar

- run:
name: Publish Docker Image to ECR
command: |
IMAGE_TAG=${CIRCLE_TAG/v/''}
: "${IMAGE_TAG:=$(echo $CIRCLE_SHA1 | cut -c -7)}"
: "${IMAGE_TAG:=latest}"
alias aws='docker run --rm -it -e AWS_ACCESS_KEY_ID -e AWS_DEFAULT_REGION -e AWS_SECRET_ACCESS_KEY amazon/aws-cli'
aws ecr get-login-password | cut -c5- | docker login --username AWS --password-stdin ${ECR_ENDPOINT}
docker tag ${IMAGE_NAME}:latest "${ECR_ENDPOINT}/${IMAGE_NAME}:$IMAGE_TAG"
docker push "${ECR_ENDPOINT}/${IMAGE_NAME}:$IMAGE_TAG"
workflows:
version: 2
ci:
jobs:
- build:
name: ruby2-6-6
version: 2
ci:
jobs:
- build

- build-image:
requires:
- build

- publish-image:
requires:
- build-image
filters:
branches:
only: qa
25 changes: 25 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.DS_Store
.bin
.git
.gitignore
.bundleignore
.bundle
.byebug_history
.rspec
tmp
log
test
config/deploy
public/packs
public/packs-test
node_modules
yarn-error.log
coverage/
.env.*
.idea
.coveralls.yml
.rubocop*.yml
.ruby-gemset
.ruby-version
.dockerignore
README.md
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ gem 'cancancan'

gem 'paper_trail'

gem 'pagy', '~> 3.7'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: %i[mri mingw x64_mingw]
gem 'database_cleaner'
gem 'factory_bot_rails'
gem 'pagy', '~> 3.7'
gem 'rspec_junit_formatter'
gem 'rspec-rails', '~> 3.8'
gem 'rubocop-rails'
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This is a web application originally developed for the Preservation Lab at the U
git clone github.com/uclibs/treatment_database
bundle install
rails db:migrate
rails db:seed
rails server
```

Expand Down
2 changes: 1 addition & 1 deletion config/credentials.yml.enc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5HBlnFxCyN2VCHbw/UQwGffIEupORPVLHdA83nc55+qDy/4dSsUPVizh3FImvs1iCYSKOF5Aunc+8gV7SBhVbH/2w+EZQvv4mc3HAP2PUBOSO93NsH75i4CG0C9yqxcZddnbHfjUg80LD45cvK0MmiSlg61C4ugT04QMbeDorWn/KSeg9aMaY4xPrcVwC6EwT6xd/6mqGDcMvtF+Go6LUgvNoloTfk6K/7axFR3mLXuuaV6BiaXhKOy1on/1YFaZMVEWEdXpgkfs/jWPyhzpd34EmALpsLc9xVFV+Jn+iXCdpdDsWDDmu+B2QVOvnjtRys+h8faipydpYvbTjgRxlzHYdT5f4lFnHlLVxZB1McxG4oaKGk5AdTLEj6MzjqyXKCoz47Q+rMv8qFQXMe3CEyGsE2RPuNjiqvEY--GTt38r7oSChXDpxc--qhTWdCWMHNlzPUyUaBgx+w==
JTVpnHXV3OQlFfuihjFVPupKNEmemIM/b40kt5FgJsY0pz9ldvi1c6w4BS+Z1eQdbWMHQjz5runXZGohMO+mRxACwvATJnRtt5i5kN+P8gqCqSqUBCVRGt3d117Ly2tKm/DTSIoXvKMr6HMTv4HzdkyqJJ5uRVkCP26PrzPeP42axY2F3K3TtlPxIi3scLonZ1DJKpodPCSH0vDN8i7fiNfmUAKnPEdmwXdApO7aUcbRFHHPth/RUlLDt68akkn7EUwp1sAN9186SQLY8lyl8pFzZHQs60JUz6WpVjMY/XYZMCUrCkmPggbgXXHLvSDtD8wymcBBDQmcJr9XzxYL5lXEwtuE/YFRgZViIkOALPHi9gJv09Xpv3vzn1Xs1JwsXEkitAfrHeCzQd1WCI9VTKG2bhzVW4QDx4GD--RYt8JCyqZCRorDSX--K/ZHirOuRGrC3hYoDCAE/Q==
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ services:
build:
context: .
dockerfile: ./docker/app/Dockerfile
args:
# The value will automatically be picked up from `.env` file, and passed as argument to the docker build
RAILS_MASTER_KEY:
depends_on:
- db
links:
Expand Down
19 changes: 9 additions & 10 deletions docker/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
FROM ruby:2.6.6-alpine3.11
RUN apk --update add build-base ncurses wkhtmltopdf xvfb xvfb-run nodejs postgresql postgresql-contrib sqlite-dev tzdata libxslt-dev libxml2-dev
RUN apk --update add openssh-server
RUN apk --update add mariadb-dev
RUN apk --update --no-cache add build-base ncurses wkhtmltopdf xvfb xvfb-run nodejs yarn sqlite-dev tzdata libxslt-dev libxml2-dev
RUN apk --update --no-cache add openssh-server mariadb-dev

ARG RAILS_MASTER_KEY
# Set Rails to run in production
ENV RAILS_ENV='production'
ENV RACK_ENV='production'
ENV RAILS_ENV='production'
ENV TREATMENT_DATABASE_ADAPTER='mysql2'

ENV RAILS_ROOT /var/www/treatment_app
RUN mkdir -p $RAILS_ROOT

WORKDIR $RAILS_ROOT
COPY scripts/wkhtmltopdf.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/wkhtmltopdf.sh
COPY Gemfile* ./
RUN gem install bundler && bundle install --jobs 20 --retry 5 --without development test
RUN gem install bundler && bundle config set --local without 'development test' && bundle install --no-cache --jobs 20 --retry 5

COPY . ./
# Precompile Rails assets.
RUN bundle exec rake assets:precompile

RUN RAILS_MASTER_KEY=$RAILS_MASTER_KEY bundle exec rake assets:precompile
RUN mkdir -p $RAILS_ROOT/tmp/puma
EXPOSE 3000

# ENTRYPOINT ["bundle", "exec"]
# CMD ["rails", "server", "-b", "0.0.0.0"]
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb"]

0 comments on commit fe2409a

Please sign in to comment.