From c786f1d85586f24552c74c2104fc4d44f669e542 Mon Sep 17 00:00:00 2001
From: David Mehren <git@herrmehren.de>
Date: Mon, 14 Jun 2021 20:23:13 +0200
Subject: [PATCH] Try to speed up building in Docker

---
 .gitlab-ci.yml | 21 +++++++++++++++++----
 Dockerfile     | 21 +++++++--------------
 2 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index de49373..10e939f 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -3,6 +3,7 @@ image: docker:latest
 
 variables:
   DOCKER_DRIVER: overlay2
+  DOCKER_BUILDKIT: 1
 
 # Docker in Docker Service
 services:
@@ -14,8 +15,21 @@ before_script:
 
 build-main:
   script:
-    - docker pull $CI_REGISTRY_IMAGE || true
-    - docker build --cache-from $CI_REGISTRY_IMAGE --pull -t "$CI_REGISTRY_IMAGE" -t "$CI_REGISTRY_IMAGE:branch-$CI_COMMIT_REF_SLUG" .
+    - docker build --cache-from $CI_REGISTRY_IMAGE:stage-src \
+      --target src \
+      --build-arg BUILDKIT_INLINE_CACHE=1 --pull \
+      -t "$CI_REGISTRY_IMAGE:stage-src" \
+      -t "$CI_REGISTRY_IMAGE:stage-src-branch-$CI_COMMIT_REF_SLUG" .
+    - docker build --cache-from $CI_REGISTRY_IMAGE:stage-texlive-update \
+      --target texlive-update \
+      --build-arg BUILDKIT_INLINE_CACHE=1 --pull \
+      -t "$CI_REGISTRY_IMAGE:stage-texlive-update" \
+      -t "$CI_REGISTRY_IMAGE:stage-texlive-update-branch-$CI_COMMIT_REF_SLUG" .
+    - docker build --cache-from $CI_REGISTRY_IMAGE \
+      --target app --squash \
+      --build-arg BUILDKIT_INLINE_CACHE=1 --pull \
+      -t "$CI_REGISTRY_IMAGE" \
+      -t "$CI_REGISTRY_IMAGE:branch-$CI_COMMIT_REF_SLUG" .
     - docker push "$CI_REGISTRY_IMAGE"
     - docker push "$CI_REGISTRY_IMAGE:branch-$CI_COMMIT_REF_SLUG"
   only:
@@ -23,8 +37,7 @@ build-main:
 
 build:
   script:
-    - docker pull $CI_REGISTRY_IMAGE || true
-    - docker build --cache-from $CI_REGISTRY_IMAGE --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" .
+    - docker build --cache-from $CI_REGISTRY_IMAGE --build-arg BUILDKIT_INLINE_CACHE=1 --pull -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG" .
     - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG"
   except:
     - main
diff --git a/Dockerfile b/Dockerfile
index cfa0185..b0a53f5 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -6,7 +6,11 @@ RUN mkdir /src && cd /src && curl "$LDAP_PLUGIN_URL" | tar -xzf - --strip-compon
 RUN ls /src
 RUN sysctl fs.file-max && lsof |wc -l && ulimit -n
 
-FROM $BASE
+FROM $BASE as texlive-update
+
+RUN tlmgr update --self --all && tlmgr install scheme-full
+
+FROM texlive-update as app
 
 # passed from .env (via make)
 ARG collab_text
@@ -15,23 +19,12 @@ ARG login_text
 # set workdir (might solve issue #2 - see https://stackoverflow.com/questions/57534295/)
 WORKDIR /var/www/sharelatex/web
 
-RUN tlmgr update --self --all && tlmgr install scheme-full
-
 # install latest npm
 RUN npm install -g npm
-# clean cache (might solve issue #2)
-#RUN npm cache clean --force
-RUN npm install ldapts-search
-RUN npm install ldapts
-RUN npm install ldap-escape
-#RUN npm install bcrypt@5.0.0
-
-# This variant of updateing texlive does not work
-#RUN  bash -c tlmgr install scheme-full
-# try this one:
+RUN npm install ldapts-search ldapts ldap-escape
+
 RUN apt-get update
 RUN apt-get -y install python-pygments
-#RUN apt-get -y install texlive texlive-lang-german texlive-latex-extra
 
 # overwrite some files
 COPY --from=src /src/ldap-overleaf-sl/sharelatex/AuthenticationManager.js /var/www/sharelatex/web/app/src/Features/Authentication/
-- 
GitLab