From 756b26c4ae3ffa1f191e751bcb569a2a662664af Mon Sep 17 00:00:00 2001
From: janmax <j.michal@stud.uni-goettingen.de>
Date: Thu, 14 Dec 2017 11:29:31 +0100
Subject: [PATCH] Minor Refactor of the gitlab-ci file

This hopefully accelerates the pipelines as it uses
caching for pip modules and resticts building the
docker image only for the master branch (Which is
the only branch that is every deployed anyway)
---
 .gitlab-ci.yml | 161 +++++++++++++++++++++++++++----------------------
 Makefile       |   2 +-
 2 files changed, 91 insertions(+), 72 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 663ce9c1..b948f105 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,97 +1,116 @@
 stages:
-        - build
-        - test
-        - pages
-        - staging
+  - build
+  - test
+  - pages
+  - build_image
+  - staging
 
 variables:
-        IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
+  IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
 
-# ============================= Building section ============================= #
-build_backend:
-        image: docker:latest
-        stage: build
-        script:
-                - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
-                - docker build -t $IMAGE_TAG .
-                - docker push $IMAGE_TAG
+
+# ========================== Build Testing section =========================== #
+build_test_env:
+  image: python:3.6
+  stage: build
+  script:
+    - python -m venv .venv
+    - source .venv/bin/activate
+    - make install
+  artifacts:
+    paths:
+      - .venv/
+    expire_in: 1 hour
 
 # ============================== Testing section ============================= #
 # ----------------------------- Backend subsection --------------------------- #
-.test_template_backend: &test_definition_backend
-        stage: test
-        image: $IMAGE_TAG
-        before_script:
-                - pip install -r requirements.dev.txt
+.test_template_virtualenv: &test_definition_virtualenv
+  image: python:3.6
+  before_script:
+    - source .venv/bin/activate
+  dependencies:
+    - build_test_env
 
 test_pytest:
-        <<: *test_definition_backend
-        services:
-                - postgres:9.5
-        script:
-                - DJANGO_SETTINGS_MODULE=grady.settings pytest --cov
-        artifacts:
-                paths:
-                        - .coverage
+  <<: *test_definition_virtualenv
+  stage: test
+  services:
+    - postgres:9.5
+  script:
+    - DJANGO_SETTINGS_MODULE=grady.settings pytest --cov
+  artifacts:
+    paths:
+      - .coverage
 
 test_flake8:
-        <<: *test_definition_backend
-        script:
-                - flake8 --exclude=migrations --ignore=N802 core
+  <<: *test_definition_virtualenv
+  stage: test
+  script:
+    - flake8 --exclude=migrations --ignore=N802 core
 
 # ----------------------------- Frontend subsection -------------------------- #
 .test_template_frontend: &test_definition_frontend
-        image: node:carbon
-        stage: test
-        before_script:
-                - cd frontend/
+  image: node:carbon
+  before_script:
+    - cd frontend/
 
 test_frontend:
-        <<: *test_definition_frontend
-        script:
-                - yarn install
-                - yarn test --single-run
+  <<: *test_definition_frontend
+  stage: test
+  script:
+    - yarn install
+    - yarn test --single-run
 
 # =========================== Gitlab pages section =========================== #
 test_coverage:
-        image: $IMAGE_TAG
-        stage:
-                pages
-        script:
-                - pip install coverage
-                - coverage html -d public
-        dependencies:
-                - test_pytest
-        artifacts:
-                paths:
-                        - public
-        only:
-                - master
+  <<: *test_definition_virtualenv
+  stage:
+    pages
+  script:
+    - coverage html -d public
+  dependencies:
+    - test_pytest
+  artifacts:
+    paths:
+      - public
+  only:
+    - master
+
+# =========================== Build Image section ============================ #
+build_backend:
+  image: docker:latest
+  stage: build_image
+  script:
+    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
+    - docker build -t $IMAGE_TAG .
+    - docker push $IMAGE_TAG
+  only:
+    - master
 
 # ============================== Staging section ============================= #
 .staging_template: &staging_definition
-        stage: staging
-        image: docker:latest
-        only:
-                - master
-        before_script:
-                - apk add --update py-pip && pip install docker-compose
+  stage: staging
+  image: docker:latest
+  only:
+    - master
+  before_script:
+    - apk add --update py-pip && pip install docker-compose
 
 staging:
-        <<: *staging_definition
-        environment:
-                name: review/$CI_COMMIT_REF_NAME
-                url: https://staging.grady.janmax.org
-                on_stop: staging_stop
-        script:
-                - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
-                - docker-compose up -d --force-recreate
+  <<: *staging_definition
+  environment:
+    name: review/$CI_COMMIT_REF_NAME
+    url: https://staging.grady.janmax.org
+    on_stop: staging_stop
+  script:
+    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
+    - docker-compose up -d --force-recreate
 
 staging_stop:
-        <<: *staging_definition
-        script:
-                - docker-compose rm --force --stop
-        when: manual
-        environment:
-                name: review/$CI_COMMIT_REF_NAME
-                action: stop
+  <<: *staging_definition
+  script:
+    - docker-compose rm --force --stop
+  when: manual
+  environment:
+    name: review/$CI_COMMIT_REF_NAME
+    action: stop
diff --git a/Makefile b/Makefile
index b9dc2101..035e3be6 100644
--- a/Makefile
+++ b/Makefile
@@ -23,7 +23,7 @@ isort-check:
 migrate:
 	python manage.py migrate
 
-install:
+install: 
 	pip install -r requirements.txt
 	pip install -r requirements.dev.txt
 
-- 
GitLab