@Library('jenkins-shared-library') _
pipeline {
    agent {
        label 'jenkins-slave'
    }
    environment {
        DEBIAN_FRONTEND = 'noninteractive'
        DEFAULT_DEV_NAME = 'Opengnsys Team'
        DEFAULT_DEV_EMAIL = 'opengnsys@qindel.com'
    }
    options {
        skipDefaultCheckout()
    }
    parameters {
        string(name: 'DEV_NAME', defaultValue: '', description: 'Nombre del desarrollador')
        string(name: 'DEV_EMAIL', defaultValue: '', description: 'Email del desarrollador')
    }
    stages {
        stage('Prepare Workspace') {
            steps {
                script {
                    env.BUILD_DIR = "${WORKSPACE}/ogcore"
                    sh "mkdir -p ${env.BUILD_DIR}"
                }
            }
        }
        stage('Checkout') {
            steps {
                dir("${env.BUILD_DIR}") {
                    checkout scm
                }
            }
        }

        stage('Generate Changelog') {
            when {
                expression {
                    return env.TAG_NAME != null
                }
            }
            steps {
                script {
                    def devName = params.DEV_NAME ? params.DEV_NAME : env.DEFAULT_DEV_NAME
                    def devEmail = params.DEV_EMAIL ? params.DEV_EMAIL : env.DEFAULT_DEV_EMAIL

                    generateDebianChangelog(env.BUILD_DIR, devName, devEmail)
                }
            }
        }

        stage('Generate Changelog (Nightly)'){
            when {
                branch 'main'
            }
            steps {
                script {
                    def devName = params.DEV_NAME ? params.DEV_NAME : env.DEFAULT_DEV_NAME
                    def devEmail = params.DEV_EMAIL ? params.DEV_EMAIL : env.DEFAULT_DEV_EMAIL
                    generateDebianChangelog(env.BUILD_DIR, devName, devEmail,"nightly")
                }
            }
        }

        stage('Build') {
            steps {
                script {
                    construirPaquete(env.BUILD_DIR, "../artifacts", "172.17.8.68", "/var/tmp/opengnsys/debian-repo/ogcore")
                }
            }
        }
        stage ('Publish to Debian Repository') {
            when {
                expression {
                    return env.TAG_NAME != null
                }
            }
            agent { label 'debian-repo' }
            steps {
                script {
                    // Construir el patrón de versión esperado en el nombre del paquete
                    def versionPattern = "${env.TAG_NAME}-${env.BUILD_NUMBER}"
                    publicarEnAptly('/var/tmp/opengnsys/debian-repo/ogcore', 'opengnsys-devel', versionPattern)
                }
            }
        }
        stage ('Publish to Debian Repository (Nightly)') {
            when {
                branch 'main'
            }
            agent { label 'debian-repo' }
            steps {
               script {
                    // Construir el patrón de versión esperado en el nombre del paquete
                    def versionPattern = "-${env.BUILD_NUMBER}~nightly"
                    publicarEnAptly('/var/tmp/opengnsys/debian-repo/ogcore', 'nightly', versionPattern)
                }
            }
        }
    }
    post {
        success {
            script {
                // Solo lanzar cuando el build sea exitoso y en la rama main
                if (env.BRANCH_NAME == 'main') {
                    build job: 'Aptly publish nightly repository', 
                          wait: false,
                          parameters: [
                              string(name: 'TRIGGERED_BY', value: "${env.JOB_NAME}-${env.BUILD_NUMBER}")
                          ]
                }
            }

        }
        always {
            notifyBuildStatus('opengnsys@qindel.com')
        }
    }
}


