pipeline {
  agent any
  stages {
    stage('maketest') {
      steps {
        catchError() {
          checkout([$class: 'GitSCM', branches: [[name: '$tag']], extensions: [[$class: 'WipeWorkspace'], [$class: 'GitTagMessageExtension']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlab-ssh-key', url: 'git@https://github.com/martonowibowo/sample-nodejs.git']]])
          sh '''
          echo "test"
          '''
          sh 'git describe'
        }
        notifySlack(currentBuild.result,"maketest")
      }
    }
    

stage('build-apps') {
        steps {
         catchError() {
        sh '''
        echo "build app"
        '''
        }
        notifySlack(currentBuild.result,"build-apps")
    }      
     
}

stage('deploy-code') {
        steps {
         catchError() {
        sh '''
        echo "deploy-code"
        '''
        }
        notifySlack(currentBuild.result,"deploy-code")
    }      
     
}

  }
}
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')
}
