From 6ce11ec2ebb3d644f0ed64d1ce6e36bcd4de3f9a Mon Sep 17 00:00:00 2001
From: Thibault Debatty <thibault.debatty@gmail.com>
Date: Fri, 22 Dec 2023 18:59:36 +0100
Subject: [PATCH] deploy

---
 .gitlab-ci.yml      | 24 ++++++++++++++++++++++++
 Dockerfile          |  2 +-
 docker-compose.tmpl | 41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 1 deletion(-)
 create mode 100644 docker-compose.tmpl

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index de45de6..f57fb5f 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -62,3 +62,27 @@ build:
     - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
     - docker push $CI_REGISTRY_IMAGE:latest
 
+deploy:
+  stage: deploy
+  image: alpine
+  before_script:
+    # install envsubst and ssh-add
+    - apk add gettext openssh-client
+  script:
+    # create the new docker-compose.yml
+    - envsubst < docker-compose.tmpl > docker-compose.yml
+    # start ssh-agent and import ssh private key
+    - eval `ssh-agent`
+    - ssh-add <(echo "$SSH_PRIVATE_KEY")
+    # add server to list of known hosts
+    - mkdir -p ~/.ssh
+    - chmod 700 ~/.ssh
+    - touch ~/.ssh/known_hosts
+    - chmod 600 ~/.ssh/known_hosts
+    - echo $SSH_HOST_KEY >> ~/.ssh/known_hosts
+    # upload docker-compose to the server
+    - scp docker-compose.yml monitoring@$SERVER:/home/monitoring/
+    # docker login and restart services
+    - ssh monitoring@$SERVER "cd /home/monitoring; 
+        docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY;
+        docker compose up -d"
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
index f9db645..010f4a2 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -5,7 +5,7 @@
 
 #### Step 1 : composer
 
-FROM cylab/php74 AS composer
+FROM cylab/php:7.4 AS composer
 
 COPY . /var/www/html
 WORKDIR /var/www/html
diff --git a/docker-compose.tmpl b/docker-compose.tmpl
new file mode 100644
index 0000000..f31b644
--- /dev/null
+++ b/docker-compose.tmpl
@@ -0,0 +1,41 @@
+#
+# docker-compose.tmpl
+# https://cylab.be/blog/229/continuous-deployment-with-gitlab-and-docker
+# $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
+#
+
+version: "3.7"
+
+services:
+  web:
+    image: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
+    depends_on:
+      - redis
+      - mysql
+    ports:
+      - "80"
+    volumes:
+      - ./volumes/web:/var/www/html/storage
+    restart: "unless-stopped"
+    environment:
+      WAIT_HOSTS: mysql:3306
+    env_file: env
+    labels:
+      - traefik.http.routers.amc.rule=Host(`monitoring.cylab.be`)
+      - traefik.http.routers.amc.tls=true
+      - traefik.http.routers.amc.tls.certresolver=letsencrypt
+
+  redis:
+    image: redis:4-alpine
+    volumes:
+      - ./volumes/redis:/data
+    restart: "unless-stopped"
+
+  mysql:
+    image: mysql:5.7
+    volumes:
+      - ./volumes/mysql:/var/lib/mysql
+    environment:
+      MYSQL_ROOT_PASSWORD: root
+      MYSQL_DATABASE: laravel
+    restart: "unless-stopped"
-- 
GitLab