pipeline {
  agent none
  parameters {
    string(name: 'DEPLOY_NON_MASTER_BRANCH', defaultValue: 'false', description: 'deploy this branch to dev')
  }
  stages {
    stage ('Build') {
      agent {
        docker {
          image 'node:12'
          label 'dind'
        }
      }
      steps {
        checkout scm
        sh "npm install"
        sh "npm install -g @angular/cli"
        sh "ng build --prod"
      }
      post {
        success {
          echo 'Build stage completed'
        }
        failure {
          echo 'Build stage failed'
        }
      }
    }
    stage ('Test') {
      agent {
        docker {
          image 'node:12'
          label 'dind'
        }
      }
      steps {
        echo "Skipping test cases"
        // sh "ng test --browsers ChromeHeadless"  
      }
      post {
        success {
          echo 'Test stage completed'
        }
        failure {
          echo 'Test stage failed'
        }
      }
    }
    stage ('Deploy') {
      agent {
        docker {
          image 'node:12'
          label 'dind'
        }
      }
      when {
        anyOf {
            branch 'master'
            expression { params.DEPLOY_NON_MASTER_BRANCH == 'true'}
        }
      }
      steps {
        sh "sh deploy.sh ${env.BRANCH_NAME}"
      }
      post {
        success {
          echo 'Deploy stage completed'
        }
        failure {
          echo 'Deploy stage failed'
        }
      }
    }
    stage ('Clean up wrokspace') {
      agent {
        docker {
          image 'node:12'
          label 'dind'
        }
      }
      steps {
        echo "Cleaning repo data"
      }
      post {
        always {
            cleanWs()
        }
      }
    }
  }
}