From c4eed754b5fabd33ac11e014567788d65940f93f Mon Sep 17 00:00:00 2001
From: Ulf Seltmann <seltmann@ub.uni-leipzig.de>
Date: Tue, 14 Aug 2018 12:09:52 +0200
Subject: [PATCH] * added multiple repo-definitions * renamed helmcharts to
 helmchart * added option for configmap creation * added option for
 imagePullSecrets

---
 .gitlab-ci.yml                                | 70 +++++++++++++------
 {helmcharts => helmchart}/.helmignore         |  0
 {helmcharts => helmchart}/Chart.yaml          |  0
 {helmcharts => helmchart}/templates/NOTES.txt |  0
 .../templates/_helpers.tpl                    |  0
 helmchart/templates/configmap.yaml            |  8 +++
 .../templates/deployment.yaml                 |  4 ++
 .../templates/ingress.yaml                    |  0
 {helmcharts => helmchart}/templates/pvc.yml   |  0
 helmchart/templates/secret.yaml               |  9 +++
 .../templates/service.yaml                    |  0
 {helmcharts => helmchart}/values.yaml         |  2 +
 helmcharts/templates/configmap.yaml           |  6 --
 13 files changed, 71 insertions(+), 28 deletions(-)
 rename {helmcharts => helmchart}/.helmignore (100%)
 rename {helmcharts => helmchart}/Chart.yaml (100%)
 rename {helmcharts => helmchart}/templates/NOTES.txt (100%)
 rename {helmcharts => helmchart}/templates/_helpers.tpl (100%)
 create mode 100644 helmchart/templates/configmap.yaml
 rename {helmcharts => helmchart}/templates/deployment.yaml (94%)
 rename {helmcharts => helmchart}/templates/ingress.yaml (100%)
 rename {helmcharts => helmchart}/templates/pvc.yml (100%)
 create mode 100644 helmchart/templates/secret.yaml
 rename {helmcharts => helmchart}/templates/service.yaml (100%)
 rename {helmcharts => helmchart}/values.yaml (98%)
 delete mode 100644 helmcharts/templates/configmap.yaml

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 3f76014..fd1c588 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -11,7 +11,9 @@ stages:
 
 variables:
   npm_config_cache: ${CI_PROJECT_DIR}/.npm
-  image_name: ubleipzig/dacap
+  production_repo: ubleipzig/dacap
+  staging_repo: services.ub.uni-leipzig.de:11443/bdd_dev/dacap
+  alpha_repo: services.ub.uni-leipzig.de:11443/bdd_dev/dacap
 
 npm_install:
   stage: prepare
@@ -110,9 +112,9 @@ docker_publish_alpha:
   script: |
     deployer publish \
       --input image.tar.gz \
-      --docker-config "${DOCKER_AUTH_CONFIG}" \
-      --name ${image_name} \
-      --tag ${CI_COMMIT_REF_NAME}
+      --docker-config "${DOCKER_ALPHA_AUTH_CONFIG}" \
+      --name ${alpha_repo} \
+      --tag ${CI_COMMIT_REF_SLUG}
   dependencies:
   - docker_build
   tags:
@@ -130,8 +132,8 @@ docker_publish_staging:
   script: |
     deployer publish \
       --input image.tar.gz \
-      --docker-config "$DOCKER_AUTH_CONFIG" \
-      --name ${image_name} \
+      --docker-config "${DOCKER_STAGING_AUTH_CONFIG}" \
+      --name ${staging_repo} \
       --tag staging
   dependencies:
   - docker_build
@@ -146,16 +148,17 @@ docker_publish_production:
   services:
   - docker:dind
   script: |
-    version=`expr ${CI_COMMIT_TAG} ':' 'release/\(.\+\)'`
+    export version=$(expr ${CI_COMMIT_TAG} ':' 'release/\(.\+\)')
+    export major_version=$(expr ${version} ':' '\([^.]\+\)')
+    export minor_version=$(expr ${version} ':' '[^.]\+\.\([^.]\+\)')
     deployer publish \
       --input image.tar.gz
-      --docker-config "$DOCKER_AUTH_CONFIG" \
-      --name ${image_name} \
+      --docker-config "$DOCKER_LIVE_AUTH_CONFIG" \
+      --name ${production_repo} \
       --tag latest \
       --tag ${version} \
-      --tag $(expr ${version} ':' '\([^.]\+\)') \
-      --tag $(expr ${version} ':' '[^.]\+\.\([^.]\+\)') \
-      --tag $(expr ${version} ':' '[^.]\+\.[^.]\+\.\(.\+\))`
+      --tag "${major_version}.${minor_version}" \
+      --tag "${major_version}"
   dependencies:
   - docker_build
   tags:
@@ -167,14 +170,14 @@ docker_publish_production:
 
 deploy_alpha:
   stage: deploy
-  image: ubleipzig/deployer:1.0.0
+  image: ubleipzig/deployer:1.1.0
   variables:
     url_schema: https://
     ingress_host: alpha.ub.uni-leipzig.de
     ingress_path: /dacap
   environment:
-    name: alpha/${CI_COMMIT_REF_NAME}
-    url: https://alpha.ub.uni-leipzig.de/dacap/${CI_COMMIT_REF_NAME}/admin
+    name: alpha/${CI_COMMIT_REF_SLUG}
+    url: https://alpha.ub.uni-leipzig.de/dacap/${CI_COMMIT_REF_SLUG}/admin
   script: |
     deployer deploy \
       --cluster-url "${K8S_ALPHA_CLUSTER_URL}" \
@@ -182,14 +185,16 @@ deploy_alpha:
       --token "${K8S_ALPHA_DEPLOY_TOKEN}" \
       --namespace "${K8S_ALPHA_CLUSTER_NAMESPACE}" \
       --service-account "${K8S_ALPHA_DEPLOY_USER}" \
-      --name issue-${CI_COMMIT_REF_NAME} \
-      --set-string environment.proxy_url=${url_schema}${ingress_host}${ingress_path}/${CI_COMMIT_REF_NAME}/ \
+      --name issue-${CI_COMMIT_REF_SLUG} \
+      --set-string environment.proxy_url=${url_schema}${ingress_host}${ingress_path}/${CI_COMMIT_REF_SLUG}/ \
       --set-string environment.http_proxy=http://proxy.uni-leipzig.de:3128 \
-      --set-string ingress.path=${ingress_path}/${CI_COMMIT_REF_NAME} \
+      --set-string ingress.path=${ingress_path}/${CI_COMMIT_REF_SLUG} \
       --set-string ingress.hosts[0]=${ingress_host} \
-      --set-string image.tag=alpha-${CI_COMMIT_REF_NAME} \
+      --set-string image.repository=${alpha_repo} \
+      --set-string image.tag=${CI_COMMIT_REF_SLUG} \
       --set-string pullPolicy=Always \
-      --charts ./helmcharts
+      --set-string dockerconfigjson=$(echo $(echo "${DOCKER_ALPHA_AUTH_CONFIG}"|base64)|tr -d " ") \
+      --charts ./helmchart
   dependencies:
   - docker_publish_alpha
   tags:
@@ -199,6 +204,25 @@ deploy_alpha:
   except:
   - master
 
+undeploy_alpha:
+  stage: deploy
+  image: ubleipzig/deployer:1.0.0
+  script: |
+    deployer undeploy \
+      --cluster-url "${K8S_ALPHA_CLUSTER_URL}" \
+      --certificate-authority "${K8S_ALPHA_CLUSTER_CA}" \
+      --token "${K8S_ALPHA_DEPLOY_TOKEN}" \
+      --namespace "${K8S_ALPHA_CLUSTER_NAMESPACE}" \
+      --service-account "${K8S_ALPHA_DEPLOY_USER}" \
+      --name issue-${CI_COMMIT_REF_SLUG}
+  tags:
+  - docker
+  only:
+  - branches
+  except:
+  - master
+  when: manual
+
 deploy_staging:
   stage: deploy
   image: ubleipzig/deployer:1.0.0
@@ -221,9 +245,11 @@ deploy_staging:
       --set-string environment.http_proxy=http://proxy.uni-leipzig.de:3128 \
       --set-string ingress.path=${ingress_path} \
       --set-string ingress.hosts[0]=${ingress_host} \
+      --set-string image.repository=${alpha_repo} \
       --set-string image.tag=staging \
       --set-string pullPolicy=Always \
-      --charts ./helmcharts
+      --set-string dockerconfigjson=$(echo $(echo "${DOCKER_STAGING_AUTH_CONFIG}"|base64)|tr -d " ") \
+      --charts ./helmchart
 
   dependencies:
   - docker_publish_staging
@@ -257,7 +283,7 @@ deploy_production:
       --set-string ingress.hosts[0]=${ingress_host} \
       --set-string image.tag=${version} \
       --set-string pullPolicy=Always \
-      --charts ./helmcharts
+      --charts ./helmchart
   dependencies:
   - docker_publish_production
   tags:
diff --git a/helmcharts/.helmignore b/helmchart/.helmignore
similarity index 100%
rename from helmcharts/.helmignore
rename to helmchart/.helmignore
diff --git a/helmcharts/Chart.yaml b/helmchart/Chart.yaml
similarity index 100%
rename from helmcharts/Chart.yaml
rename to helmchart/Chart.yaml
diff --git a/helmcharts/templates/NOTES.txt b/helmchart/templates/NOTES.txt
similarity index 100%
rename from helmcharts/templates/NOTES.txt
rename to helmchart/templates/NOTES.txt
diff --git a/helmcharts/templates/_helpers.tpl b/helmchart/templates/_helpers.tpl
similarity index 100%
rename from helmcharts/templates/_helpers.tpl
rename to helmchart/templates/_helpers.tpl
diff --git a/helmchart/templates/configmap.yaml b/helmchart/templates/configmap.yaml
new file mode 100644
index 0000000..8b7c01e
--- /dev/null
+++ b/helmchart/templates/configmap.yaml
@@ -0,0 +1,8 @@
+{{- if .Values.createConfigMap -}}
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: {{ .Release.Name }}-environment
+data:
+{{ toYaml .Values.environment | indent 2 }}
+{{- end }}
diff --git a/helmcharts/templates/deployment.yaml b/helmchart/templates/deployment.yaml
similarity index 94%
rename from helmcharts/templates/deployment.yaml
rename to helmchart/templates/deployment.yaml
index 6caea91..a14e632 100644
--- a/helmcharts/templates/deployment.yaml
+++ b/helmchart/templates/deployment.yaml
@@ -19,6 +19,10 @@ spec:
         app: {{ template "dacap.name" . }}
         release: {{ .Release.Name }}
     spec:
+{{- if .Values.dockerconfigjson }}
+      imagePullSecrets:
+      - name: registrypullsecret
+{{- end }}
       containers:
         - name: {{ .Chart.Name }}
           image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
diff --git a/helmcharts/templates/ingress.yaml b/helmchart/templates/ingress.yaml
similarity index 100%
rename from helmcharts/templates/ingress.yaml
rename to helmchart/templates/ingress.yaml
diff --git a/helmcharts/templates/pvc.yml b/helmchart/templates/pvc.yml
similarity index 100%
rename from helmcharts/templates/pvc.yml
rename to helmchart/templates/pvc.yml
diff --git a/helmchart/templates/secret.yaml b/helmchart/templates/secret.yaml
new file mode 100644
index 0000000..e81bf0f
--- /dev/null
+++ b/helmchart/templates/secret.yaml
@@ -0,0 +1,9 @@
+{{- if .Values.dockerconfigjson -}}
+apiVersion: v1
+kind: Secret
+metadata:
+ name: registrypullsecret
+data:
+ .dockerconfigjson: {{ .Values.dockerconfigjson }}
+type: kubernetes.io/dockerconfigjson
+{{- end -}}
\ No newline at end of file
diff --git a/helmcharts/templates/service.yaml b/helmchart/templates/service.yaml
similarity index 100%
rename from helmcharts/templates/service.yaml
rename to helmchart/templates/service.yaml
diff --git a/helmcharts/values.yaml b/helmchart/values.yaml
similarity index 98%
rename from helmcharts/values.yaml
rename to helmchart/values.yaml
index 5c42aa8..9cfe1d8 100644
--- a/helmcharts/values.yaml
+++ b/helmchart/values.yaml
@@ -9,6 +9,8 @@ image:
   tag: 1.0.5
   pullPolicy: IfNotPresent
 
+createConfigMap: true
+
 environment:
   autosave_interval: "60"
   cache_ttl: "600"
diff --git a/helmcharts/templates/configmap.yaml b/helmcharts/templates/configmap.yaml
deleted file mode 100644
index 36cb9c8..0000000
--- a/helmcharts/templates/configmap.yaml
+++ /dev/null
@@ -1,6 +0,0 @@
-apiVersion: v1
-kind: ConfigMap
-metadata:
-  name: {{ .Release.Name }}-environment
-data:
-{{ toYaml .Values.environment | indent 2 }}
\ No newline at end of file
-- 
GitLab