首页 > 解决方案 > 从 Jenkinsfile 中的 jenkins 作业配置中读取 SVN URL

问题描述

我有一个 Jenkins 管道作业,它被配置为“来自 SCM 的管道脚本”,指向 SVN 中的 Jenkinsfile。例如,SVN URL 是 http://example.com/svn/myproject/trunk/ jenkins master 进行浅层检出(仅根文件),然后在根目录中找到 Jenkinsfile。

此 URL 在 Jenkinsfile 中重复,在该步骤中检出单独的 jenkins 代理上的 SVN 工作区。

因此,每当我为新分支克隆 jenkins 作业时,我需要在两个不同的位置修复 URL:一次在 jenkins 作业配置中,它指向 Jenkinsfile,一次在 Jenkinsfile 中。有没有办法避免这种情况,例如通过从 Jenkinsfile 读取当前作业配置?

标签: jenkinsjenkins-pipeline

解决方案


最后,我设法通过scm全局从作业配置中检索 SVN URL,将其存储在 SVNURL 环境变量中,然后在结帐步骤中使用它,而不是对 SVN URL 进行硬编码。

stage('stage-name')
{
    steps
    {
        // Get the SVN URL from the pipeline job configuration
        script
        {
            env.SVNURL = scm.getLocations()[0].getURL()
        }

        checkout([
            $class: 'SubversionSCM',
            filterChangelog: false,
            ignoreDirPropChanges: false,
            locations:
                [[
                    credentialsId: '<guid>',
                    depthOption: 'infinity',
                    ignoreExternalsOption: false,
                    local: 'my-checkout-folder',
                    remote: env.SVNURL
                ]],
            workspaceUpdater: [$class: 'UpdateWithCleanUpdater']
        ])
    }
}

推荐阅读