管道DSL:即时更改内部版本名称

时间:2019-06-04 08:14:27

标签: jenkins jenkins-pipeline jenkins-groovy

我想基于一个核心作业(下游作业)并行运行一些构建,在下面的代码中,没有选项可以动态更改构建名称,例如,我想代替数字(默认),我想要获得ABCDE _ $ {number},这可能吗?

我尝试使用一些版本号插件插入,但是我无法设置内部版本号...。

    stage ("Run Tests") {

        steps {

            dir("UIAutomationV2.0") {
                script {
                        tasks = [:]
                        products_set = [].toSet()

                        features_list.each {

                            def featureName = it.key

                            tasks[featureName] = {

                                withEnv(["FEATURE_NAME=${featureName}"]) {

                                    def valArr = it.value.split(",")
                                    def productName = valArr[0]
                                    def productPath = valArr[1]

                                    def runnerFeaturePath = productPath.replace("UIAutomationV2.0/", '')

                                    metaData["tests"][it.key]['phases']['Run Tests']["startTime"] = getEpochTime();
                                        println "Run test startTime : " + metaData["tests"][it.key]['phases']['Run Tests']["startTime"]

                                    println "Calling build for feature '${featureName}' in job '${productName}' under path ='${productPath}' "
                                    pJob = build job: "v2_Core_Task_Runner_Slack", propagate: false, parameters: [
                                        [$class: 'StringParameterValue', name: 'FeatureName', value: "${featureName}"],
                                        [$class: 'StringParameterValue', name: 'FeaturePath', value: "${runnerFeaturePath}"],
                                        [$class: 'StringParameterValue', name: 'TagName', value: "${params.TagName}"],
                                        [$class: 'StringParameterValue', name: 'Environment', value: "${params.Environment}"],
                                        [$class: 'BooleanParameterValue', name: 'CreateTenant', value: params.CreateTenant],
                                        [$class: 'StringParameterValue', name: 'TagNameCondition', value: "${params.TagNameCondition}"],
                                        [$class: 'StringParameterValue', name: 'TenantTemplate', value: "${params.TenantTemplate}"],
                                        [$class: 'StringParameterValue', name: 'ClientLabel', value: "AGENTS_LABEL_${JOB_NAME}_${BUILD_NUMBER}"] 
                                    ]

                                    metaData["tests"][it.key]['phases']['Run Tests']["endTime"] = getEpochTime();
                                    metaData["tests"][it.key]['consoleUrl'] = pJob.getAbsoluteUrl();
                                    metaData["tests"][it.key]['result'] = pJob.getResult();
                                    //pJob.nextBuildNumber = pJob.nextBuildNumber() + 1
                                    println "Job result = " + pJob.getResult() + ", Url: " + pJob.getAbsoluteUrl()
                                //  def nextBldNo = VersionNumber(versionNumberString: '${BUILD_DATE_FORMATTED, "yyyyMMdd"}-feature-${featureName}-${BUILDS_TODAY}')
                                //  nextBldNo = '${nextBldNo}' + pJob.nextBuildNumber
                                //  println "next build :: " + '${nextBldNo}'
                                //  println "next build num >> " +  VersionNumber(versionNumberString: '${BUILD_DATE_FORMATTED, "yyyyMMdd"}-feature-${featureName}-${BUILDS_TODAY}')
                                    println "Copy artificats to 'allreports/${productName}/${featureName}'"

                                    copyArtifacts(
                                        projectName: 'v2_Core_Task_Runner_Slack',
                                        filter: '**/report.json',
                                        fingerprintArtifacts: true,
                                        target: "allreports/${productName}/${featureName}",
                                        flatten: true,
                                        selector: specific(pJob.getId()),
                                        optional: true
                                    )

                                    println "Run test endtime : " + metaData["tests"][it.key]['phases']['Run Tests']["endTime"]



                        parallel tasks
                        metaData["endTime"] = getEpochTime()
                        metDataStr = new JsonBuilder(metaData).toPrettyString()

                        //killPhaseCondition("NEVER")


                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

def test = [:]

pipeline {
stages {
    stage ('create map') {
        steps {
            script {
                for (int i = 0; i < (yourCounter as Integer); i++) {
                    def name = i
                    test.put(name, testFunc(name))
                }
            }
        }
    }
    stage ('run parallel') {
        steps {
            script {

                parallel test
            }
        }
    }
}
}

def testFunc(name) {
return {
    stage ("${name}") {
        node ("${name}") {
            script {
                echo name
            }
        }
    }
 }
}

通过这种方式,您可以将任何值传递给testFunc()并控制其参数