image: node:11.2.0

stages:
  - backup
  - deploy

before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - echo "$GITLAB_SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - ssh-keyscan $SSH_HOST >> ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts

.backup_files:
  stage: backup
  script:
    - ssh $SSH_USER@$SSH_HOST "echo files backup ongoing ..."
    - ssh $SSH_USER@$SSH_HOST "rm -rf backup/files/$ENV"
    - ssh $SSH_USER@$SSH_HOST "sleep 10"
    - ssh $SSH_USER@$SSH_HOST "rsync -av --exclude='$ENV/vendor/*' --exclude='$ENV/var/log/*' --exclude='$ENV/var/cache/*' --exclude='$ENV/var/sessions/*' --exclude='$ENV/node_modules/*' $ENV backup/files"
    - ssh $SSH_USER@$SSH_HOST "echo files backup done ..."

.backup_database:
  stage: backup
  script:
    - ssh $SSH_USER@$SSH_HOST "echo database backup ongoing ..."
    # database backup script
    - ssh $SSH_USER@$SSH_HOST "cd backup/database/$ENV && mysqldump --host=$DB_HOST --user=$DB_USER --password=$DB_PASS $DB_NAME > $ENV.sql"
    - ssh $SSH_USER@$SSH_HOST "echo database backup done ..."

.deploy:
  stage: deploy
  script:
    # Get distant env file before yarn, then remove it
    - scp $SSH_USER@$SSH_HOST:$ENV/.env ./.env
    - yarn install
    - yarn encore production
    - ssh $SSH_USER@$SSH_HOST "echo deploy ..."
    - ssh $SSH_USER@$SSH_HOST "cd $ENV && rm -rf assets bin config core src templates tests translations vendor composer.json composer.lock symfony.lock yarn.lock package.json webpack.config.js node_modules public/build"
    # Remove .env and node_modules before transfering files
    - rm -rf ./node_modules ./.env
    - scp -r ./* $SSH_USER@$SSH_HOST:$ENV
    # Composer
    - ssh $SSH_USER@$SSH_HOST "cd $ENV && curl -sS https://getcomposer.org/installer | /usr/local/php7.2/bin/php"
    - ssh $SSH_USER@$SSH_HOST "cd $ENV && /usr/local/php7.2/bin/php composer.phar install"
    # DB update
    - ssh $SSH_USER@$SSH_HOST "cd $ENV && /usr/local/php7.2/bin/php bin/console doctrine:schema:update --force"
    # Cache clear
    - ssh $SSH_USER@$SSH_HOST "cd $ENV && /usr/local/php7.2/bin/php bin/console cache:clear --no-warmup --env=prod"



job_staging_backup_files:
  only:
    - develop
  extends: .backup_files
  variables:
    ENV: 'staging'

job_staging_backup_database:
  only:
    - develop
  extends: .backup_database
  variables:
    ENV: 'staging'
    DB_HOST: $STAGING_DB_HOST
    DB_USER: $STAGING_DB_USER
    DB_PASS: $STAGING_DB_PASS
    DB_NAME: $STAGING_DB_NAME
    
job_staging_deploy:
  only:
    - develop
  extends: .deploy
  variables:
    ENV: 'staging'


job_prod_backup_files:
  only:
    - master
  extends: .backup_files
  variables:
    ENV: 'prod'

job_prod_backup_database:
  only:
    - master
  extends: .backup_database
  variables:
    ENV: 'prod'
    DB_HOST: $PROD_DB_HOST
    DB_USER: $PROD_DB_USER
    DB_PASS: $PROD_DB_PASS
    DB_NAME: $PROD_DB_NAME

job_prod_deploy:
  only:
    - master
  extends: .deploy
  variables:
    ENV: 'prod'
