From a50720452433151e343523177ae12f557b5ac016 Mon Sep 17 00:00:00 2001
From: Jonas Zohren <git-pbkyr@jzohren.de>
Date: Sat, 25 Feb 2023 12:38:57 +0100
Subject: [PATCH] Deploy with CapRover

---
 .dockerignore      |  4 +++
 .gitlab-ci.yml     | 81 ++++++++++++++++++++++++++++++++++++++++------
 Dockerfile         | 11 +++++++
 captain-definition |  4 +++
 package-lock.json  |  4 ++-
 5 files changed, 93 insertions(+), 11 deletions(-)
 create mode 100644 .dockerignore
 create mode 100644 Dockerfile
 create mode 100644 captain-definition

diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..f023edf
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,4 @@
+/node_modules/
+/public/build/
+.DS_Store
+test-results.xml
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 94507ee..7e02572 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,15 +1,76 @@
-include:
-  - template: Security/License-Scanning.gitlab-ci.yml
+stages:
+  - "test"
+  - "build"
+  - "deploy"
 
-pages:
+test:
   needs: []
-  image: node:14-alpine
-  stage: deploy
+  stage: "test"
+  image: "node:18-alpine"
   script:
-    - npm install
-    - npm run build
+    - "npm install"
+    - "npm run test -- --parallel --reporter mocha-junit-reporter --reporter-options mochaFile=junit.xml"
+  artifacts:
+    when: always
+    reports:
+      junit:
+        - junit.xml
+
+build_docker_main:
+  needs: []
+  stage: "build"
+  image:
+    name: "gcr.io/kaniko-project/executor:debug"
+    entrypoint: [""]
+  script:
+    - "mkdir -p /kaniko/.docker"
+    - 'echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json'
+    - "/kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:commit-$CI_COMMIT_SHORT_SHA --destination $CI_REGISTRY_IMAGE:latest"
+  rules:
+    - if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH"
+
+build_docker_branches:
+  needs: []
+  stage: "build"
+  image:
+    name: "gcr.io/kaniko-project/executor:debug"
+    entrypoint: [""]
+  script:
+    - "mkdir -p /kaniko/.docker"
+    - 'echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json'
+    - "/kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:commit-$CI_COMMIT_SHORT_SHA --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-latest"
+  rules:
+    - if: "$CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH"
+
+pages:
+  needs:
+    - "test"
+  stage: "deploy"
+  image: "node:18-alpine"
+  script:
+    - "npm install"
+    - "npm run build"
   artifacts:
     paths:
-      - public
-  only:
-    - master
+      - "public"
+  rules:
+    - if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH"
+
+deploy_to_caprover:
+  needs:
+    - "test"
+    - "build_docker_main"
+  stage: "deploy"
+  image:
+    name: "caprover/cli-caprover:latest"
+    entrypoint: [""]
+  variables:
+    # We don't need the files, just tell CapRover to use the built docker image:
+    GIT_STRATEGY: none
+  script:
+    - "caprover deploy -h https://captain.$CAPROVER_URL -a $CAPROVER_APP --imageName $CI_REGISTRY_IMAGE:commit-$CI_COMMIT_SHORT_SHA"
+  environment:
+    name: "production"
+    url: "https://swk-dpll.oh14.dev/"
+  rules:
+    - if: "$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CAPROVER_URL && $CAPROVER_APP_TOKEN && $CAPROVER_APP"
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..618bf02
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,11 @@
+FROM node:18-alpine AS build
+
+WORKDIR /build
+COPY package.json package-lock.json /build/
+RUN npm install --ignore-scripts
+
+COPY . .
+RUN npm run build
+
+FROM pierrezemb/gostatic AS final
+COPY --from=build /build/public/ /srv/http/
\ No newline at end of file
diff --git a/captain-definition b/captain-definition
new file mode 100644
index 0000000..0922ea6
--- /dev/null
+++ b/captain-definition
@@ -0,0 +1,4 @@
+{
+  "schemaVersion" :2 ,
+  "dockerfilePath" : "Dockerfile"
+}
diff --git a/package-lock.json b/package-lock.json
index 2d31e5c..0878dae 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -4693,7 +4693,9 @@
       "resolved": "https://registry.npmjs.org/ts-pegjs/-/ts-pegjs-0.3.1.tgz",
       "integrity": "sha512-4FqLW/HdZ4KvKHUpnaf7yeZM2L/1Y9cHlQ0Z59bVNABPbMEFQnKAYs/3p0zMvvCWkY3fnjMyr/F7wKzZ//vHIw==",
       "dev": true,
-      "requires": {}
+      "requires": {
+        "pegjs": "^0.10.0"
+      }
     },
     "tslib": {
       "version": "2.2.0",
-- 
GitLab