stages:
  - lint
  - build
  - test
  - publish

variables:
  ESLINT_PATHS: js plugins
  REGISTRY_PROJECT: cthulhoo

include:
  - project: 'ci/ci-templates'
    ref: master
    file: .ci-build-docker-kaniko.yml
  - project: 'ci/ci-templates'
    ref: master
    file: .ci-lint-common.yml
  - project: 'ci/ci-templates'
    ref: master
    file: .ci-integration-test.yml
  - project: 'ci/ci-templates'
    ref: master
    file: .ci-update-helm-imagetag.yml

phpunit:
  extends: .phpunit
  variables:
    PHPUNIT_ARGS: --exclude integration --coverage-filter classes --coverage-filter include

eslint:
  extends: .eslint

phpstan:
  extends: .phpstan

ttrss-web-nginx:branch:
  extends: .build-branch
  variables:
    DOCKERFILE: ${CI_PROJECT_DIR}/.docker/web-nginx/Dockerfile

ttrss-fpm-pgsql-static:branch:
  extends: .build-branch
  variables:
    DOCKERFILE: ${CI_PROJECT_DIR}/.docker/app/Dockerfile

ttrss-web-nginx:
  extends: .build-master-commit-only
  variables:
    DOCKERFILE: ${CI_PROJECT_DIR}/.docker/web-nginx/Dockerfile

ttrss-fpm-pgsql-static:
  extends: .build-master-commit-only
  variables:
    DOCKERFILE: ${CI_PROJECT_DIR}/.docker/app/Dockerfile

phpdoc:
  image: ${PHP_IMAGE}
  stage: publish
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $PHPDOC_DEPLOY_SSH_KEY != null
  script:
    - php83 /phpDocumentor.phar -d classes -d include -t phpdoc --visibility=public
    - mkdir -p ~/.ssh &&
        cp ${PHPDOC_DEPLOY_SSH_KEY} ~/.ssh/id_ed25519 &&
        chmod 0600 ~/.ssh/id_ed25519
    - rsync -av -e 'ssh -o StrictHostKeyChecking=no' phpdoc/ ${PHPDOC_DEPLOY_HOST}:phpdoc/

phpunit-integration:
  image: ${PHP_IMAGE}
  variables:
    TEST_HELM_REPO: oci://registry.fakecake.org/infra/helm-charts/tt-rss
  extends: .integration-test
  script:
    - export K8S_NAMESPACE=$(kubectl get pods -o=custom-columns=NS:.metadata.namespace | tail -1)
    - export API_URL="http://tt-rss-${CI_COMMIT_SHORT_SHA}-app.$K8S_NAMESPACE.svc.cluster.local/tt-rss/api/"
    - export TTRSS_DB_HOST=tt-rss-${CI_COMMIT_SHORT_SHA}-app.$K8S_NAMESPACE.svc.cluster.local
    - export TTRSS_DB_USER=postgres
    - export TTRSS_DB_NAME=postgres
    - export TTRSS_DB_PASS=password
    - php83 vendor/bin/phpunit --group integration --do-not-cache-result --log-junit phpunit-report.xml --coverage-cobertura phpunit-coverage.xml --coverage-text --colors=never
  artifacts:
    when: always
    reports:
      junit: phpunit-report.xml
      coverage_report:
        coverage_format: cobertura
        path: phpunit-coverage.xml
  coverage: '/^\s*Lines:\s*\d+.\d+\%/'

selenium:
  image: ${SELENIUM_IMAGE}
  variables:
    TEST_HELM_REPO: oci://registry.fakecake.org/infra/helm-charts/tt-rss
    SELENIUM_GRID_ENDPOINT: http://selenium-hub.selenium-grid.svc.cluster.local:4444/wd/hub
  extends: .integration-test
  script:
    - export K8S_NAMESPACE=$(kubectl get pods -o=custom-columns=NS:.metadata.namespace | tail -1)
    - python3 tests/integration/selenium_test.py
  needs:
    - job: phpunit-integration
  artifacts:
    when: always
    reports:
      junit: selenium-report.xml

ttrss-web-nginx:publish:
  stage: publish
  extends: .build-master
  variables:
    DOCKERFILE: ${CI_PROJECT_DIR}/.docker/web-nginx/Dockerfile

ttrss-fpm-pgsql-static:publish:
  stage: publish
  extends: .build-master
  variables:
    DOCKERFILE: ${CI_PROJECT_DIR}/.docker/app/Dockerfile

update-demo:
  extends: .update-helm-imagetag
  variables:
    CHART_REPO: gitlab.fakecake.org/git/helm-charts/tt-rss.git
    CHART_VALUES: values-demo.yaml
    ACCESS_TOKEN: ${DEMO_HELM_TOKEN}
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $DEMO_HELM_TOKEN != null
  stage: publish

update-prod:
  extends: .update-helm-imagetag
  variables:
    CHART_REPO: gitlab.fakecake.org/git/helm-charts/tt-rss-prod.git
    CHART_VALUES: values-prod.yaml
    ACCESS_TOKEN: ${PROD_HELM_TOKEN}
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $PROD_HELM_TOKEN != null
  stage: publish
