pipeline {
    agent {
        kubernetes {
            label 'jenkins-agent'
            namespace 'devops-tools'
        }
    }
    tools {nodejs "node18"}
    stages {
        stage('Install') {
            steps {
                echo 'Install Dependencies..'
                sh 'npm install'
            }
        }
        // Commented for jenkins testing
        // stage('Test') {
        //     steps {
        //         echo 'Testing..'
        //         sh 'npm run test'
        //     }
        // }
        stage('Build') {
            steps {
                echo 'Building..'
                sh 'npm run build'
            }
        }
        stage('Publish') {
            steps {
                echo 'Publish..'
                sh "npm publish"
            }
}
    }

 
    post {
        success {
            script {
                def gitAuthor = sh(returnStdout: true, script: 'git log -1 --pretty=format:%an').trim()
                discordSend description: "Git Commit: ${env.GIT_COMMIT}\nGit Branch: ${env.GIT_BRANCH}\nGit URL: ${env.GIT_URL}\nGit Author: ${gitAuthor}", footer: '', image: '', link: env.BUILD_URL, result: 'SUCCESS', scmWebUrl: '', thumbnail: '', title: "${env.JOB_NAME} Build #${env.BUILD_NUMBER} Success", webhookURL: 'https://discord.com/api/webhooks/1158914945300307988/9NMxvuI4OSHlxUABQoXduqPmCwwXIhe49agGU0mKjfoWtF0Eq0rew6BEdY5Z0QyZeC-g'
            }
        }

        failure {
            script {
                def gitAuthor = sh(returnStdout: true, script: 'git log -1 --pretty=format:%an').trim()
                discordSend description: "Git Commit: ${env.GIT_COMMIT}\nGit Branch: ${env.GIT_BRANCH}\nGit URL: ${env.GIT_URL}\nGit Author: ${gitAuthor}", footer: '', image: '', link: env.BUILD_URL, result: 'FAILURE', scmWebUrl: '', thumbnail: '', title: "${env.JOB_NAME} Build #${env.BUILD_NUMBER} Failed", webhookURL: 'https://discord.com/api/webhooks/1158914945300307988/9NMxvuI4OSHlxUABQoXduqPmCwwXIhe49agGU0mKjfoWtF0Eq0rew6BEdY5Z0QyZeC-g'
            }
        }
    } 

}
