声明性Jenkins管道:使用;“本地”;共享图书馆的功能

声明性Jenkins管道:使用;“本地”;共享图书馆的功能,jenkins,jenkins-pipeline,Jenkins,Jenkins Pipeline,我为我们的项目使用一个共享库,以拥有一个公共ci管道。还有一些特定于项目的东西,比如构建脚本。所以我想做的是在本地项目的Jenkinsfile中定义一些函数,并在库中使用它们 这是我的Jenkinsfile library identifier: "pipeline-helper@master", retriever: modernSCM( [$class: 'GitSCMSource', remote: 'https://bitbucket.intr

我为我们的项目使用一个共享库,以拥有一个公共ci管道。还有一些特定于项目的东西,比如构建脚本。所以我想做的是在本地项目的
Jenkinsfile
中定义一些函数,并在库中使用它们

这是我的
Jenkinsfile

library identifier: "pipeline-helper@master", retriever: modernSCM(
    [$class: 'GitSCMSource',
        remote: 'https://bitbucket.intra/scm/jenkins/pipeline-helper.git',
        credentialsId: 'bitbucket.service.user'
    ])

defaultCiPipelineGeneric {
    node = "APR"
}

/**
 * Builds the application based on parameters
 * @param pipelineParams map of all pipeline parameters
 */
def buildApplication(pipelineParams) {
    powershell '''
Write-Host "### [INFO] $(Get-Date) Restoring nuget packages: ${env:NUGET_HOME}"

$exe = ${env:NUGET_HOME} + "\\nuget.exe"
&$exe restore .\\Solution.sln
cmd.exe /c .\\Build.bat
cmd.exe /c .\\UnitTests.bat
'''
}
pipeline helper
定义了公共管道,我想在这里使用
buildApplication()
,它是在
Jenkinsfile
中定义的,而不是在
pipeline helper

请参见下面的
defaultCiPipelineGeneric.groovy

/**
 * Shared ci pipeline for generic projects
 * Source: https://jenkins.io/blog/2017/10/02/pipeline-templates-with-shared-libraries/
 */
def call(body) {
    // evaluate the body block, and collect configuration into the object
    def pipelineParams= [:]
    body.resolveStrategy = Closure.DELEGATE_FIRST
    body.delegate = pipelineParams
    body()

    pipeline {
        agent {
            label pipelineParams.nodes
        }
        stages {
            stage('Build') {
                steps {
                    script {
                        buildApplication(pipelineParams)
                    }
                }
            }
        }
    }
}
但是,这不起作用,因为我得到以下错误


这可能吗?我知道我可以有一个单独的groovy文件,并使用
load
-函数加载int。但我发现,如果您只需要一个文件(
Jenkinsfile
),其中包含所有用于构建的指令,则更具吸引力。

您需要将该方法作为对象输入传递给全局var方法。此外,您不应将
闭包
类型作为单个输入传递给全局var方法。由于各种问题,它被弃用了大约一年。传递
Map
类型是当前的最佳实践。