pipeline {
    agent {
        kubernetes {
            label 'jenkins-agent'
            namespace 'devops-tools'
        }
    }
    tools {nodejs "node18"}
    stages {
        stage('Install') {
            steps {
                echo 'Install Dependencies..'
                sh 'npm install'
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
                // sh 'npm run test'
            }
        }
        stage('Build') {
            steps {
                echo 'Building..'
                sh 'npm run build'
            }
        }
        stage('Publish') {
            steps {
                echo 'Publish..'
                withCredentials([
                    usernamePassword(credentialsId: 'npm-auth-token', usernameVariable: 'NPM_USERNAME', passwordVariable: 'AUTH_TOKEN')
                ]) {
                sh "npm set //registry.npmjs.org/:_authToken=$AUTH_TOKEN"
                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/1166358157166125076/AquDPsQ6L5piq_TfiSfLGOujmVqgHhxfK8yVEfJZrWfhgT6g-j5gJ3xhuXGFZUjRhhlo'
            }
        }

        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/1166358157166125076/AquDPsQ6L5piq_TfiSfLGOujmVqgHhxfK8yVEfJZrWfhgT6g-j5gJ3xhuXGFZUjRhhlo'
            }
        }
    } 

}
