pipeline {
  agent any
  environment {
    REPOSITORY="widgetario"
    RELEASE="21.12"    
    SONAR_ENFORCE_GATE="true"
    DOCKER_BUILDKIT=0
  }
  stages {
    stage('Audit tools') {                        
      steps {
        sh '''
          docker version
          docker compose version
        '''
      }
    }
    stage('Build') {
      environment {
        TRIVY_SCAN=1
      }
      steps {
        echo "Building images for release: ${RELEASE}"
        withCredentials([string(credentialsId: 'sonarqube-token', variable: 'SONAR_TOKEN')]) {
          sh '''
            docker compose -f project/compose/docker-compose.yml -f project/compose/build.yml build
          '''
        }
      }
    }
    stage('Smoke Test Deploy') {
      steps {
        script {
          try{
            echo "Running local containers"
            sh 'docker compose -p smoke -f project/compose/docker-compose.yml -f project/compose/test.yml up -d'
          }
          catch(x){
            echo "Repeating in case of i/o timeout error"
            sh 'docker compose -p smoke -f project/compose/docker-compose.yml -f project/compose/test.yml up -d'
          }
        }    
      } 
    }
    stage('Smoke Test Verify') {
      steps {
        echo "Testing HTTP services"
        sh '''
          stock_address=$(docker port smoke-stock-api-1 8080)
          wget -O- -q "${stock_address/0.0.0.0/host.docker.internal}/healthz"
        '''
        sh '''
          web_address=$(docker port smoke-web-1 80)
          wget -O- -q "${web_address/0.0.0.0/host.docker.internal}/up"
        '''
        script {
          try{
            sh '''
              products_address=$(docker port smoke-products-api-1 80)
              wget -O- -q -T 5 "${products_address/0.0.0.0/host.docker.internal}/healthz"
            '''
          }
          catch(x){
            sleep(20)
            sh '''
              products_address=$(docker port smoke-products-api-1 80)
              wget -O- -q -T 5 "${products_address/0.0.0.0/host.docker.internal}/healthz"
            '''
          }
        }
      }
    }
    stage('Push build version') {
      steps {
        echo "Pushing images for release: ${RELEASE}, build: ${BUILD_NUMBER}"       
        withCredentials([usernamePassword(credentialsId: 'docker-hub', usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PASSWORD')]) {
          sh '''
            docker login --username ${REGISTRY_USER} --password ${REGISTRY_PASSWORD}
            docker compose -f project/compose/docker-compose.yml -f project/compose/build.yml push
          '''
        }
      }
    }
    stage('Push release version') {
      steps {
        echo "Pushing images for release: ${RELEASE}"   
        sh 'docker compose -f project/compose/docker-compose.yml -f project/compose/build.yml -f project/compose/release.yml build'
        withCredentials([usernamePassword(credentialsId: 'docker-hub', usernameVariable: 'REGISTRY_USER', passwordVariable: 'REGISTRY_PASSWORD')]) {
          sh '''
            docker login --username ${REGISTRY_USER} --password ${REGISTRY_PASSWORD}
            docker compose -f project/compose/docker-compose.yml -f project/compose/build.yml -f project/compose/release.yml push
          '''
        }
      }
    }
    stage('Deploy to AKS') {
      steps {
        echo "Deploying release: ${RELEASE}"
        withCredentials([file(credentialsId: 'aks-kubeconfig', variable: 'KUBECONFIG')]) {
          sh '''
            kubectl apply -f project/kubernetes/widgetario --kubeconfig="${KUBECONFIG}"
            kubectl rollout restart deploy/web --kubeconfig="${KUBECONFIG}"
            kubectl get svc web-lb --kubeconfig="${KUBECONFIG}"
          '''
        }
      }
    }
  }
  post{
    always {      
      script {
        sh 'docker logout'
        try{
          sh 'docker compose -p smoke -f project/compose/docker-compose.yml -f project/compose/test.yml down' 
        }
        catch(x){
          echo "Ignoring compose down failure"
        }
      }       
    }
  }
}