/**
 * 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 {
        APT_MIRROR = 'apt.nerve.cloud'
        HOME = sh(script: "mkdir -p build/ && mktemp -d -p $WORKSPACE/build/", returnStdout: true).trim()
    }
    options {
        buildDiscarder(logRotator(
            artifactDaysToKeepStr: '30',
            artifactNumToKeepStr: '45',
            daysToKeepStr: '30',
            numToKeepStr: '45'
        ))
        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.BRANCH_NAME == 'integration',
            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 {
                nervePrepare()
                nerveAptCheckStatus()
                nerveArtifactoryDockerLogin()
            }
        }

        /* Unfortunately, there is no test suite.
        stage ('Test') {
            environment {
                BASE_IMAGE = "$REGISTRY/nervesw/nerve:${params.DIST}"
                DOCKER_BUILD_ARGS = "--pull -t $DOCKER_SAFE_TAG -f Dockerfile.test --label=$DOCKER_LABEL"
            }
            steps {
                sh "docker build --build-arg IMAGE=$BASE_IMAGE $DOCKER_BUILD_ARGS ."
                sh "docker run --rm --user $UID:$GID -v $WORKSPACE:/dest/ $DOCKER_SAFE_TAG"
            }
        }
        */

        stage ('Build package') {
            steps {
                nerveGitBuildpackage(
                   volumes: [
                       "${WORKSPACE}/debian/docker-build.sh": "/usr/local/bin/build.sh",
                       "${WORKSPACE}/debian/docker-gbp.sh": "/usr/local/bin/gbp.sh"
                   ]
                )
            }
        }
        stage ('Upload to Artifactory') {
            steps {
                nerveArtifactoryUpload spec: "build/${params.DIST}-${params.ARCH}/*"
            }
        }
        stage ('Upload to APT') {
            when { expression { return params.UPLOAD } }
            steps {
                nerveAptUpload()
            }
        }
    }
    post {
        always {
            archiveArtifacts artifacts: "build/${params.DIST}-${params.ARCH}/*", fingerprint: true, allowEmptyArchive: true
            cleanWs()
            nerveCleanup()
        }
    }
}
