Skip to content

Commit af084f9

Browse files
authored
Dockerfile and Github Action (#576)
* Update Dockerfile to match rails * Add Github Action to build Dockerfile
1 parent e0568d0 commit af084f9

File tree

7 files changed

+100
-79
lines changed

7 files changed

+100
-79
lines changed

.controlplane/Dockerfile

+50-20
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,69 @@
1-
FROM ruby:3.1.2
1+
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
2+
ARG RUBY_VERSION=3.1.2
3+
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base
24

3-
RUN apt-get update
5+
# Install packages needed to build gems and node modules
6+
RUN apt-get update -qq && \
7+
apt-get install --no-install-recommends -y build-essential curl git libpq-dev libvips node-gyp pkg-config python-is-python3
48

5-
# install node and yarn
6-
RUN curl -sL https://deb.nodesource.com/setup_18.x | bash
7-
RUN apt-get install -y nodejs
8-
RUN npm install -g yarn
9+
# Install JavaScript dependencies
10+
# Make sure NODE_VERSION matches the node version in .nvmrc and package.json
11+
ARG NODE_VERSION=18.13.0
12+
ARG YARN_VERSION=1.22.19
13+
ENV PATH=/usr/local/node/bin:$PATH
14+
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
15+
/tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
16+
npm install -g yarn@$YARN_VERSION && \
17+
rm -rf /tmp/node-build-master
918

19+
# Rails app lives here
1020
WORKDIR /app
1121

12-
# install ruby gems
13-
COPY Gemfile* ./
22+
# Set production environment
23+
ENV RAILS_ENV="production" \
24+
BUNDLE_DEPLOYMENT="1" \
25+
BUNDLE_PATH="/usr/local/bundle" \
26+
BUNDLE_WITHOUT="development test"
1427

15-
RUN bundle config set without 'development test' && \
16-
bundle config set with 'staging production' && \
17-
bundle install --jobs=3 --retry=3
1828

19-
# install node packages
29+
# Throw-away build stage to reduce size of final image
30+
FROM base as build
31+
32+
# Install application gems
33+
COPY Gemfile Gemfile.lock ./
34+
RUN bundle install && \
35+
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git
36+
37+
# Install node modules
2038
COPY package.json yarn.lock ./
21-
RUN yarn install
39+
RUN yarn install --frozen-lockfile
40+
41+
# Copy application code
42+
COPY . .
43+
44+
# Final stage for app image
45+
FROM base
2246

23-
COPY . ./
47+
# Install packages needed for deployment
48+
RUN apt-get update -qq && \
49+
apt-get install --no-install-recommends -y curl libvips postgresql-client && \
50+
rm -rf /var/lib/apt/lists /var/cache/apt/archives
2451

25-
ENV RAILS_ENV=production
26-
ENV NODE_ENV=production
52+
# Copy built artifacts: gems, application
53+
COPY --from=build /usr/local/bundle /usr/local/bundle
54+
COPY --from=build /app /app
2755

56+
ENV RAILS_ENV=production \
57+
NODE_ENV=production \
58+
SECRET_KEY_BASE=NOT_USED_NON_BLANK
2859
# compiling assets requires any value for ENV of SECRET_KEY_BASE
29-
ENV SECRET_KEY_BASE=NOT_USED_NON_BLANK
3060

3161
RUN yarn res:build
32-
RUN rails react_on_rails:locale
33-
RUN rails assets:precompile
62+
RUN bin/rails react_on_rails:locale
63+
RUN bin/rails assets:precompile
3464

3565
# add entrypoint
3666
COPY .controlplane/entrypoint.sh ./
3767
ENTRYPOINT ["/app/entrypoint.sh"]
3868

39-
CMD ["rails", "s"]
69+
CMD ["rails", "server"]

.controlplane/entrypoint.sh

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
#!/bin/sh
1+
#!/bin/bash -e
22
# Runs before the main command
3+
# This script is unique to this demo project as it ensures the database and redis are ready before running the rails server
4+
35

46
wait_for_service()
57
{
@@ -18,8 +20,13 @@ echo " -- Waiting for services"
1820
wait_for_service $(echo $DATABASE_URL | sed -e 's|^.*@||' -e 's|/.*$||')
1921
wait_for_service $(echo $REDIS_URL | sed -e 's|redis://||' -e 's|/.*$||')
2022

21-
echo " -- Preparing database"
22-
rails db:prepare
23+
# If running the rails server then create or migrate existing database
24+
if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then
25+
echo " -- Preparing database"
26+
./bin/rails db:prepare
27+
fi
2328

2429
echo " -- Finishing entrypoint.sh, executing '$@'"
30+
31+
# Run the main command
2532
exec "$@"

.github/workflows/deploy-to-control-plane.yml

+7-20
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ env:
2020
CPLN_TOKEN: ${{secrets.CPLN_TOKEN}}
2121

2222
jobs:
23-
deploy-to-control-plane:
23+
build-image-for-control-plane:
2424
runs-on: ubuntu-latest
2525

2626
steps:
@@ -39,28 +39,15 @@ jobs:
3939
cpln --version
4040
gem install cpl -v 1.1.2
4141
42-
- name: setup-control-plane-tools-staging
43-
run: |
44-
cpln profile create default --token ${{ secrets.CPLN_TOKEN_STAGING }} --org ${{ secrets.CPLN_TOKEN_STAGING }} --gvc ${{ secrets.APP_NAME_STAGING }}
45-
cpln image docker-login
46-
4742
- name: Set Short SHA
4843
id: vars
4944
run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
5045

51-
- name: Get short SHA
52-
id: short_sha
53-
run: echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
46+
- name: cpl profile
47+
run: |
48+
cpln profile update default --token ${CPLN_TOKEN}
5449
55-
- name: Containerize and push image
50+
- name: cpl build-image
5651
run: |
57-
cpl build-image -a ${{ secrets.APP_NAME_STAGING }} --commit ${env.SHORT_SHA::7}
58-
# # Containerize and push the application to the org's private image repository. The tag is the short SHA of the commit.
59-
# - name: Containerize application and push image
60-
# shell: bash
61-
# run: |
62-
# cpln profile update default --token ${CPLN_TOKEN}
63-
# cpln image docker-login
64-
# cpln image build --name ${CPLN_IMAGE}:${{steps.vars.outputs.sha_short}} --dockerfile ./Dockerfile --push
65-
##
66-
## build-and-push:
52+
cpln image docker-login
53+
cpl build-image -a ${{ secrets.APP_NAME_STAGING }} --commit ${{steps.vars.outputs.sha_short}}

.overcommit.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Use this file to configure the Overcommit hooks you wish to use. This will
2+
# extend the default configuration defined in:
3+
# https://github.com/sds/overcommit/blob/master/config/default.yml
4+
#
5+
# At the topmost level of this YAML file is a key representing type of hook
6+
# being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
7+
# customize each hook, such as whether to only run it on certain files (via
8+
# `include`), whether to only display output if it fails (via `quiet`), etc.
9+
#
10+
# For a complete list of hooks, see:
11+
# https://github.com/sds/overcommit/tree/master/lib/overcommit/hook
12+
#
13+
# For a complete list of options that you can use to customize hooks, see:
14+
# https://github.com/sds/overcommit#configuration
15+
#
16+
# Uncomment the following lines to make the configuration take effect.
17+
18+
#PreCommit:
19+
# RuboCop:
20+
# enabled: true
21+
# on_warn: fail # Treat all warnings as failures
22+
#
23+
# TrailingWhitespace:
24+
# enabled: true
25+
# exclude:
26+
# - '**/db/structure.sql' # Ignore trailing whitespace in generated files
27+
#
28+
#PostCheckout:
29+
# ALL: # Special hook name that customizes all hooks of this type
30+
# quiet: true # Change all post-checkout hooks to only display output on failure
31+
#
32+
# IndexTags:
33+
# enabled: true # Generate a tags file with `ctags` each time HEAD changes

Dockerfile

-27
This file was deleted.

Gemfile

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
55

66
ruby "3.1.2"
77

8-
gem "cpl", "~>1.1.2"
9-
108
gem "react_on_rails", "13.4.0"
119
gem "shakapacker", "7.1.0"
1210

Gemfile.lock

-7
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,6 @@ GEM
116116
term-ansicolor (~> 1.6)
117117
thor (>= 0.20.3, < 2.0)
118118
tins (~> 1.16)
119-
cpl (1.1.2)
120-
debug (~> 1.7.1)
121-
dotenv (~> 2.8.1)
122-
psych (~> 5.1.0)
123-
thor (~> 1.2.1)
124119
crass (1.0.6)
125120
database_cleaner (2.0.2)
126121
database_cleaner-active_record (>= 2, < 3)
@@ -135,7 +130,6 @@ GEM
135130
debug_inspector (1.1.0)
136131
diff-lcs (1.5.0)
137132
docile (1.4.0)
138-
dotenv (2.8.1)
139133
drb (2.1.1)
140134
ruby2_keywords
141135
erubi (1.12.0)
@@ -429,7 +423,6 @@ DEPENDENCIES
429423
capybara-screenshot
430424
coffee-rails
431425
coveralls_reborn (~> 0.25.0)
432-
cpl (~> 1.1.2)
433426
database_cleaner
434427
debug (>= 1.0.0)
435428
factory_bot_rails

0 commit comments

Comments
 (0)