Build and Test Implementation

#!groovy

/*--------------------------------------------------------------------------------------------------
 *
 * Copyright (C) 2018, HERE Global B.V.
 *
 * These coded instructions, statements, and computer programs contain
 * unpublished proprietary information of HERE Global B.V., and are copy
 * protected by law. They may not be disclosed to third parties or copied
 * or duplicated in any form, in whole or in part, without the specific,
 * prior written permission of HERE Global B.V.
 *
 *--------------------------------------------------------------------------------------------------
 */

/**
 * Following parameters should be automatically provided by Gerrit plugin for Jenkins
 * @param [GERRIT_REFSPEC] Gerrit refspec to checkout
 * @param [GERRIT_BRANCH] Gerrit branch to checkout
 * @param [GERRIT_EVENT_TYPE] Gerrit event type change-merged or patchset-created
 * @param [GERRIT_PORT] Gerrit server port
 */

/**
 *
 * Git credentials ID from here https://${JENKINS_URL}/credentials/store/system/domain/_/
 * @env [GIT_CREDS_FILE_ID]
 *
 * ID:s of multiple configuration files from here https://${JENKINS_URL}/configfiles/index
 *
 * @env [SETTINGS_XML_FILE_ID] (e.g. ${USER_HOME}/.m2/settings.xml)
 *
 * Note:
 * ${JENKINS_URL} should be replaced with your own Jenkins URL
 */

def node_label = env.NODE_LABEL ? env.NODE_LABEL : 'master'

/**
 * Maven get project version function.
 */
String getProjectVersion() {
    return sh(
            script: "mvn -q " +
                    "-Dexec.executable=echo " +
                    "-Dexec.args=\'\${projects.version}\' " +
                    "--non-recursive " +
                    "org.codehaus.mojo:exec-maven-plugin:1.6.0:exec",
            returnStdout: true).trim()
}

/**
 * Maven get project groupId function.
 */
String getProjectGroupId() {
    return sh(
            script: "mvn -q " +
                    "-Dexec.executable=echo " +
                    "-Dexec.args=\'\${projects.groupId}\' " +
                    "--non-recursive " +
                    "org.codehaus.mojo:exec-maven-plugin:1.6.0:exec",
            returnStdout: true).trim()
}

/**
 * Maven get project artifactId function.
 */
String getProjecArtifactId() {
    return sh(
            script: "mvn -q " +
                    "-Dexec.executable=echo " +
                    "-Dexec.args=\'\${projects.artifactId}\' " +
                    "--non-recursive " +
                    "org.codehaus.mojo:exec-maven-plugin:1.6.0:exec",
            returnStdout: true).trim()
}

node(node_label) {
    def inPostSubmitVerification = env.GERRIT_EVENT_TYPE == 'change-merged' ? true : false
    def MAVEN_OPTS = "-B -q -s \$MAVEN_SETTINGS"

    configFileProvider([configFile(fileId: env.MAVEN_SETTINGS_FILE_ID, variable: 'MAVEN_SETTINGS')]) {

        stage('Initialize') {
            // Clean workspace before build starts
            cleanWs()
        }

        stage('Clone the Git repository') {
            // Clone repository into the workspace
            checkout(
                    $class: 'GitSCM',
                    branches: [[
                                       name: env.GERRIT_BRANCH
                               ]],
                    doGenerateSubmoduleConfigurations: false,
                    extensions: [],
                    submoduleCfg: [],
                    userRemoteConfigs: [
                            [
                                    credentialsId: env.GIT_CREDS_FILE_ID,
                                    url          : "ssh://${env.GERRIT_HOST}:${env.GERRIT_PORT}/${env.GERRIT_PROJECT}",
                                    refspec      : env.GERRIT_REFSPEC
                            ]
                    ]
            )
        }

        stage('Build') {
            sh("mvn ${MAVEN_OPTS} compile -Pplatform")
        }

        stage('Run Local Tests') {
            sh("mvn ${MAVEN_OPTS} verify -Pplatform")
        }

        // In case of pre-submit verification skip publication
        if (inPostSubmitVerification) {
            stage('Publish JAR to artifact repository') {
                sh("mvn ${MAVEN_OPTS} deploy -DskipTests -Pplatform -Ptests")
            }

            stage('Deploy to testing environment') {
                build(
                        job: 'CI-CD-examples/step2',
                        parameters: [
                                [
                                        $class: 'StringParameterValue',
                                        name  : 'PROJECT_VERSION',
                                        value : getProjectVersion()
                                ],
                                [
                                        $class: 'StringParameterValue',
                                        name  : 'PROJECT_GROUP_ID',
                                        value : getProjectGroupId()
                                ],
                                [
                                        $class: 'StringParameterValue',
                                        name  : 'PROJECT_ARTIFACT_ID',
                                        value : getProjecArtifactId()
                                ]
                        ],
                        wait: false
                )
            }
        }
    }
}

results matching ""

    No results matching ""