#!/usr/bin/env groovy

pipeline {
    agent {
        docker {
            image 'docker.pkgs.xarth.tv/devtools/bionic/node12.10.0:latest'
            args '-u root'
        }
    }

    options {
        disableConcurrentBuilds()
        timestamps()
        skipDefaultCheckout(true)
    }

    environment {
        NPM_REPO="https://npm.pkgs.xarth.tv/"
        GIT_TRACE=true
    }

    stages {
        stage('checkout') {
            steps {
                checkout([
                $class: 'GitSCM',
                branches: scm.branches,
                doGenerateSubmoduleConfigurations: false,
                extensions: [[$class: 'CleanCheckout']] + scm.extensions + [[$class: 'CloneOption', noTags: false, reference: '', shallow: false]],
                submoduleCfg: [],
                userRemoteConfigs: scm.userRemoteConfigs
                ])
            }
        }
        stage('prepare') {
            steps {
                script {
                    TAG_NAME = sh(script: "git describe --exact | tr -d '\n'", returnStdout: true)
                    GIT_ORIGIN = sh(script: "git remote get-url origin", returnStdout: true)
                    isTaggedBuild = TAG_NAME != ""
                    isLibraryBuild = (TAG_NAME ==~ /v\d+\.\d+\.\d+.*/)

                    echo "Tag name: '${TAG_NAME}' (tagged build=${isTaggedBuild}, library build=${isLibraryBuild})"
                    echo "Git origin: '${GIT_ORIGIN}'"
                }
            }
        }
        stage('checkout submodules') {
            steps {
                sh 'git config --local url."git@git.xarth.tv:".insteadOf "https://git.xarth.tv/"'
                checkout([
                $class: 'GitSCM',
                branches: scm.branches,
                doGenerateSubmoduleConfigurations: false,
                extensions: [[$class: 'SubmoduleOption',
                                disableSubmodules: false,
                                parentCredentials: true,
                                recursiveSubmodules: true,
                                reference: '',
                                trackingSubmodules: false]],
                submoduleCfg: [],
                userRemoteConfigs: [[credentialsId: 'git-aws-read-key', url: "${GIT_ORIGIN}"]]
                ])
            }
        }
        stage('build') {
            steps {
                sh 'npm ci --unsafe-perm'
                sh 'npm run prebuild'
                sh 'npm run format'
                sh 'npm run lint -- --max-warnings=0'
                sh 'npm run coverage'
                sh 'npm run build'
            }
        }

        stage('publish') {
            when {
                equals expected: true, actual: isLibraryBuild
            }
            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 'npm publish'
            }
        }
    }

    post {
        always {
            cleanWs()
        }
    }
}
