Deployment 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.
 *
 *--------------------------------------------------------------------------------------------------
 */

import com.here.platform.ci.*

/**
 * ID:s of multiple configuration files from here https://${JENKINS_URL}/configfiles/index
 *
 * @env [OLP_CREDENTIALS_FILE_ID] (e.g. ${USER_HOME}/.here/credentials.properties)
 * @env [SETTINGS_XML_FILE_ID] (e.g. ${USER_HOME}/.m2/settings.xml)
 * @env [PIPELINE_ID] (optional) Will be set recursively if undefined.
 * @env [NODE_LABEL] (optional) Jenkins label or node name. By default 'master'.
 *
 * Note:
 * ${JENKINS_URL} should be replaced with your own Jenkins URL
 */

def setJobParametersRecursively(pipeline_id) {
    properties([
            parameters([
                    string(defaultValue: pipeline_id,
                            description: "This is the ID of the production " +
                                    "pipeline to which a successfully tested application " +
                                    "will be deployed",
                            name: 'PIPELINE_ID',
                            trim: true),
                    string(defaultValue: env.OLP_CREDENTIALS_FILE_ID,
                            name: 'OLP_CREDENTIALS_FILE_ID',
                            trim: true),
                    string(defaultValue: env.SETTINGS_XML_FILE_ID,
                            name: 'SETTINGS_XML_FILE_ID',
                            trim: true),
            ])
    ])
}

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

node(node_label) {
    def MAVEN_OPTS = "-B -q -s \$MAVEN_SETTINGS"

    // Initalize objects from HERE Delivery Jenkins DSL Shared Libraries
    CLIHelper cli = new CLIHelper(this)

    withCredentials([file(credentialsId: env.OLP_CREDENTIALS_FILE_ID, variable: 'OLP_CREDENTIALS')]) {
        configFileProvider([configFile(fileId: env.SETTINGS_XML_FILE_ID, variable: 'MAVEN_SETTINGS')]) {
            try {
                GString artifact = "${env.PROJECT_GROUP_ID}:${env.PROJECT_ARTIFACT_ID}:${env.PROJECT_VERSION}:jar"
                String deploymentId = "prd"
                GString path_prefix = "${WORKSPACE}/target/dependency/deployments/"
                GString deployment_config_path = "${path_prefix}/${deploymentId}/deployment.properties"
                def deployment
                def pipeline_id
                def current_version_id
                def version_id
                def pipeline_config_path

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

                    // Get 'fat' JAR package which contains all the dependencies
                    // If build was triggered by upstream build then no parameters are needed
                    def dependencyPlugin = "org.apache.maven.plugins:maven-dependency-plugin:3.1.1"
                    sh("mvn ${MAVEN_OPTS} ${dependencyPlugin}:unpack -Dartifact=${artifact} -Dproject.basedir=${WORKSPACE}")
                    sh("mvn ${MAVEN_OPTS} ${dependencyPlugin}:copy -Dartifact=${artifact}:platform -Dproject.basedir=${WORKSPACE}")

                    // Load properties
                    deployment = readProperties(file: deployment_config_path)

                    // Download HERE SDK and and unpack CLI it to ${WORKSPACE}/target/dependency/
                    cli.getOlpCliFromSDK(deployment.sdk_version)
                }

                stage('Deploy pipeline') {
                    pipeline_config_path = "${path_prefix}/pipeline-config.conf"
                    pipeline_config = cli.preparePipelineConfig(pipeline_config_path, path_prefix, deployment)

                    def fat_jar_path = FileHelper.findFile(this, "**/*-platform.jar")

                    if (env.PIPELINE_ID) {
                        deployment.put("pipeline_id", env.PIPELINE_ID)
                    }

                    def result = cli.deployPipeline(deployment, fat_jar_path, pipeline_config_path)
                    pipeline_id = result.pipeline_id
                    version_id = result.version_id

                    if (!env.PIPELINE_ID) {
                        // Set default PIPELINE_ID recursively in the same job configuration
                        setJobParametersRecursively(pipeline_id)
                    }
                }

                stage("Activate/upgrade pipeline") {
                    def all_pipeline_versions =
                            cli.json("olp pipeline version list ${pipeline_id}").pipelineVersions
                    current_version_id = cli.getCurrentVersionId(all_pipeline_versions)

                    if (current_version_id == '') {
                        cli.run(
                                "olp pipeline version activate "
                                        + "${pipeline_id} ${version_id}")
                    } else {
                        cli.run(
                                "olp pipeline version upgrade ${pipeline_id}"
                                        + "--from ${current_version_id} --to ${version_id}")
                    }
                }
            } catch (error) {
                stage("Clean-up") {
                    cli.cleanUp()
                }
                throw error
            }
        }
    }
}

results matching ""

    No results matching ""