From 1b8bc6655d354491e55167f1c7ae526777110a37 Mon Sep 17 00:00:00 2001
From: Tom Teichler <tom.teichler@teckids.org>
Date: Tue, 9 Apr 2024 23:44:50 +0200
Subject: [PATCH] Update ci config

---
 .gitlab-ci.yml                            | 52 ++++++++++++++++++++---
 deploy/overlays/review/kustomization.yaml |  2 +-
 2 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index ddc27013..41e265ab 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -35,14 +35,56 @@ docker build:
     paths:
       - DIGESTFILE
 
-deploy docker image:
+.deploy:
   stage: deploy
+  image: line/kubectl-kustomize:latest
   tags:
     - teckids-trusted
-  image: line/kubectl-kustomize:latest
-  script:
+  before_script:
     - echo $KUBECONFIG | base64 -d > /tmp/kubeconfig
+
+deploy production:
+  extends: .deploy
+  environment:
+    name: production
+    url: https://teckids.org
+  script:
     - cd deploy/overlays/production
-    - kustomize edit set image registry.edugit.org/teckids/team-pr/teckids.org:$(cat ../../../DIGESTFILE)
-    - kustomize build | envsubst >output.yaml
+    - kustomize edit set image registry.edugit.org/teckids/team-pr/teckids.org:$CI_COMMIT_REF_NAME@$(cat ../../../DIGESTFILE)
+    - kustomize build | envsubst > output.yaml
     - kubectl --kubeconfig=/tmp/kubeconfig apply -f output.yaml
+  only:
+    - master
+
+deploy review:
+  extends: .deploy
+  script: deploy_review
+  environment:
+    name: review/$CI_COMMIT_REF_SLUG
+    url: https://$CI_COMMIT_REF_SLUG.review.teckids.org
+    on_stop: delete review
+    auto_stop_in: 3 days
+  only:
+    - merge_requests
+  when: manual
+  script:
+    - cd deploy/overlays/review
+    - kustomize edit set image registry.edugit.org/teckids/team-pr/teckids.org:$CI_COMMIT_REF_NAME@$(cat ../../../DIGESTFILE)
+    - kustomize build | envsubst > output.yaml
+    - kubectl --kubeconfig=/tmp/kubeconfig apply -f output.yaml
+
+delete review:
+  extends: .deploy
+  script: delete_review
+  environment:
+    name: review/$CI_COMMIT_REF_SLUG
+    action: stop
+  before_script:
+    - echo $KUBECONFIG | base64 -d > /tmp/kubeconfig
+    - cd deploy/overlays/review
+    - kustomize build | envsubst > output.yaml
+  script:
+    - kubectl --kubeconfig=/tmp/kubeconfig delete -f output.yaml
+  when: manual
+  only:
+    - merge_requests
diff --git a/deploy/overlays/review/kustomization.yaml b/deploy/overlays/review/kustomization.yaml
index 89495c29..5ee2add9 100644
--- a/deploy/overlays/review/kustomization.yaml
+++ b/deploy/overlays/review/kustomization.yaml
@@ -6,7 +6,7 @@ commonAnnotations:
   source-repository: https://edugit.org/Teckids/team-pr/teckids.org
 commonLabels:
   app: teckids-website
-  environment: production
+  environment: ${CI_COMMIT_REF_SLUG}
 namespace: teckids-website
 patches:
 - target:
-- 
GitLab