From 1375cfcfef40ea593eb645c4c478f4590e0713a1 Mon Sep 17 00:00:00 2001
From: Andrej Shadura <andrew.shadura@collabora.co.uk>
Date: Thu, 27 Jan 2022 17:31:58 +0100
Subject: [PATCH] =?UTF-8?q?Build=20assets=20in=20the=20base=20stage=20sinc?=
 =?UTF-8?q?e=20they=20don=E2=80=99t=20change=20often?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitlab-ci.yml           |  3 +++
 Dockerfile.frontend      | 19 +++++++---------
 Dockerfile.frontend-base | 47 +++++++++++++++++++++++++++++-----------
 3 files changed, 45 insertions(+), 24 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c9155951eb..85c801f39b 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -36,6 +36,9 @@ frontend-base:
   only:
     changes:
       - Dockerfile.frontend-base
+      - src/api/app/assets
+      - src/api/public
+      - src/api/vendor
       - src/api/Gemfile
       - src/api/Gemfile.lock
   variables:
diff --git a/Dockerfile.frontend b/Dockerfile.frontend
index 11628b66a1..d58b3be355 100644
--- a/Dockerfile.frontend
+++ b/Dockerfile.frontend
@@ -10,7 +10,14 @@ ARG INSTALLDIR=/obs
 
 ADD src/api/ $INSTALLDIR/src/api/
 
-COPY --from=base $INSTALLDIR/src/api/Gemfile $INSTALLDIR/src/api/Gemfile.lock $INSTALLDIR/src/api/
+COPY --from=base $INSTALLDIR/src/api/Gemfile.lock $INSTALLDIR/src/api/
+
+RUN sed -e "/gem 'puma'/d" Gemfile > Gemfile.new; \
+    echo "gem 'puma'" >> Gemfile.new; \
+    diff -u Gemfile Gemfile.new; \
+    mv Gemfile.new Gemfile
+
+COPY --from=base $INSTALLDIR/src/api/public/assets  $INSTALLDIR/src/api/public/assets
 
 WORKDIR $INSTALLDIR/src/api
 
@@ -19,21 +26,11 @@ RUN install -D -t public/css /usr/share/javascript/bootstrap/css/bootstrap.min.c
 
 RUN ls -la
 
-ARG BUNDLE_BUILD__SASSC=--disable-march-tune-native
 ARG NOKOGIRI_USE_SYSTEM_LIBRARIES=1
 
 RUN bundle install --jobs=$(nproc) --retry=3 \
  && rm -rf /var/lib/gems/*/cache
 
-ENV RAILS_ENV=production
-ENV RAILS_LOG_TO_STDOUT=true
-
-RUN echo nonce > config/secret.key \
- && DATABASE_URL=mysql2://localhost/noncedb bundle exec rake assets:precompile RAILS_GROUPS=assets \
- && rm config/secret.key
-
-RUN bundle config --local without test:assets:development
-
 RUN sed -i 's|^#!/usr/bin/ruby.ruby.*$|#!/usr/bin/ruby|' bin/* script/*
 RUN sed -i -e /mailcatcher:/d -e /web:/d Procfile
 
diff --git a/Dockerfile.frontend-base b/Dockerfile.frontend-base
index 3f42bae0bd..c8ee2038ff 100644
--- a/Dockerfile.frontend-base
+++ b/Dockerfile.frontend-base
@@ -1,7 +1,10 @@
-FROM debian:bookworm-slim as frontend-base
+ARG INSTALLDIR=/obs
+ARG DEBIAN_RELEASE=bookworm
+
+FROM debian:$DEBIAN_RELEASE-slim as base
 ENV LC_ALL=C.UTF-8
 ARG DEBIAN_FRONTEND=noninteractive
-ARG INSTALLDIR=/obs
+ARG INSTALLDIR
 
 RUN apt-get update \
  && apt-get install -y \
@@ -39,29 +42,47 @@ RUN apt-get update \
         libxslt1-dev \
         zlib1g-dev
 
-RUN gem install --no-format-executable brakeman --version 5.0.2 --no-doc
-RUN gem install sassc --version 2.0.1 --no-doc
-
 ADD src/api/Gemfile src/api/Gemfile.lock $INSTALLDIR/src/api/
 WORKDIR $INSTALLDIR/src/api/
 
-RUN sed -e "/gem 'puma'/d" Gemfile > Gemfile.new; \
-    echo "gem 'puma'" >> Gemfile.new; \
-    diff -u Gemfile Gemfile.new; \
-    mv Gemfile.new Gemfile
-
 RUN sed -e '/BUNDLED WITH/,+1 d' Gemfile.lock > Gemfile.lock.new; \
     diff -u Gemfile.lock Gemfile.lock.new; \
     mv Gemfile.lock.new Gemfile.lock
 
-ARG BUNDLE_BUILD__SASSC=--disable-march-tune-native
 ARG NOKOGIRI_USE_SYSTEM_LIBRARIES=1
 
-RUN bundle config --global without development:test
+RUN bundle config --global without assets:development:test
 
 RUN bundle install --jobs=$(nproc) --retry=3 \
  && rm -rf \
     /var/lib/gems/*/cache/ \
     /var/lib/gems/*/test/ \
     /var/lib/gems/*/extensions/*/*/*/gem_make.out \
-    /var/lib/gems/*/extensions/*/*/*/*.log \
+    /var/lib/gems/*/extensions/*/*/*/*.log
+
+ENV RAILS_ENV=production
+ENV RAILS_LOG_TO_STDOUT=true
+
+FROM base as asset-builder
+ARG INSTALLDIR
+
+ADD src/api/ $INSTALLDIR/src/api/
+
+COPY --from=base $INSTALLDIR/src/api/Gemfile $INSTALLDIR/src/api/Gemfile.lock $INSTALLDIR/src/api/
+
+ARG BUNDLE_BUILD__SASSC=--disable-march-tune-native
+ARG NOKOGIRI_USE_SYSTEM_LIBRARIES=1
+
+RUN gem install --no-format-executable brakeman --version 5.0.2 --no-doc
+RUN gem install sassc --version 2.0.1 --no-doc
+
+RUN bundle config --local without development:test
+
+RUN bundle install --jobs=$(nproc) --retry=3
+
+RUN echo nonce > config/secret.key \
+ && DATABASE_URL=mysql2://localhost/noncedb bundle exec rake assets:precompile RAILS_GROUPS=assets
+
+FROM base
+ARG INSTALLDIR
+COPY --from=asset-builder $INSTALLDIR/src/api/public/assets  $INSTALLDIR/src/api/public/assets
-- 
GitLab