GitLab-CI: add build-docker job with slimming

Change-Id: If2b9d90d28e2637fab7aa6ea525feb446f6e05d4
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index f0209f3..137a62d 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -18,6 +18,13 @@
   PERL5LIB: ./perl5/lib/perl5
 
 build-and-test:
+  rules:
+    - if: $CI_COMMIT_TAG =~ /.+/
+      variables:
+        VID: $CI_COMMIT_TAG
+    - when: manual
+      variables:
+        VID: $CI_COMMIT_BRANCH-$CI_COMMIT_SHORT_SHA
   stage: build
   cache:
     # Cache is shared between branches, but has a unique cache per job.
@@ -105,3 +112,35 @@
   script:
     - echo "This is a manual job which doesn't start automatically, and the pipeline can complete without it starting."
     - ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $DEPLOY_HOST "cd /opt/korap/derekovecs && git stash && git pull && cpanm --installdeps . && cpanm . && rm -rf _Inline && sudo systemctl restart derekovecs"
+
+build-docker:
+  image: docker:latest
+  services:
+    - docker:dind
+  rules:
+    - if: $CI_COMMIT_TAG =~ /.+/
+      variables:
+        VID: $CI_COMMIT_TAG
+    - when: manual
+      variables:
+        VID: $CI_COMMIT_BRANCH-$CI_COMMIT_SHORT_SHA
+  stage: build
+  before_script:
+    - env
+    - apk update
+    - apk add --no-cache git
+  script:
+    - ESCAPED_PWD=$(echo $CI_PROJECT_DIR | sed 's/\//\\\//g')
+    - echo $ESCAPED_PWD
+    - sed -i -e "s/RUN rm -rf example-models//" Dockerfile
+    - cat Dockerfile
+    - docker build -f Dockerfile -t idscorpuslinguistics/derekovecs:$VID-large .
+    - wget https://github.com/slimtoolkit/slim/releases/latest/download/dist_linux.tar.gz
+    - tar zxf dist_linux.tar.gz
+    - export PATH=$(pwd)/dist_linux:$PATH
+    - docker run --rm -v /var/run/docker.sock:/var/run/docker.sock dslim/slim build --include-path /sbin --include-path /usr/local/share/perl5 --env MOJO_CONFIG=/derekovecs/example-models/example-docker.conf --http-probe=true  --tag idscorpuslinguistics/derekovecs:tmp idscorpuslinguistics/derekovecs:$VID-large
+    - docker build -f Dockerfile.remove-example-data --tag idscorpuslinguistics/derekovecs:latest --tag idscorpuslinguistics/derekovecs:$VID .
+    - docker save idscorpuslinguistics/derekovecs:$VID | xz -T0 -M16G -9 > derekovecs.tar.xz
+  artifacts:
+    paths:
+      - derekovecs.tar.xz
diff --git a/Dockerfile b/Dockerfile
index b1da33b..cb329c2 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -58,8 +58,9 @@
 
 # RUN MOJO_CONFIG=/derekovecs/example.conf hypnotoad -t script/derekovecs-server # Just do the Inlined::C stuff
 # does not work with slimming
-
-RUN rm -rf example-models /root/.perl-cpm
+RUN perl -p -i -e 's@"/example-models@"/derekovecs/example-models@' example-models/example-docker.conf
+RUN rm -rf example-models
+RUN rm -rf /root/.perl-cpm
 RUN rm /bin/cpm && \
     apk del git \
 #            g++ \
diff --git a/Dockerfile.remove-example-data b/Dockerfile.remove-example-data
new file mode 100644
index 0000000..4c81d23
--- /dev/null
+++ b/Dockerfile.remove-example-data
@@ -0,0 +1,4 @@
+FROM idscorpuslinguistics/derekovecs:tmp
+RUN hypnotoad -t script/derekovecs-server || true
+RUN rm -rf /usr/bin/g++* /usr/include/linux /usr/bin/gcc* /usr/bin/make /usr/lib/gcc /usr/bin/x86_64-alpine-linux*
+RUN rm -rf /derekovecs/example-models