image: maven:3.3.9

clone:
  depth: full

definitions:
  caches:
    sonar: ~/.sonar
  services:
    mysql:
      image: mysql:5.7
      variables:
        MYSQL_ROOT_PASSWORD: 'root'
        # MYSQL_DATABASE: 'test_servicesapi'
        # MYSQL_USER: 'mysqlusr'
        # MYSQL_PASSWORD: 'mysqlpsw'
  steps:
#    - step: &analysis-step
#        name: SonarQube analysis
#        image: sonarsource/sonar-scanner-cli:5.0
#        service:
#          - docker
#       variables:
#          SONAR_HOST_URL: $SONAR_HOST_URL # Get the value from the repository/workspace variable.
#          SONAR_TOKEN: $SONAR_TOKEN # Get the value from the repository/workspace variable. You shouldn't set secret in clear text here.
#          DEBUG: "true"
#        script:
#          - mkdir -p /tmp/cacerts
#          - apk add --no-cache openssl
#          - echo -n | openssl s_client -connect pipelines.fischerdigital.lt:443  | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > /tmp/cacerts/cert.cert
#         - /usr/bin/entrypoint.sh
#          - cat .scannerwork/report-task.txt
#        artifacts:
#          - .scannerwork/report-task.txt

#    - step: &report-step
#        name: SonarQube quality gate
#        image: sonarsource/sonarqube-quality-gate:1.1.0
#        service:
#          - docker
#        variables:
#          SONAR_HOST_URL: $SONAR_HOST_URL # Get the value from the repository/workspace variable.
#          SONAR_TOKEN: $SONAR_TOKEN # Get the value from the repository/workspace variable. You shouldn't set secret in clear text here.
#         DEBUG: "true"
#        script:
#          - mkdir -p /sonarsource/sonarqube-scan
#         - cp .scannerwork/report-task.txt /sonarsource/sonarqube-scan/report-task.txt # Copy the report-task.txt from the previous step
#          - apk add --no-cache openssl
#          - echo -n | openssl s_client -connect pipelines.fischerdigital.lt:443  | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' >> /etc/ssl/certs/ca-certificates.crt # Add the certificate to the trusted certificates for cURL
#          - /usr/bin/check-quality-gate.sh

    - step: &deploy-staging
        name: 'Deploy to staging'
        deployment: staging
        script:
          - pipe: atlassian/ssh-run:0.5.0
            variables:
              SSH_USER: $SSH_LOCAL_TEST_USER
              SERVER: $SSH_LOCAL_SERVER
              PORT: $SSH_LOCAL_TEST_PORT
              COMMAND: |
                cd /var/www/html/staging-services-api && \
                git pull origin develop && \
                export SERVICE_API_FOLDER='staging-services-api' && \
                bash /var/www/html/staging-services-api/deploy.sh

    - step: &deploy-production
        name: 'Deploy to PRODUCTION'
        deployment: production
        script:
          - pipe: atlassian/ssh-run:0.5.0
            variables:
              SSH_USER: $SSH_IV_PROD_USER
              SERVER: $SSH_IV_PROD_SERVER
              PORT: $SSH_IV_PROD_PORT
              COMMAND: 'cd /home/svcuser/domains/api.fischerdigital.lt/public_html && git pull origin master && php artisan down && ~/composer.phar install && php artisan migrate --no-interaction --force && php artisan config:cache && php artisan up'

    - step: &run-tests
        name: 'Run tests'
        image: php:8.3
        script:
          - apt-get update && apt-get install -y unzip libzip-dev default-mysql-client libpng-dev libmagickwand-dev --no-install-recommends --force-yes
          - mysql -u root -proot -h 127.0.0.1 -e "CREATE DATABASE test_b2b"
          - mysql -u root -proot -h 127.0.0.1 -e "CREATE DATABASE test_servicesapi"
          - docker-php-ext-configure gd
          - docker-php-ext-install gd
          - pecl install imagick
          - docker-php-ext-enable imagick
          - docker-php-ext-install zip
          - docker-php-ext-enable zip
          - docker-php-ext-install sockets
          - docker-php-ext-enable sockets
          - docker-php-ext-install bcmath
          - docker-php-ext-enable bcmath
          - docker-php-ext-install pdo_mysql
          - docker-php-ext-enable pdo_mysql
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer --version
          - composer self-update --2
          - export COMPOSER_ALLOW_SUPERUSER=1
          - composer install
          - cp .env.testing.example .env.testing
          - php artisan key:generate --env=testing
          - sleep 5
          - php artisan test
        services:
          - mysql

options:
  size: 4x
  runtime:
    cloud:
      atlassian-ip-ranges: true

pipelines:
  pull-requests:
    '**':
#      - step: *analysis-step
#      - step: *report-step
      - step: *run-tests

  branches:
    '{develop}':
      - step: *run-tests
      - step: *deploy-staging

    '{master}':
      - step: *run-tests
      - step: *deploy-production
