// ERPNext → Coolify production CI (Forgejo: epistemophiliac/erpnext)
pipeline {
  agent any

  options {
    timestamps()
    disableConcurrentBuilds()
    buildDiscarder(logRotator(numToKeepStr: '20'))
  }

  stages {
    stage('Verify') {
      steps {
        sh '''
          echo "=== ERPNext Coolify CI ==="
          echo "commit: $(git rev-parse --short HEAD)"
          echo "branch: ${BRANCH_NAME:-main}"
          test -f Jenkinsfile
          test -f docker-compose.yml
          test -f example.env
        '''
      }
    }

    stage('Production readiness') {
      steps {
        sh '''
          chmod +x scripts/ci/*.sh
          bash scripts/ci/ci-readiness.sh .
          bash scripts/ci/validate-docker-compose.sh .
        '''
      }
    }

    stage('Bootstrap Docker tools') {
      steps {
        sh 'bash scripts/ci/jenkins-bootstrap.sh'
      }
    }

    stage('Compose validate') {
      steps {
        sh 'bash scripts/ci/jenkins-compose-validate.sh'
      }
    }

    stage('Verify ERPNext image') {
      steps {
        sh 'bash scripts/ci/jenkins-pull-image.sh'
      }
    }
  }

  post {
    success {
      archiveArtifacts artifacts: 'dist/docker-compose.coolify.yml', fingerprint: true, onlyIfSuccessful: true
      echo 'CI passed — safe to deploy docker-compose.yml on Coolify (set DB_PASSWORD, SITE_NAME, ADMIN_PASSWORD).'
    }
    failure {
      echo 'CI failed — do not deploy to Coolify until this build is green.'
    }
  }
}
