stages:
  - install_dependencies
  - test
  - sonar
  - dockerize

variables:
    VERSION: "3.7"
    SONARQUBE_URL: https://sonarqube.lam.fr

install_dependencies:
    image: jakzal/phpqa:php8.0
    stage: install_dependencies
    script:
        - cd server
        - composer install --ignore-platform-reqs
    cache:
        key: ${CI_COMMIT_REF_SLUG}
        paths:
            - server/vendor
        policy: pull-push

test:
    image: jakzal/phpqa:php8.0
    stage: test
    cache: 
        key: ${CI_COMMIT_REF_SLUG}
        paths:
            - server/vendor
        policy: pull
    script:
        - cd server
        - phpdbg 
            -qrr ./vendor/bin/phpunit 
            --bootstrap ./tests/bootstrap.php 
            --whitelist src 
            --coverage-clover ./coverage/clover.xml 
            --log-junit ./coverage/junit-logfile.xml 
            --coverage-text --colors=never ./tests
    artifacts:
        paths:
            - server/coverage

sonar_scanner:
    image: sonarsource/sonar-scanner-cli:latest
    stage: sonar
    script:
        - cd server
        - sonar-scanner 
            -Dsonar.projectKey=anis-next 
            -Dsonar.sources=src 
            -Dsonar.projectVersion=$VERSION 
            -Dsonar.host.url=$SONARQUBE_URL 
            -Dsonar.login=$SONAR_TOKEN_SERVER 
            -Dsonar.php.tests.reportPath=./coverage/junit-logfile.xml 
            -Dsonar.php.coverage.reportPaths=./coverage/clover.xml

dockerize:
    image: docker:stable
    stage: dockerize
    cache: 
        key: ${CI_COMMIT_REF_SLUG}
        paths:
            - server/vendor
        policy: pull
    dependencies: []
    script:
        - cd server
        - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
        - docker pull $CI_REGISTRY/anis/anis-next/server:latest || true
        - docker build --cache-from $CI_REGISTRY/anis/anis-next/server:latest -t $CI_REGISTRY/anis/anis-next/server:latest .
        - docker push $CI_REGISTRY/anis/anis-next/server:latest
