podTemplate(
  label: 'simulation-platform-2',
  containers: [
    containerTemplate(
      name: 'build-env',
      image: 'us.gcr.io/equitysim-production/firebase-tools:1.4',
      command: 'cat',
      ttyEnabled: true,
      envVars: [
        secretEnvVar(key: 'FIREBASE_TOKEN', secretName: 'firebase-secrets', secretKey: 'finance-simulation-token'),
        secretEnvVar(key: 'NPM_TOKEN', secretName: 'npm-token', secretKey: 'token')
      ]
    )
  ]
) {
  node('simulation-platform-2') {

    checkout scm

    container('build-env') {
      def slackNotified = false

      try {
        stage('Install') {
          try {
            sh 'echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN}" > .npmrc'
            sh 'npm install'
          } catch (e) {
            slackSend(channel: "#builds", color: 'danger', message: "@frontends NPM -i FAIL: ${env.BUILD_URL}")
            slackNotified = true
            throw e
          }
        }

        stage('Test') {
          try {
            sh 'npm test'
          } catch (e) {
            slackSend(channel: "#builds", color: 'warning', message: "@frontends TESTS FAIL: ${env.BUILD_URL}")
            slackNotified = true
            throw e
          }
        }

        def isStaging = env.BRANCH_NAME == 'develop'
        def isQA = env.BRANCH_NAME == 'qa'
        def isProduction = env.BRANCH_NAME == 'master'

        if (isStaging || isQA) {
          properties([buildDiscarder(logRotator(artifactDaysToKeepStr: '10', artifactNumToKeepStr: '10', daysToKeepStr: '10', numToKeepStr: '10'))])
        }

        if (isStaging || isQA || isProduction) {
          stage('Build') {
            try {
              def buildCmd

              if (isProduction){ buildCmd = 'prod' }
              if (isQA){ buildCmd = 'qa' }
              if (isStaging){ buildCmd = 'dev' }

              sh "node --max_old_space_size=4096 npm run build:${buildCmd}"
            } catch (e) {
              slackSend(channel: "#builds", color: 'danger', message: "@frontends BUILD FAIL: ${env.BUILD_URL}")
              slackNotified = true
              throw e
            }
          }

          stage('Deploy') {
            try {
              sh """
                firebase use ${env.BRANCH_NAME}
                firebase deploy
              """
            } catch (e) {
              slackSend(channel: "#builds", color: 'danger', message: "@frontends DEPLOYMENT FAIL: ${env.BUILD_URL}")
              slackNotified = true
              throw e
            }
          }
        }
      } catch (exception) {
        throw exception
      } finally {
        def result = currentBuild.currentResult
        echo "result: $result"
        def color = "good"

        if (result == "FAILURE") {
            color = "danger"
        } else if (result == "UNSTABLE") {
            color = "warning"
        }

        if (!slackNotified) {
          slackSend(channel: "#builds", color: color, message: "@frontends $result: ${env.BUILD_URL}")
        }
      }
    }
  }
}
