pipeline {
  agent {
    docker {
      image 'docker.internal.justin.tv/devtools/bionic/node14.15.1:latest'
      args '-u root'
    }
  }
  environment {
    NPM_REPO="https://packages.internal.justin.tv/artifactory/api/npm"
  }
  stages {
    stage('build') {
      steps {
        sh 'python3 --version'
        sh 'pip --version'
        sh 'apt-get update && apt-get install python3-venv -y && apt-get -y install python3-pip'
        sh 'pip3 install setuptools-rust twine'
        sh 'npm install && npx tsc && npm test'
        sh 'npm run build && npm run package'
      }
    }
    stage('deploy npm package') {
      when { branch 'master' }
      steps {
        withCredentials([[$class: 'StringBinding', credentialsId: 'dta_tools_deploy', variable: 'DTA_TOOLS_DEPLOY_PASS']]) {
          sh 'curl -udta_tools:$DTA_TOOLS_DEPLOY_PASS $NPM_REPO/auth > ~/.npmrc'
          sh 'echo "email = dta@justin.tv" >> ~/.npmrc'
          sh 'npm publish --registry "$NPM_REPO/ip-npm"'
        }
      }
    }
    stage('deploy pypi package') {
      when { branch 'master' }
      steps {
        withCredentials([[$class: 'StringBinding', credentialsId: 'dta_tools_deploy', variable: 'DTA_TOOLS_DEPLOY_PASS']]) {
          sh 'uploadPypi --upload dist/python/*'
        }
      }
    }
  }
  post {
    always {
      sh 'git clean -fdx'
    }
  }
}
