Jenkins共享库可以访问作业参数

时间:2017-12-03 16:41:42

标签: jenkins shared-libraries environment

编辑:我必须确保Jenkins中的管道插件尽可能地保持最新状态。

我在我的本地Jenkins实例上创建了一个共享库。在Windows上运行的单个主节点。这是一个抽象的管道,其他管道可以随后利用。一切都运行良好,直到我转移到Linux盒子上的Jenkins实例。管道中的环境{}步骤似乎没有被运行,因此我必须在我的阶段开始时插入脚本{},以基本上设置环境变量。不幸的是,这些并没有通过执行传播,看起来我必须在地图中手动传递它们?这可能是我看到的共享库插件问题,也可能是Jenkins本身的版本?

谢谢, 标记

我有一个Jenkins文件如下

@Library("lifeSharedLibs") _
standardLifePipeline {
    projectName = "CheckmarxTestProj"
    recipientListDefault = "myemail@address.com"
    cxHost = 'https://checkmarxserver'
    cxDomain = 'domain'
    cxGroupId = 'groupId'
    cxHighThreshold = 1
    cxMediumThreshold = 5
    cxLowThreshold = 3
}

引用sharedPipeline,如下所示。您可以在Checkmarx阶段的脚本中看到我如何轻微复制环境。如果没有这个变量,调用checkmarxScan定义就无法使用这些变量。

 def call(body) {

    def pipelineParams = [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = pipelineParams
    body()

    pipeline {

        agent any

        tools {
            maven 'Maven_3.2.3'
            jdk 'java-8'
        }

        environment {
            cxProjectName = "${pipelineParams.projectName}"
            jobName = "${cxProjectName}" + ' CheckMarx Scan'
            gitHost = 'https://github.com'
            branchName = "${env.BRANCH_NAME}"
        }


        stages {
            stage('Checkmarx Scan and New Issue check') {
                steps {
                    script {
                        // Duplicating the environment section above
                        cxProjectName = "${pipelineParams.projectName}"
                        jobName = "${cxProjectName}" + ' CheckMarx Scan'
                        gitHost = 'https://github.com'
                        branchName = "${env.BRANCH_NAME}"

                        // email parameters
                        recipientListDefault = """ ${pipelineParams.recipientListDefault}"""
                    }
                    checkMarxScan(cxProjectName: "${pipelineParams.projectName}"+ "_" + branchName,
                            cxHost: "${pipelineParams.cxHost}",
                            cxDomain: "${pipelineParams.cxDomain}",
                            cxGroupId: "${pipelineParams.cxGroupId}",
                            cxHighThreshold: "${pipelineParams.cxHighThreshold}",
                            cxMediumThreshold: "${pipelineParams.cxMediumThreshold}",
                            cxLowThreshold: "${pipelineParams.cxLowThreshold}",
                            cxIncremental: params.CHECKMARX_INCREMENTAL_SCAN,
                            cxExcludeFolders : "${pipelineParams.cxExcludeFolders}",
                            cxRecipientListDefault : "${pipelineParams.recipientListDefault}"
                    )
                }
                post {
                    always {
                        echo "Post scan"
                    }
                }
            }
        }
        post {
            always {
                echo "Build completed. currentBuild.result = ${currentBuild.result}"
            }

            changed {
                echo 'Build result changed'
                script {
                    if (currentBuild.result == 'SUCCESS') {
                        echo 'Build has changed to SUCCESS status'
                    }
                }
            }
        }
    }
}

以前能够引用环境变量的checkmarxScan groovy方法的开头看起来像

def call(Map scanParams) {

    echo "The scanParams are " + scanParams
    echo "The cxProjectName variable set in the environment{} is " + cxProjectName

0 个答案:

没有答案