From 4f22060811c67751dc00892d5efcfa9981d55cd2 Mon Sep 17 00:00:00 2001
From: q3k <q3k@q3k.org>
Date: Thu, 25 Jul 2019 13:03:56 +0000
Subject: [PATCH] fix(ci): Use pre-built docker images

In this change we modify the CI pipeline to use pre-built Docker images
for both build and deploy stages.  To this end, we create
`docker/{build-env,deploy-env}`, two directories containing Dockerfiles
describing how to build those images, and scripts to push them to a
Docker registry.

Currently, docker.card10.badge.events.ccc.de seems broken. Thus, we push
to the Warsaw Hackerspace Docker registry.  Since the Docker images will
be pulled rarely anyway (as they are cached by build nodes), any low
bandwidth-caused slowdowns will not be usually end up in the CI critical
path.

We also bump `.gitlab-ci.yml` to point to those new images.
---
 .gitlab-ci.yml               | 16 ++++------------
 docker/build-env/Dockerfile  | 14 ++++++++++++++
 docker/build-env/push.sh     | 17 +++++++++++++++++
 docker/deploy-env/Dockerfile | 17 +++++++++++++++++
 docker/deploy-env/push.sh    | 17 +++++++++++++++++
 5 files changed, 69 insertions(+), 12 deletions(-)
 create mode 100644 docker/build-env/Dockerfile
 create mode 100755 docker/build-env/push.sh
 create mode 100644 docker/deploy-env/Dockerfile
 create mode 100755 docker/deploy-env/push.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index f521b03cd..7e7dd356c 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,13 +1,8 @@
-image: "debian"
+# maintained by q3k, built using docker/build-env
+image: "registry.k0.hswaw.net/q3k/card10-build-env:20190725-110707Z-4f3bf2a"
 
 build:
     stage: build
-    before_script:
-        - echo "deb http://deb.debian.org/debian stretch-backports main" >> /etc/apt/sources.list
-        - apt update -qq
-        - apt install -y -qq gcc-arm-none-eabi python3-pip git
-        - apt install -y -qq -t stretch-backports meson
-        - pip3 install crc16
     script:
         - ./bootstrap.sh
         - ninja -C build/
@@ -15,11 +10,8 @@ build:
 
 pages:
     stage: deploy
-    image: ubuntu:bionic
-    before_script:
-        - apt update -qq
-        - apt install -y -qq python3-pip git clang libclang-dev llvm
-        - pip3 install sphinx sphinx_rtd_theme clang
+    # maintaned by q3k, build using docker/deploy-env
+    image: "registry.k0.hswaw.net/q3k/card10-deploy-env:20190725-112709Z-4c5d8e8"
     script:
         - export LD_LIBRARY_PATH=$(llvm-config --libdir)
         - echo $LD_LIBRARY_PATH
diff --git a/docker/build-env/Dockerfile b/docker/build-env/Dockerfile
new file mode 100644
index 000000000..4cab5b4ff
--- /dev/null
+++ b/docker/build-env/Dockerfile
@@ -0,0 +1,14 @@
+FROM debian:stretch-backports
+
+RUN set -e -x ;\
+    export DEBIAN_FRONTEND=noninteractive ;\
+    apt-get update -y ;\
+    apt-get install -y \
+        git \
+        gcc-arm-none-eabi \
+        python3-pip ;\
+    apt-get install -y -t stretch-backports \
+        meson ;\
+    pip3 install crc16 ;\
+    rm -rf /var/lib/apt/lists
+
diff --git a/docker/build-env/push.sh b/docker/build-env/push.sh
new file mode 100755
index 000000000..0458b84db
--- /dev/null
+++ b/docker/build-env/push.sh
@@ -0,0 +1,17 @@
+#!/usr/bin/env bash
+
+# Build and push the build env Docker container to the Gitlab container registry.
+
+set -e
+
+function rev() {
+    ( cd $1; git describe --always --match "v[0-9].*" --dirty )
+}
+
+IMAGE=registry.k0.hswaw.net/q3k/card10-build-env
+TAG=$(TZ=UTC date +%Y%m%d-%H%M%SZ)-$(rev .)
+
+docker build -t $IMAGE:$TAG .
+docker push $IMAGE:$TAG
+
+echo "Pushed $IMAGE:$TAG"
diff --git a/docker/deploy-env/Dockerfile b/docker/deploy-env/Dockerfile
new file mode 100644
index 000000000..28434fb52
--- /dev/null
+++ b/docker/deploy-env/Dockerfile
@@ -0,0 +1,17 @@
+FROM ubuntu:bionic
+
+RUN set -e -x ;\
+    export DEBIAN_FRONTEND=noninteractive ;\
+    apt-get update -y ;\
+    apt-get install -y \
+        clang \
+        git \
+        libclang-dev \
+        llvm \
+        python3-pip ;\
+    pip3 install \
+        clang \
+        sphinx \
+        sphinx_rtd_theme ;\
+    rm -rf /var/lib/apt/lists
+
diff --git a/docker/deploy-env/push.sh b/docker/deploy-env/push.sh
new file mode 100755
index 000000000..6efb60278
--- /dev/null
+++ b/docker/deploy-env/push.sh
@@ -0,0 +1,17 @@
+#!/usr/bin/env bash
+
+# Build and push the deploy env Docker container to the Gitlab container registry.
+
+set -e
+
+function rev() {
+    ( cd $1; git describe --always --match "v[0-9].*" --dirty )
+}
+
+IMAGE=registry.k0.hswaw.net/q3k/card10-deploy-env
+TAG=$(TZ=UTC date +%Y%m%d-%H%M%SZ)-$(rev .)
+
+docker build -t $IMAGE:$TAG .
+docker push $IMAGE:$TAG
+
+echo "Pushed $IMAGE:$TAG"
-- 
GitLab