From aae39c7a728c7a228aa603899d6d77ced6bffcec Mon Sep 17 00:00:00 2001
From: Daniel Ecer <de-code@users.noreply.github.com>
Date: Mon, 3 Jun 2019 19:57:51 +0800
Subject: [PATCH] refactor build (#103)

* added Makefile; optionally install dev requirements

* added ci-build-and-test

* use ci-build-and-test

* don't install dev dependencies as part of project_tests

* renamed to dev

* use PROJECT_FOLDER for consistency with other projects

* change order of tests run
---
 .travis.yml        |  8 ++------
 Dockerfile         | 20 ++++++++++++--------
 Jenkinsfile        | 16 ++++++----------
 Makefile           | 19 +++++++++++++++++++
 docker-compose.yml |  6 ++++--
 project_tests.sh   |  9 ++++-----
 6 files changed, 47 insertions(+), 31 deletions(-)
 create mode 100644 Makefile

diff --git a/.travis.yml b/.travis.yml
index 451e3ba..4d62948 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -1,12 +1,8 @@
 sudo: required
-language: python
+language: generic
 
 services:
   - docker
 
-before_install:
-  - docker-compose -f docker-compose.yml -f docker-compose.ci.yml build
-
 script:
-  - docker-compose -f docker-compose.yml -f docker-compose.ci.yml run --rm sciencebeam-gym ./project_tests.sh
-
+  - make ci-build-and-test
diff --git a/Dockerfile b/Dockerfile
index 4528f26..5462281 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,21 +1,25 @@
 FROM python:2.7.14-stretch
 
-ENV PROJECT_HOME=/srv/sciencebeam-gym
+ENV PROJECT_FOLDER=/srv/sciencebeam-gym
 
-ENV VENV=${PROJECT_HOME}/venv
+ENV VENV=${PROJECT_FOLDER}/venv
 RUN virtualenv ${VENV}
 ENV PYTHONUSERBASE=${VENV} PATH=${VENV}/bin:$PATH
 
-WORKDIR ${PROJECT_HOME}
+WORKDIR ${PROJECT_FOLDER}
 
-COPY requirements.prereq.txt ${PROJECT_HOME}/
+COPY requirements.prereq.txt ${PROJECT_FOLDER}/
 RUN venv/bin/pip install -r requirements.prereq.txt
 
-COPY requirements.txt ${PROJECT_HOME}/
+COPY requirements.txt ${PROJECT_FOLDER}/
 RUN venv/bin/pip install -r requirements.txt
 
-COPY sciencebeam_gym ${PROJECT_HOME}/sciencebeam_gym
-COPY *.conf *.sh *.in *.txt *.py ${PROJECT_HOME}/
+COPY sciencebeam_gym ${PROJECT_FOLDER}/sciencebeam_gym
+COPY *.conf *.sh *.in *.txt *.py ${PROJECT_FOLDER}/
+
+ARG install_dev
+COPY requirements.dev.txt ./
+RUN if [ "${install_dev}" = "y" ]; then pip install -r requirements.dev.txt; fi
 
 # tests
-COPY .pylintrc .flake8 ${PROJECT_HOME}/
+COPY .pylintrc .flake8 ${PROJECT_FOLDER}/
diff --git a/Jenkinsfile b/Jenkinsfile
index a2f088a..f265842 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -7,17 +7,13 @@ elifeLibrary {
     }
 
     node('containers-jenkins-plugin') {
-        stage 'Build images', {
+        stage 'Build and run tests', {
             checkout scm
-            dockerComposeBuild(commit)
-        }
-
-        stage 'Project tests', {
-            dockerComposeRun(
-                "sciencebeam-gym",
-                "./project_tests.sh",
-                commit
-            )
+            try {
+                sh "make IMAGE_TAG=${commit} ci-build-and-test"
+            } finally {
+                sh "make ci-clean"
+            }
         }
     }
 
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..4f48b20
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,19 @@
+DOCKER_COMPOSE_DEV = docker-compose
+DOCKER_COMPOSE_CI = docker-compose -f docker-compose.yml
+DOCKER_COMPOSE = $(DOCKER_COMPOSE_DEV)
+
+
+build-dev:
+	$(DOCKER_COMPOSE) build sciencebeam-gym-dev
+
+
+test: build-dev
+	$(DOCKER_COMPOSE) run --rm sciencebeam-gym-dev ./project_tests.sh
+
+
+ci-build-and-test:
+	make DOCKER_COMPOSE="$(DOCKER_COMPOSE_CI)" test
+
+
+ci-clean:
+	$(DOCKER_COMPOSE_CI) down -v
diff --git a/docker-compose.yml b/docker-compose.yml
index 667ce27..a4b3eb5 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,8 +1,10 @@
 version: '3'
 
 services:
-    sciencebeam-gym:
+    sciencebeam-gym-dev:
         build:
             context: .
             dockerfile: Dockerfile
-        image: elifesciences/sciencebeam-gym:${IMAGE_TAG}
+            args:
+                install_dev: y
+        image: elifesciences/sciencebeam-gym-dev:${IMAGE_TAG}
diff --git a/project_tests.sh b/project_tests.sh
index 27f2114..dcbe828 100755
--- a/project_tests.sh
+++ b/project_tests.sh
@@ -1,14 +1,13 @@
 #!/bin/bash
 set -e
 
-pip install -r requirements.dev.txt
-
-pytest sciencebeam_gym
+echo "running flake8"
+flake8 sciencebeam_gym setup.py
 
 echo "running pylint"
 pylint sciencebeam_gym setup.py
 
-echo "running flake8"
-flake8 sciencebeam_gym setup.py
+echo "running tests"
+pytest sciencebeam_gym
 
 echo "done"
-- 
GitLab