@Library('sphinx-lib@main') _

pipeline {
    agent {
        kubernetes {
            yaml libraryResource('templates/pod.yaml')
        }
    }
    stages {
        stage('Checkout') {
            steps {
                git(
                    url: 'https://github.com/spring-projects/spring-petclinic.git',
                    branch: 'main'
                )
            }
        }
        stage('Build') {
            steps {
                mavenBuild(goals: 'clean package -DskipTests')
            }
        }
        stage('Analyse') {
            steps {
                sonarScan('lab-demo')
            }
        }
        stage('Package') {
            steps {
                container('build') {
                    sh '''
                        tar -czf app.tar.gz target/*.jar
                        ls -lh app.tar.gz
                    '''
                }
            }
        }
        stage('Publish') {
            steps {
                publishArtifact(file: 'app.tar.gz', path: 'demo/1.0.0')
            }
        }
        stage('Deploy') {
            steps {
                deployToK3s(name: 'demo', image: 'nginx:latest', replicas: 2)
            }
        }
    }
    post {
        success {
            echo "Pipeline terminé avec succès ✅"
        }
        failure {
            echo "Pipeline en échec ❌"
        }
        cleanup {
            cleanWs()
        }
    }
}