.prepare-deploy: &prepare-deploy
  - echo Deployment using "${ENV}"
  - ./.gitlab/ci/version_create.sh

.run-deploy: &run-deploy
  - echo "Copy project files"
  - sudo -u ${SYS_USER} /usr/bin/rsync -rlptogq -P --delete --exclude-from=.rsyncignore . ${ROOT_PATH}
  - echo "<<<<<<<<<<<<<<<<<<<<<"

.post-deploy: &post-deploy
  - echo "Run after deployment scripts"
  - sudo -u ${SYS_USER} ${K10_PHP_EX_PATH} ${ROOT_PATH}/artisan about
  - sudo -u ${SYS_USER} ${K10_PHP_EX_PATH} ${ROOT_PATH}/artisan cache:clear
  - sudo -u ${SYS_USER} ${K10_PHP_EX_PATH} ${ROOT_PATH}/artisan config:clear
  - sudo -u ${SYS_USER} ${K10_PHP_EX_PATH} ${ROOT_PATH}/vendor/jtl/migrations-lib/bin/console --config=${ROOT_PATH}/includes/migrations.php migrate
  - sudo -u ${SYS_USER} ${K10_PHP_EX_PATH} ${ROOT_PATH}/vendor/jtl/migrations-lib/bin/console --config=${ROOT_PATH}/includes/migrations-admin.php migrate
  - sudo /usr/bin/supervisorctl restart jtladmin_worker:*

.post-deploy-php-feature: &post-deploy-php-feature
  - sudo -u ${SYS_USER} ${K10_PHP_FEATURE_EX_PATH} ${ROOT_PATH}/vendor/jtl/migrations-lib/bin/console --config=${ROOT_PATH}/includes/migrations.php migrate
  - sudo -u ${SYS_USER} ${K10_PHP_FEATURE_EX_PATH} ${ROOT_PATH}/vendor/jtl/migrations-lib/bin/console --config=${ROOT_PATH}/includes/migrations-admin.php migrate
  - sudo /usr/bin/supervisorctl restart jtladmin_worker:*

stages:
  - build
  - test
  - deploy-dev
  - deploy-qas
  - deploy-staging
  - deploy-production

build-dependencies-dev:
  stage: build
  allow_failure: false
  script:
    - time COMPOSER_MEMORY_LIMIT=2G ${PHP_EX_PATH} ${COMPOSER_EX_PATH} install
  when: manual
  rules:
    - if: $CI_COMMIT_BRANCH != $IDEV_MASTER_BRANCH && $CI_COMMIT_TAG == null
  artifacts:
    paths:
      - vendor/
      - composer.lock
  tags:
    - JTLMultiRunner2

build-dependencies:
  stage: build
  allow_failure: false
  script:
    - time COMPOSER_MEMORY_LIMIT=2G ${PHP_EX_PATH} ${COMPOSER_EX_PATH} install --no-dev --optimize-autoloader
  when: manual
  rules:
    - if: $CI_COMMIT_BRANCH != $IDEV_MASTER_BRANCH
  artifacts:
    paths:
      - vendor/
  tags:
    - JTLMultiRunner2

composer-analysis:
  stage: test
  allow_failure: true
  rules:
    - if: $CI_COMMIT_BRANCH != $IDEV_MASTER_BRANCH && $CI_COMMIT_TAG == null
  script:
    - time ${PHP_EX_PATH} ./vendor/bin/security-checker security:check composer.lock
    - 'eval ${PHP_EX_PATH} ./vendor/bin/security-checker security:check composer.lock --format=json > security-check-report-"$(git show -s --format=%ct $CI_COMMIT_SHA)".json'
  artifacts:
    paths:
      - security-check-report-$(git show -s --format=%ct $CI_COMMIT_SHA).json
    expire_in: 1 days
  needs: ["build-dependencies-dev"]
  tags:
    - JTLMultiRunner2

code-analysis:
  stage: test
  allow_failure: true
  rules:
    - if: $CI_COMMIT_BRANCH != $IDEV_MASTER_BRANCH && $CI_COMMIT_TAG == null
  script:
    - time ${PHP_EX_PATH} vendor/bin/php-cs-fixer fix --dry-run
  needs: ["build-dependencies-dev"]
  tags:
    - JTLMultiRunner2

deploy-dev:
  stage: deploy-dev
  allow_failure: false
  needs: ["build-dependencies-dev"]
  rules:
    - if: $CI_COMMIT_BRANCH != $IDEV_MASTER_BRANCH && $CI_COMMIT_TAG == null
  variables:
    ENV: dev
    ROOT_PATH: ${DEV_WORK_DIR}
  environment:
    name: ${TARGET}_${SERVER}
    url: ${DEV_URL}
  before_script:
    - *prepare-deploy
  script:
    - *run-deploy
  after_script:
    - *post-deploy
  when: manual
  tags:
    - ${SERVER}
  parallel:
    matrix:
      - TARGET:
          - 'dev'
        SERVER: [ "k10-dev" ]

deploy-qas:
  stage: deploy-qas
  allow_failure: false
  needs: ["build-dependencies-dev"]
  rules:
    - if: $CI_COMMIT_BRANCH != $IDEV_MASTER_BRANCH && $CI_COMMIT_TAG == null
  variables:
    ENV: qas
    ROOT_PATH: ${QAS_WORK_DIR}
  environment:
    name: ${TARGET}_${SERVER}
    url: ${QAS_URL}
  before_script:
    - *prepare-deploy
  script:
    - *run-deploy
  after_script:
    - *post-deploy
  when: manual
  tags:
    - ${SERVER}
  parallel:
    matrix:
      - TARGET:
          - 'qas'
        SERVER: [ "k10-qas" ]

deploy-stage:
  stage: deploy-staging
  allow_failure: false
  needs: ["build-dependencies"]
  rules:
    - if: $CI_COMMIT_BRANCH != $IDEV_MASTER_BRANCH
  variables:
    ENV: staging
    ROOT_PATH: ${STAGING_WORK_DIR}
  environment:
    name: ${TARGET}_${SERVER}
    url: ${STAGING_URL}
  before_script:
    - *prepare-deploy
  script:
    - *run-deploy
  after_script:
    - *post-deploy
  when: manual
  tags:
    - ${SERVER}
  parallel:
    matrix:
      - TARGET:
          - 'staging'
        SERVER: [ "k10-stage" ]

deploy-prod:
  stage: deploy-production
  allow_failure: false
  needs: ["build-dependencies", "deploy-stage"]
  rules:
    - if: $CI_COMMIT_BRANCH != $IDEV_MASTER_BRANCH && $CI_COMMIT_TAG == $CI_COMMIT_REF_NAME
  variables:
    ENV: production
    ROOT_PATH: ${PROD_WORK_DIR}
  environment:
    name: ${TARGET}_${SERVER}
    url: ${PROD_URL}
  before_script:
    - *prepare-deploy
  script:
    - *run-deploy
  after_script:
    - *post-deploy
  when: manual
  tags:
    - ${SERVER}
  parallel:
    matrix:
      - TARGET:
          - 'production'
        SERVER: [ "k10-idev" ]
