From 9d16851713f7146f448cf30eb8122ff1b00c025d Mon Sep 17 00:00:00 2001
From: Antoine Gaudron-desjardins <antoine.gaudrondesjardins@student-cs.fr>
Date: Tue, 5 Jul 2022 21:01:02 +0200
Subject: [PATCH] ci

---
 backend/.gitlab-ci.yml     | 188 +++++++++++++++++++++++++++++++++++++
 backend/docker-compose.yml |   2 +-
 2 files changed, 189 insertions(+), 1 deletion(-)
 create mode 100644 backend/.gitlab-ci.yml

diff --git a/backend/.gitlab-ci.yml b/backend/.gitlab-ci.yml
new file mode 100644
index 0000000..1715a7b
--- /dev/null
+++ b/backend/.gitlab-ci.yml
@@ -0,0 +1,188 @@
+image: python:3.9
+
+variables:
+  MYSQL_DATABASE: $MYSQL_DATABASE
+  MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD
+  MYSQL_USER: $MYSQL_USER
+  MYSQL_PASSWORD: $MYSQL_PASSWORD
+  MYSQL_DATABASE: $MYSQL_DATABASE
+
+services:
+  - name: mysql:latest
+    command: ["mysqld", "--authentication-policy=mysql_native_password"]
+    alias: mysql
+
+# services:
+#   - name: mysql:latest
+#     command: ["mysqld", "--authentication-policy=mysql_native_password"]
+#     alias: mysql
+
+# variables:
+#   MYSQL_DATABASE: $MYSQL_DATABASE
+#   MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD
+
+# Host: mysql
+# User: $MYSQL_USER
+# Password: $MYSQL_PASSWORD
+# Database: $MYSQL_DATABASE
+
+# cache:
+#   paths:
+#     - .cache/pip
+#     - venv/
+    # - env/
+
+# before_script:
+  # - python --version  # For debugging
+  # - pip install venv
+  # - python3 -m venv /venv
+  # - source /venv/bin/activate
+
+stages:
+  - build
+  - test
+  - deploy
+
+workflow:
+  rules:
+    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
+    - if: $CI_COMMIT_BRANCH
+
+# include:
+#   - template: 'Code-Quality.gitlab-ci.yml'
+
+#######################################################################################################################################
+####                                                                                                                               ####
+####                                                 Install dependencies                                                          ####
+####                                                                                                                               ####
+#######################################################################################################################################
+
+install:
+  stage: build
+  script:
+    - pip install venv
+    - python3 -m venv ./venv
+    - source ./venv/bin/activate
+    - pip install --upgrade pip && pip install pip-tools
+    - pip install -r requirements.txt
+  artifacts:
+    paths:
+      - .venv/
+    expire_in: 30 mins
+
+#######################################################################################################################################
+####                                                                                                                               ####
+####                                               Testing code integration                                                        ####
+####                                                                                                                               ####
+#######################################################################################################################################
+
+lint:
+  stage: test
+  allow_failure: false
+  before_script:
+    - source .venv/bin/activate
+    - pip install pylint
+  script:
+    - pylint --recursive=y ./
+
+
+format:
+  stage: test
+  needs: lint
+  before_script:
+    - source .venv/bin/activate
+    - pip install autopep8
+  script:
+    - autopep8 --in-place --recursive .
+
+
+test:
+  stage: test
+  variables:
+    MYSQL_DATABASE: $MYSQL_DATABASE
+    MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD
+    MYSQL_DATABASE: $MYSQL_DATABASE
+    MYSQL_USER: $MYSQL_USER
+    MYSQL_PASSWORD: $MYSQL_PASSWORD
+    DB_HOST: mysql
+    DB_PORT: 3306
+    WEB_ROOT: http://localhost:3000
+    # SECRET_KEY: $SECRET_KEY
+    # TEST: "TRUE"
+    # STATIC_ROOT: "./"
+    # DJANGO_DEBUG: "FALSE"
+    # ALLOWED_HOSTS: ""
+    # DB_ENGINE: $ENGINE
+    # DB_NAME: $POSTGRES_DB
+    # DB_USER: $POSTGRES_USER
+    # DB_PASSWORD: $POSTGRES_PASSWORD
+    # DB_HOST: $POSTGRES_DB_HOST
+    # DB_PORT: 5432
+  before_script:
+    - source .venv/bin/activate
+  script:
+    - python -m uvicorn main:app --port=80 --host 0.0.0.0
+    - curl "http://localhost/api/health"
+
+#######################################################################################################################################
+####                                                                                                                               ####
+####                                                        Deploy                                                                 ####
+####                                                                                                                               ####
+#######################################################################################################################################
+
+.deploy:
+  stage: deploy
+  script:
+    # Install ssh-agent if not already installed, it is required by Docker.
+    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
+
+    # Run ssh-agent (inside the build environment)
+    - eval $(ssh-agent -s)
+
+    # Add the SSH key stored in PRIVATE_KEY variable to the agent store
+    - ssh-add <(echo "$PRIVATE_KEY")
+
+    # For Docker builds disable host key checking. Be aware that by adding that
+    # you are suspectible to man-in-the-middle attacks.
+    # WARNING: Use this only with the Docker executor, if you use it with shell
+    # you will overwrite your user's SSH config.
+    - mkdir -p ~/.ssh
+    - chmod 700 ~/.ssh
+    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
+    # In order to properly check the server's host key, assuming you created the
+    # SSH_SERVER_HOSTKEYS variable previously, uncomment the following two lines
+    # instead.
+    # - mkdir -p ~/.ssh
+    # - '[[ -f /.dockerenv ]] && echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts'
+
+    # Get build job ID from file in artifact
+    # - job_id=$(cat job_id)
+    - >
+      ssh "eatfast@$DOMAIN"
+      "cd /var/www/backend &&
+      git stash &&
+      git checkout "$CI_COMMIT_BRANCH" &&
+      git stash &&
+      git pull &&
+      docker-compose build &&
+      docker-compose up -d &&
+      exit"
+
+# deploy-staging:
+#   extends: .deploy
+#   rules:
+#     - if: $CI_COMMIT_BRANCH == $STAGING_BRANCH
+#       when: always
+#   variables:
+#     DOMAIN: nofist.test.viarezo.fr
+#     PRIVATE_KEY: "$SSH_PRIVATE_KEY_STAGING"
+
+
+deploy-prod:
+  extends: .deploy
+  rules:
+    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
+      when: always
+  variables:
+    DOMAIN: morbiustvplus.cs-campus.fr
+    PRIVATE_KEY: "$SSH_PRIVATE_KEY"
diff --git a/backend/docker-compose.yml b/backend/docker-compose.yml
index 76308ff..514c904 100644
--- a/backend/docker-compose.yml
+++ b/backend/docker-compose.yml
@@ -2,7 +2,7 @@ version: "3.3"
 
 services:
   db:
-    image: mysql
+    image: mysql:latest
     container_name: "db"
     restart: always
     env_file: .env
-- 
GitLab