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:
    - python3 -m venv ./venv
    - source ./venv/bin/activate
    - pip install --upgrade pip && pip install pip-tools
    - pip install -r ./backend/requirements.txt
  artifacts:
    paths:
      - ./venv/
    expire_in: 30 mins

#######################################################################################################################################
####                                                                                                                               ####
####                                               Testing code integration                                                        ####
####                                                                                                                               ####
#######################################################################################################################################

lint:
  stage: test
  before_script:
    - source ./venv/bin/activate
    - pip install pycodestyle
  script:
    - pycodestyle --config=./backend/setup.cnf ./backend


# 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
#   before_script:
#     - source ./venv/bin/activate
#   script:
#     - cd ./backend
#     - docker build -t app ./backend
#     - docker run --detach -p 8000:80 app --env-file ./backend/.env
#     - curl "http://localhost:8000/api/health"

#######################################################################################################################################
####                                                                                                                               ####
####                                                        Deploy                                                                 ####
####                                                                                                                               ####
#######################################################################################################################################

.deploy:
  image: node:14.6.0
  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)
    - if [ $IMAGE = "back" ] ; then
    - >
      ssh "eatfast@$DOMAIN"
      "cd /var/www/eatfast-website/backend &&
      git stash &&
      git checkout "$CI_COMMIT_BRANCH" &&
      git stash &&
      git pull &&
      docker-compose build &&
      docker-compose up -d &&
      exit" ; else
    - cd ./frontend
    - npm run build
    - scp -r build/ eatfast@"$DOMAIN":/var/www/eatfast-website/frontend ; fi


# deploy-back-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-back-prod:
  extends: .deploy
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: always
  variables:
    DOMAIN: eatfast.cs-campus.fr
    PRIVATE_KEY: "$SSH_PRIVATE_KEY"
    IMAGE: "back"


deploy-front-prod:
  extends: .deploy
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: always
  variables:
    DOMAIN: eatfast.cs-campus.fr
    PRIVATE_KEY: "$SSH_PRIVATE_KEY"
    IMAGE: "front"
