pipeline {
  agent {
    label "executor"
  }
  environment {
    kubmaster = credentials('kubmaster')
  }
  options {
    buildDiscarder(
      logRotator(
        numToKeepStr: "10"
      )
    )
    timeout(
      time: 15,
      unit: "MINUTES"
    )
    skipDefaultCheckout()
  }
  stages {
    stage("SCM"){
      when { 
        branch 'analysis' 
      }
      steps {
        checkout scm
      }
    }
    stage("Analysis"){
      when { 
        branch 'analysis' 
      }
      steps {
        echo "====++++ executing Analysis ++++===="
        withSonarQubeEnv("SonarqubeServer") {
          sh "sonar-scanner"
        }
        sleep(time:1, unit:"MINUTES")
        timeout(time: 10, unit: "MINUTES") {
          waitForQualityGate abortPipeline: true
        }
      }
    }
    stage("Remote Project Dev"){
      when { 
        branch 'development' 
      }
      steps {
        echo "====++++ executing Git Pull ++++===="
        script {
          def remote = [:]
          remote.name = 'kubmaster'
          remote.host = env.kubmaster
          remote.allowAnyHosts = true

          withCredentials([sshUserPrivateKey(credentialsId: 'lstvuser', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
            remote.user = userName
            remote.identityFile = identity

            // Initialize repo in /dev/<project> directory --> only done once, comment out if done
            //sshCommand remote: remote, command: 'eval $(ssh-agent -s) && ssh-add ~/.ssh/bitbucket-ssh && cd /var/www/html/dev && git clone git@bitbucket.org:lstvwebacc/acc_onedb.git && cd acc_onedb && git checkout development && exit'

            // Can execute db config & setting
            // sshCommand remote: remote, command: '<-- DB commands --> && exit'
            
            // Main command pulling latest branch/commit
            //sshCommand remote: remote, command: 'eval $(ssh-agent -s) && ssh-add ~/.ssh/bitbucket-ssh && cd /var/www/html/dev/<project> && git switch development && git pull && exit'
          }
        }
      }
    }
    stage("Remote Project Testing - Decrypted"){
      when { 
        branch 'testing' 
      }
      steps {
        echo "====++++ executing Git Pull ++++===="
        script {
          def remote = [:]
          remote.name = 'kubmaster'
          remote.host = env.kubmaster
          remote.allowAnyHosts = true

          withCredentials([sshUserPrivateKey(credentialsId: 'lstvuser', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
            remote.user = userName
            remote.identityFile = identity

            // Initialize repo in /testing/dec/<project> directory --> only done once, comment out if done
            //sshCommand remote: remote, command: 'eval $(ssh-agent -s) && ssh-add ~/.ssh/bitbucket-ssh && cd /var/www/html/online_test/ && git clone git@bitbucket.org:lstvwebacc/acc_onedb.git && cd acc_onedb && git checkout testing && exit'

            // Can execute db config & setting --> use when needed
            // sshCommand remote: remote, command: '<-- DB commands --> && exit'
            
            // Main command pulling latest branch/commit
            sshCommand remote: remote, command: 'eval $(ssh-agent -s) && ssh-add ~/.ssh/bitbucket-ssh && cd /var/www/html/online_test/acc_onedb && git switch testing && git pull && exit'
          }
        }
      }
    }
    stage("Remote Project Testing - Encrypted"){
      when { 
        branch 'testing-enc' 
      }
      steps {
        echo "====++++ executing Git Pull ++++===="
        script {
          def remote = [:]
          remote.name = 'kubmaster'
          remote.host = env.kubmaster
          remote.allowAnyHosts = true

          withCredentials([sshUserPrivateKey(credentialsId: 'lstvuser', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'userName')]) {
            remote.user = userName
            remote.identityFile = identity

            // Initialize repo in /testing/enc/<project> directory --> only done once, comment out if done
            // sshCommand remote: remote, command: 'eval $(ssh-agent -s) && ssh-add ~/.ssh/bitbucket-ssh && cd /var/www/html/testing/enc && git clone <project ssh url> && cd <project> && git checkout testing-enc && exit'

            // Can add db config & setting --> use when needed
            // sshCommand remote: remote, command: '<-- DB commands --> && exit'
            
            // Main command pulling latest branch/commit
            // sshCommand remote: remote, command: 'eval $(ssh-agent -s) && ssh-add ~/.ssh/bitbucket-ssh && cd /var/www/html/testing/enc/<project> && git switch testing-enc && git pull && exit'
          }
        }
      }
    }
  }
  post {
    always {
      withCredentials([usernamePassword(credentialsId: 'skype', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
        sh """
        python3 /files/skype-notif.py ${currentBuild.currentResult} ${env.BUILD_NUMBER} "${currentBuild.fullDisplayName}" ${env.BUILD_URL} ${USERNAME} ${PASSWORD}
        """
      }
    }
  }
}
