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


stages:
  - install
  - test
  - deploy

workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: $CI_COMMIT_BRANCH

cache:
  paths:
    - venv/
    - frontend/node_modules/

# include:
#   - template: 'Code-Quality.gitlab-ci.yml'

#######################################################################################################################################
####                                                                                                                               ####
####                                                 Install dependencies                                                          ####
####                                                                                                                               ####
#######################################################################################################################################

install-npm-packages:
  image: node:14.6.0
  stage: install
  script:
    - cd ./frontend
    - npm ci
  artifacts:
    paths:
      - frontend/node_modules/
    expire_in: 30 mins

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

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


lint-front:
  image: node:14.6.0
  stage: test
  allow_failure: true
  script:
    - cd frontend/
    - npm run lint
  dependencies:
    - install-npm-packages


# 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 pull &&
      git checkout "$CI_COMMIT_BRANCH" &&
      git stash &&
      git pull &&
      docker-compose build &&
      docker-compose up -d &&
      exit" ; else
    - cd ./frontend
    - echo 'REACT_APP_BASE_URL_BACK="https://$DOMAIN/api"' > .env;
    - echo 'REACT_APP_BASE_URL_FRONT="https://$DOMAIN"' >> .env;
    - 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.viarezo.fr
    PRIVATE_KEY: "$SSH_PRIVATE_KEY"
    IMAGE: "back"
  dependencies: []


deploy-front-prod:
  extends: .deploy
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: always
  variables:
    DOMAIN: eatfast.viarezo.fr
    PRIVATE_KEY: "$SSH_PRIVATE_KEY"
    IMAGE: "front"
  dependencies:
    - install-npm-packages


deploy-back-staging:
  extends: .deploy
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: never
    - if: $CI_COMMIT_BRANCH
      when: always
  variables:
    DOMAIN: eatfast.test.viarezo.fr
    PRIVATE_KEY: "$SSH_PRIVATE_KEY_STAGING"
    IMAGE: "back"
  dependencies: []


deploy-front-staging:
  extends: .deploy
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: never
    - if: $CI_COMMIT_BRANCH
      when: always
  variables:
    DOMAIN: eatfast.test.viarezo.fr
    PRIVATE_KEY: "$SSH_PRIVATE_KEY_STAGING"
    IMAGE: "front"
  dependencies:
    - install-npm-packages
