Skip to content

Commit

Permalink
improve dev setup
Browse files Browse the repository at this point in the history
* add doker build action
* add test worklflow running in docker buildx
* use multistage build
  • Loading branch information
mortbauer committed Feb 5, 2025
1 parent b2deb0e commit 7088b3d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 11 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Docker

on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Docker meta
uses: docker/metadata-action@v5
id: meta
with:
images: foodcoops/foodsoft
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
push: ${{ !github.event_name != 'pull_request' }}
tags: ghcr.io/${{ github.repository }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
build-args: |
BUILDTIME=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
REVISION=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Tests

on: [push, pull_request]

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Run Tests
run: |
docker build \
--target test \
.
42 changes: 31 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ruby:2.7.8
FROM ruby:2.7.8 AS base

RUN supercronicUrl=https://github.com/aptible/supercronic/releases/download/v0.1.3/supercronic-linux-amd64 && \
supercronicBin=/usr/local/bin/supercronic && \
Expand All @@ -7,29 +7,48 @@ RUN supercronicUrl=https://github.com/aptible/supercronic/releases/download/v0.1
echo "$supercronicSha1sum $supercronicBin" | sha1sum -c - && \
chmod +x "$supercronicBin"

ARG RAILS_ENV=production

ENV PORT=3000 \
SMTP_SERVER_PORT=2525 \
RAILS_ENV=production \
RAILS_ENV=${RAILS_ENV} \
RAILS_LOG_TO_STDOUT=true \
RAILS_SERVE_STATIC_FILES=true

WORKDIR /usr/src/app

COPY . ./
RUN --mount=type=cache,target=/var/cache/apt/ \
buildDeps='libmagic-dev mariadb-server nodejs' && \
apt-get update && \
apt-get install --no-install-recommends -y $buildDeps

COPY plugins plugins
COPY config config
COPY config.ru Gemfile Gemfile.lock proc-start Procfile Rakefile VERSION ./
COPY app app
COPY bin bin
COPY db db
COPY lib lib
COPY script script
COPY spec spec
COPY vendor vendor

# install dependencies and generate crontab
RUN buildDeps='libmagic-dev' && \
apt-get update && \
apt-get install --no-install-recommends -y $buildDeps && \
echo 'gem: --no-document' >> ~/.gemrc && \
RUN echo 'gem: --no-document' >> ~/.gemrc && \
gem install bundler -v 2.4.22 && \
bundle config build.nokogiri "--use-system-libraries" && \
bundle install --deployment --without development test -j 4 && \
apt-get purge -y --auto-remove $buildDeps && \
rm -Rf /var/lib/apt/lists/* /var/cache/apt/* ~/.gemrc ~/.bundle && \
\
bundle exec whenever >crontab

FROM base AS test

WORKDIR /usr/src/app
RUN bundle install --deployment --with development --with test -j 4
COPY .rubocop.yml .rubocop_todo.yml ./
RUN bundle exec rubocop --format github --parallel

FROM base AS dist

# compile assets with temporary mysql server
RUN export DATABASE_URL=mysql2://localhost/temp?encoding=utf8 && \
export SECRET_KEY_BASE=thisisnotimportantnow && \
Expand All @@ -46,7 +65,8 @@ RUN export DATABASE_URL=mysql2://localhost/temp?encoding=utf8 && \
/etc/init.d/mariadb stop && \
rm -Rf /run/mysqld /tmp/* /var/tmp/* /var/lib/mysql /var/log/mysql* && \
apt-get purge -y --auto-remove mariadb-server && \
rm -Rf /var/lib/apt/lists/* /var/cache/apt/*
apt-get purge -y --auto-remove $buildDeps && \
rm -Rf /var/lib/apt/lists/* /var/cache/apt/* ~/.gemrc ~/.bundle

# Make relevant dirs and files writable for app user
RUN mkdir -p tmp storage && \
Expand Down

0 comments on commit 7088b3d

Please sign in to comment.