def label = "node-${UUID.randomUUID().toString()}"

podTemplate(label: label, inheritFrom: 'base', , containers: [
  containerTemplate(name: 'base', image: 'taylorchristieyardstick/build-base:node')
]) {
  node(label) {
    stage('Checkout Project') {
      container('base') {
          checkout scm
      }
    }
    stage('Install Dependencies') {
      container('base') {
        sh "npm install"
        sh "npm install -g typescript"
      }
    }
    stage('Build the Project') {
      container('base') {
        sh "tsc"
      }
    }
    stage('Run unit tests') {
      container('base') {
        sh "npm test"
      }
    }
    stage('Login to Dockerhub') {
        withCredentials([usernamePassword(credentialsId: 'DockerHubAccessYardstick', usernameVariable: 'USER', passwordVariable: 'PASS')]) {
            container('base') {
                sh "docker login --username ${USER} --password ${PASS}"
            }
        }
    }
    stage('Build the Docker Image') {
      container('base') {
        sh "make build branch=${BRANCH_NAME}"
      }
    }

    stage('Push the Docker Image') {
      container('base') {
        sh "make push branch=${BRANCH_NAME}"
      }
    }
  }
}