image: itwadic/php7.4-apache2-git-ssh:latest

definitions:
  steps:
    - step: &php-syntax-check
        name: PHP Syntax Check
        script:
          - echo "Checking PHP syntax on changed files..."
          # Fetch the branch to compare against
          - git fetch origin $BITBUCKET_BRANCH
          # Find changed PHP files
          - git diff --name-only --diff-filter=ACMRT HEAD~1 HEAD > changed_files.txt

          # Run syntax check on changed files
          - |
            if [ -s changed_files.txt ]; then
            while read file; do
              # Check if the file has a .php extension
              if [[ $file == *.php ]]; then
                php -l "$file";
              fi
            done < changed_files.txt | tee /tmp/lint_results.log;

            if grep -q "Errors parsing" /tmp/lint_results.log; then
              echo "PHP syntax check failed.";

              # Send Slack notification
              curl -X POST -H 'Content-type: application/json' \
                --data '{"text":"🚨 *PHP Syntax Error* - The PHP syntax check has failed for the branch *'$BITBUCKET_BRANCH'*. \n *Commit Link:* https://bitbucket.org/scopetechnologies/polyscope-server/commits/'$BITBUCKET_COMMIT' \n *Build Link:* https://bitbucket.org/scopetechnologies/polyscope-server/pipelines/results/'$BITBUCKET_BUILD_NUMBER' "}' $SLACK_WEBHOOK_URL;

              exit 1;
            else
              echo "PHP syntax check passed.";
            fi
            else
              echo "No PHP files changed.";
            fi
    - step: &deploy-production
        name: Deploy on Production
        size: 4x
        runtime:
          cloud:
            atlassian-ip-ranges: true
        deployment: production
        script:
          - echo "Starting deployment to Production server..."
          - set -e  # Exit immediately if a command exits with a non-zero status
          - curl ifconfig.me
          # Capture errors and notify on failure
          - |
            trap 'curl -X POST -H "Content-type: application/json" \
            --data "{\"text\":\"<!here> <@UMETN1NJV> <@U05NCKQKHS8> \n 🚨 *Polyscope-Sever API Production Deployment Failed* 🚨 \n Branch Name: *'$BITBUCKET_BRANCH'* \n <!subteam^S048MESV5HR> please fix the pipeline. \n *Build Link:* https://bitbucket.org/scopetechnologies/polyscope-server/pipelines/results/'$BITBUCKET_BUILD_NUMBER' \"}" $SLACK_WEBHOOK_URL' ERR;
          - |
            ssh -o StrictHostKeyChecking=no $PROD_USER@$PROD_SERVER "
            cd /var/www/html/ && sudo git pull
            "
          - |
            # Send Slack notification on success
            curl -X POST -H 'Content-type: application/json' \
              --data '{"text":"<!here> <@UMETN1NJV> <@U05NCKQKHS8> \n ✅ *Polyscope-Sever API Production Deployment* ✅ \n Branch *'$BITBUCKET_BRANCH'* has been successfully deployed on Production. \n *Commit Link:* https://bitbucket.org/scopetechnologies/polyscope-server/commits/'$BITBUCKET_COMMIT' \n *Build Link:* https://bitbucket.org/scopetechnologies/polyscope-server/pipelines/results/'$BITBUCKET_BUILD_NUMBER' "}' $SLACK_WEBHOOK_URL;


pipelines:
  default:
    - step: *php-syntax-check
  branches:
    master:
      - step: *php-syntax-check
      - step: *deploy-production
