pipeline {
 //agent any     //ini untuk seluruh pipeline
  agent {label 'docker'}  // untuk menentukan label yang akan dpakai

  stages {
        stage('checkout code') {
          steps {
            script {
                catchError() {
                  checkout([$class: 'GitSCM', branches: [[name: '$gitlabBranch']],
                  extensions: [[$class: 'WipeWorkspace']], submoduleCfg: [],
                  userRemoteConfigs: [[
                      credentialsId: 'gitpas_eko',
                //      url: 'http://sdp.ditjenpas.go.id/gitpas/ekobp/app-js.git'    ini jika berdasarkan repository
                      url: '$gitlabSourceRepoHttpUrl'     // variabel ini dapat digunakan jika sudah dibuat variabel sebelumnya dan sudah di echo untuk variabel tersebut
                      ]]
                    ])
                  sh '''
                    echo $gitlabSourceRepoHttpUrl
                  '''
                }
            }
            notifySlack(currentBuild.result,"checkout code")
            script{
              if (currentBuild.result == "FAILURE"){
                error "FAILURE/UNSTABLE"
              }
            }
          }
        }

      stage('build-apps') {
        steps {
         catchError() {
            script{
               docker.withServer('tcp://172.18.0.1:2375')
               {
               def buildImage = docker.build("flashpras/$gitlabSourceRepoName:pipeline-${BUILD_NUMBER}")
               docker.withRegistry('', 'dockerhubtokenbaru')
                  {
                    buildImage.push()
                  }         
                }
                  }
         }
         notifySlack(currentBuild.result,"build-apps")
         script{
              if (currentBuild.result == "FAILURE"){
                error "FAILURE/UNSTABLE"
               }
             }
          }
      }
      stage('deploy-apps') {
        steps {
         catchError() {
            script{
              withCredentials([file(credentialsId: 'kubeconfig', variable: 'KUBECONFIG')]) {
                  sh 'export KUBECONFIG=${KUBECONFIG}'
                  // Use kubectl commands
                  sh '''
			              kubectl config current-context
                    kubectl set image deployment/sample-app sample-app=flashpras/$gitlabSourceRepoName:pipeline-${BUILD_NUMBER}
                    kubectl get pods
                    '''
                        // You can add more kubectl commands as needed
                    }
                }

            }
        notifySlack(currentBuild.result,"deploy-apps")
         script{
              if (currentBuild.result == "FAILURE"){
                error "FAILURE/UNSTABLE"
               }
             }
            }
         }

  }
}
def notifySlack(String buildStatus = 'STARTED', String StageName) {
  // build status of null means successful
  buildStatus =  buildStatus ?: 'SUCCESSFUL'

  // Default values
  def colorName = 'RED'
  def colorCode = '#FF0000'
  def subject = "${buildStatus}: Stage '${StageName} on ${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
  def summary = "${subject} (${env.BUILD_URL})"

  // Override default values based on build status
  if (buildStatus == 'STARTED') {
    color = 'YELLOW'
    colorCode = '#FFFF00'
  } else if (buildStatus == 'SUCCESSFUL') {
    color = 'GREEN'
    colorCode = '#00FF00'
  } else {
    color = 'RED'
    colorCode = '#FF0000'
  }

  // Send notifications
  //slackSend (color: colorCode, message: summary, baseUrl: 'https://slack.com/services/hooks/jenkins-ci/', channel: '#deployment', failOnError: true, token: 'xxxxxxxx')
}