#1.10.6
before_script:
  - cd ${CI_PROJECT_DIR}

variables:
  MYSQL_ROOT_PASSWORD: mysql
  MYSQL_DATABASE: wptest
  MYSQL_USER: mysql
  MYSQL_PASSWORD: mysql
  MYSQL_INNODB_LOG_BUFFER_SIZE: 32M
  PHP_ERROR_REPORTING: E_ALL
  COMPOSER_ALLOW_SUPERUSER: 1
  GIT_STRATEGY: fetch

cache:
  untracked: true
  paths:
    - vendor

stages:
  - build
  - tests
  - pre-deploy
  - deploy

.template: &job-test-template
  stage: tests
  dependencies:
    - build php
  coverage: '/^\s*Lines:\s*\d+.\d+\%/'

.template: &job-test-integration-template
  <<: *job-test-template
  services:
    - mysql
  script:
    - ls -l
    - php --version
    - composer phpunit-integration

.template: &job-deploy-template
  image: wpdesknet/amazon-svn-deploy
  stage: deploy
  dependencies:
    - build to deploy
  retry: 2
  when: manual
  only:
    - tags

build php:
  image: wpdesknet/phpunit-woocommerce:0-0
  stage: build
  artifacts:
    expire_in: 1 day
    name: "dev vendor"
    paths:
      - vendor/
  script:
    - ls /usr/local/bin
    - composer install --no-progress

code style test:
  <<: *job-test-template
  image: wpdesknet/phpunit-woocommerce:0-0
  allow_failure: true
  script:
    - ls -l
    - php --version
    - composer phpcs

unit test 0:
  <<: *job-test-template
  image: wpdesknet/phpunit-woocommerce:0-0
  script:
    - ls -l
    - php --version
    - composer phpunit-unit

integration test 0-0:
  <<: *job-test-integration-template
  image: wpdesknet/phpunit-woocommerce:0-0

integration test 1-1:
  <<: *job-test-integration-template
  image: wpdesknet/phpunit-woocommerce:1-1

integration test 2-2:
  <<: *job-test-integration-template
  image: wpdesknet/phpunit-woocommerce:2-2

integration test 3-3:
  <<: *job-test-integration-template
  image: wpdesknet/phpunit-woocommerce:3-3
  script:
    - php --version
    - phpunit --configuration phpunit-integration.xml

integration test 4-3:
  <<: *job-test-integration-template
  image: wpdesknet/phpunit-woocommerce:4-3
  script:
    - php --version
    - phpunit --configuration phpunit-integration.xml

apigen docs:
  image:
    name: wpdesknet/apigen
  stage: build
  artifacts:
    expire_in: 1 day
    name: "docs"
    paths:
      - docs/
  script:
    - ls -l
    - /app/vendor/bin/apigen generate
    - php /app/hooks-docs.php ${CI_PROJECT_DIR}
  only:
    - tags

pages:
  stage: deploy
  dependencies:
    - apigen docs
  script:
    - rm -rf public
    - mv docs/ public/
  artifacts:
    expire_in: 1 day
    paths:
      - public
  only:
    - tags

build to deploy:
  image: wpdesknet/phpunit-woocommerce:0-0
  stage: pre-deploy
  artifacts:
    expire_in: 1 month
    name: "production release"
    paths:
      - release
      - release.zip
  script:
    - php --version
    - ls -l
    - rm -rf ${CI_PROJECT_DIR}/release ${CI_PROJECT_DIR}/release.zip /tmp/release
    - mkdir /tmp/release
    - mkdir -p ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}
    - cp -rf ${CI_PROJECT_DIR}/* /tmp/release
    - cp -rf /tmp/release/* ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}
    - cd ${CI_PROJECT_DIR}/release/${CI_PROJECT_NAME}
    - composer install --no-dev --no-progress
    - rm -rf build-coverage release tests docs .git .editorconfig .gitignore .gitlab-ci.yml apigen.neon \
      composer.json composer.lock phpcs.xml.dist phpunit-integration.xml phpunit-unit.xml composer.phar wp-cli.phar
    - cd ../
    - zip -r -q ../release.zip ./
  only:
    - tags

deploy to shop:
  <<: *job-deploy-template
  script:
    - /tmp/deploy_shop.sh ${CI_PROJECT_NAME} release.zip ${CI_PROJECT_NAME}.zip
    - 'curl -X POST --data-urlencode "payload={\"text\": \"Projekt <https://gitlab.com/wpdesk/${CI_PROJECT_NAME}|${CI_PROJECT_NAME}> zdeployowany do sklepu w wersji ${CI_COMMIT_REF_NAME}\", }" https://hooks.slack.com/services/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'
  environment:
    name: wpdesk shop
    url: https://wpdeskplugin.s3.amazonaws.com/${CI_PROJECT_NAME}.zip

deploy to demo:
  <<: *job-deploy-template
  script:
    - /tmp/deploy_demo.sh release/${CI_PROJECT_NAME} ${CI_PROJECT_NAME}
    - 'curl -X POST --data-urlencode "payload={\"text\": \"Projekt <https://gitlab.com/wpdesk/${CI_PROJECT_NAME}|${CI_PROJECT_NAME}> zdeployowany do demo w wersji ${CI_COMMIT_REF_NAME}\", }" https://hooks.slack.com/services/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'
  environment:
    name: wpdesk demo
    url: https://demo.wpdesk.org

deploy to repository:
  <<: *job-deploy-template
  script:
    - rm -rf /tmp/svn-repository
    - mkdir /tmp/svn-repository
    - /tmp/deploy_repository.sh ${CI_PROJECT_NAME} release/${CI_PROJECT_NAME} /tmp/svn-repository
    - 'curl -X POST --data-urlencode "payload={\"text\": \"Projekt <https://gitlab.com/wpdesk/${CI_PROJECT_NAME}|${CI_PROJECT_NAME}> zdeployowany do repozytorium WP w wersji ${CI_COMMIT_REF_NAME}\", }" https://hooks.slack.com/services/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'
  environment:
    name: wordpress repository
    url: https://downloads.wordpress.org/plugin/${CI_PROJECT_NAME}.${CI_COMMIT_REF_NAME}.zip
