From eb66aa914e841c6495e0a4e83f9bf2ace909023a Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Thu, 7 Sep 2017 22:52:23 +0200
Subject: [PATCH] Add Jenkinsfile and Makefile to build the image

---
 Jenkinsfile             | 54 +++++++++++++++++++++++++++++++++++++++++
 Makefile                | 48 +++++++++++++++++++++++++++++++-----
 README.rst              |  6 ++++-
 docker-compose-test.yml |  4 +--
 docker-compose.yml      |  2 +-
 5 files changed, 103 insertions(+), 11 deletions(-)
 create mode 100644 Jenkinsfile

diff --git a/Jenkinsfile b/Jenkinsfile
new file mode 100644
index 0000000..c548e32
--- /dev/null
+++ b/Jenkinsfile
@@ -0,0 +1,54 @@
+pipeline {
+    agent { label 'docker' }
+
+    stages {
+        stage('Refresh') {
+            steps {
+                slackSend (color: 'good', message: "STARTED: <${env.BUILD_URL}|${env.JOB_NAME} [${env.BUILD_NUMBER}]>")
+                sh 'git fetch --tags'
+                sh 'make clean'
+                sh 'make refresh'
+            }
+        }
+        stage('Build') {
+            steps {
+                ansiColor('xterm') {
+                    sh 'make build'
+                }
+            }
+        }
+        stage('Test') {
+            steps {
+                sh 'make db_test'
+                /* let the time to postgres to start */
+                sh 'sleep 5'
+                sh 'make test'
+            }
+        }
+        stage('Push') {
+            steps {
+                sh 'make tag'
+                sh 'make push'
+            }
+        }
+        stage('Clean') {
+            steps {
+                sh 'make clean'
+            }
+        }
+    }
+
+    post {
+        always {
+            /* clean up the workspace */
+            deleteDir()
+        }
+        failure {
+            slackSend (color: 'danger', message: "FAILED: <${env.BUILD_URL}|${env.JOB_NAME} [${env.BUILD_NUMBER}]>")
+        }
+        success {
+            slackSend (color: 'good', message: "SUCCESSFUL: <${env.BUILD_URL}|${env.JOB_NAME} [${env.BUILD_NUMBER}]>")
+        }
+    }
+
+}
diff --git a/Makefile b/Makefile
index 26ba66b..4de5afe 100644
--- a/Makefile
+++ b/Makefile
@@ -1,13 +1,49 @@
-.PHONY: help db_test test
+.PHONY: help build tag push refresh release db_dev initdb db_test test
+
+OWNER := europeanspallationsource
+GIT_TAG := $(shell git describe --always)
+IMAGE := ics-inventory
 
 
 help:
-	@echo "Please use \`make <target>' where <target> is one of"
-	@echo "  db_test    to start the postgres database for test"
-	@echo "  test       to run the tests"
+# http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
+	@echo "ics-inventory"
+	@echo "============="
+	@echo
+	@grep -E '^[a-zA-Z0-9_%/-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
+
+build: ## build the latest image
+	docker build -t $(OWNER)/$(IMAGE):latest .
+
+tag: ## tag the latest image with the git tag
+	docker tag $(OWNER)/$(IMAGE):latest $(OWNER)/$(IMAGE):$(GIT_TAG)
+
+push: ## push the latest and git tag image
+	docker push $(OWNER)/$(IMAGE):$(GIT_TAG)
+	docker push $(OWNER)/$(IMAGE):latest
+
+clean: ## remove the image with git tag and the test database
+	-docker rmi $(OWNER)/$(IMAGE):$(GIT_TAG)
+	-docker rm -f inventory_postgres_test
+
+refresh: ## pull the latest image from Docker Hub
+# skip if error: image might not be on dockerhub yet
+	-docker pull $(OWNER)/$(IMAGE):latest
+
+release: refresh \
+	build \
+	tag \
+	push
+release: ## build, tag, and push all stacks
+
+db_dev: ## start postgres for development
+	docker-compose up -d postgres
+
+initdb: ## initialize the dev database
+	docker-compose run --rm web flask initdb
 
-db_test:
+db_test: ## start postgres for test
 	docker-compose -f docker-compose-test.yml up -d postgres_test
 
-test:
+test:  ## run the tests
 	docker-compose -f docker-compose-test.yml run --rm web_test
diff --git a/README.rst b/README.rst
index c028402..89f675c 100644
--- a/README.rst
+++ b/README.rst
@@ -16,8 +16,12 @@ You can use docker for development:
 
     # Start only postgres so it has time to initialize
     $ docker-compose up -d postgres
+    or
+    $ make db_dev
     # Create the database
-    $ docker-compose run web flask initdb
+    $ docker-compose run --rm web flask initdb
+    or
+    $ make initdb
 
 3. Start the application::
 
diff --git a/docker-compose-test.yml b/docker-compose-test.yml
index 987622f..ed71ab4 100644
--- a/docker-compose-test.yml
+++ b/docker-compose-test.yml
@@ -1,7 +1,7 @@
 version: '2'
 services:
   web_test:
-    image: inventory
+    image: europeanspallationsource/ics-inventory:latest
     container_name: inventory_web_test
     build: .
     command: pytest --cov=app -v
@@ -14,8 +14,6 @@ services:
     container_name: inventory_postgres_test
     expose:
       - "5432"
-    volumes:
-      - ./data/test:/var/lib/postgresql/data/pgdata
     environment:
       POSTGRES_USER: ics
       POSTGRES_PASSWORD: icstest
diff --git a/docker-compose.yml b/docker-compose.yml
index dafc603..d6f7922 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -1,7 +1,7 @@
 version: '2'
 services:
   web:
-    image: inventory
+    image: europeanspallationsource/ics-inventory:latest
     container_name: inventory_web
     build: .
     environment:
-- 
GitLab