pipeline {

    options {
        buildDiscarder(logRotator(numToKeepStr: '10', artifactNumToKeepStr: '10'))
    }
  
    agent any
      
    tools {nodejs "DefaultNodeJs"}
         
    triggers {
        bitbucketPush()
    }

    stages {
        stage("Build") {
            steps {
                bitbucketStatusNotify buildState: "INPROGRESS"
                sh 'npm run build'
            }
        }

        stage("Test") {
            steps {
                echo 'Running Unit Tests'
                sh 'npm run test-unit'
            }
        }
        
        stage('Deploy') {
            when { buildingTag() }
    
            steps {
                sh "echo //registry.npmjs.org/:_authToken=${NPM_TOKEN} > .npmrc"
                sh 'npm run deploy'
                sh 'rm .npmrc'
            }
        }
    }

    post {
        success {
            bitbucketStatusNotify buildState: "SUCCESSFUL"
        }

        failure {
            bitbucketStatusNotify buildState: "FAILED"
        }
    }
}