/**
 * Pipeline to run the test suite and build a Debian package.
 *
 * https://industrial.jenkins.tttech.com/view/NERVE/
 *
 * See also:
 *  https://confluence.tttech.com/display/NER/Debian+packages
 *  https://confluence.tttech.com/display/NER/Jenkins
 *  https://confluence.tttech.com/display/JENKINS/Pipeline+guides+and+examples
 *  https://confluence.tttech.com/display/INFRA/How+to+Configure+a+Jenkins+Pipeline+job+for+doc-builder+Docker
 */
@Library('nerve-jenkins-libraries') _
pipeline {
    agent { label 'nerve && docker && nerve-main' }
    environment {
        HOME = sh(script: "mkdir -p build/ && mktemp -d -p $WORKSPACE/build/", returnStdout: true).trim()
        /* as a FOSS package, use public repository */
        APT_MIRROR = 'apt.nerve.cloud'
    }
    options {
        buildDiscarder(logRotator(
            artifactDaysToKeepStr: '30',
            artifactNumToKeepStr: '45',
            daysToKeepStr: '30',
            numToKeepStr: '45'
        ))
        skipDefaultCheckout() /* FOSS package is non-native */
        timestamps()
    }
    parameters {
        string(
            name: 'DIST',
            defaultValue: 'buster',
            description: 'Release to build for (buster = development)'
        )
        choice(
            name: 'ARCH',
            choices: 'amd64\ni386',
            description: 'Architecture to build on (for now should always be "amd64")'
        )
        booleanParam (
            name : 'UPLOAD',
            defaultValue: !!env.TAG_NAME,  /* FOSS: only upload tagged builds */
            description: 'Upload package to APT repository.'
        )
        string(
            name: 'APT_COMPONENT',
            defaultValue: 'main',
            description: 'APT component to upload to.'
        )
        string(
            name: 'PARENT_BUILD_NAME',
            defaultValue: '',
            description: '<strong>Internal use only.</strong>'
        )
        string(
            name: 'PARENT_BUILD_NUMBER',
            defaultValue: '',
            description: '<strong>Internal use only.</strong>'
        )
    }
    stages {
        stage ('Prepare environment') {
            steps {
                nerveDebianPackageCheckout() /* FOSS package in non-native */
                /* make pristine-tar visible: needed for build! */
                script {
                    sh "git branch -t pristine-tar origin/pristine-tar"
                }
                nervePrepare()
                nerveAptCheckStatus()
                nerveArtifactoryDockerLogin()
            }
        }

        stage ('Build package') {
            environment {
                /* skip tests during package build */
                /* not building tests also avoids additional build dependencies here */
                DEB_BUILD_PROFILES = 'nocheck'
                DEB_BUILD_OPTIONS = 'nocheck'
            }
            steps {
                nerveGitBuildpackage()
            }
        }
        stage ('Upload to Artifactory') {
            steps {
                nerveArtifactoryUpload spec: "build/${params.DIST}-${params.ARCH}/*", target: "deb/"
            }
        }
        stage ('Upload to APT') {
            when { expression { return params.UPLOAD } }
            steps {
                nerveAptUpload()
            }
        }
    }
    post {
        always {
            cleanWs()
            nerveCleanup()
        }
    }
}
