首页 > 解决方案 > 在 Jenkins 多分支管道中指定 GitHub 项目 URL

问题描述

我正在尝试在 Jenkins Multibranch 管道中指定 GithubProjectProperty。我试图在选项块中设置一个条目来控制这个值,但没有成功。

管道语法片段生成器建议:

properties([
    $class: 'GithubProjectProperty',
    displayName: '',
    projectUrlStr: 'https://myServer/myOrg/myRepo'
])

以下似乎都不起作用:

1)尝试将属性直接放在选项块中

options {
        // Set the URL for the GitHub project option
        properties([
            $class: 'GithubProjectProperty',
            displayName: '',
            projectUrlStr: 'https://myServer/myOrg/myRepo'
        ])
}

错误:“属性”部分已重命名为 0.8 版。改用“选项”

2)删除属性关键字,但将选项保留在选项块中

options {
        // Set the URL for the GitHub project option
        [
            $class: 'GithubProjectProperty',
            displayName: '',
            projectUrlStr: 'https://myServer/myOrg/myRepo'
        ]
}

错误:选项不能定义为地图

3) 将 GitHubProjectProperty 视为可以实例化(如 office365ConnectorWebhooks)

options {
        // Set the URL for the GitHub project option
        GithubProjectProperty('https://myServer/myOrg/myRepo')
}

错误:无效的选项类型“GithubProjectProperty”。有效选项类型:[authorizationMatrix、buildDiscarder、catchError、checkoutToSubdirectory、disableConcurrentBuilds、disableResume、durabilityHint、newContainerPerStage、office365ConnectorWebhooks、overrideIndexTriggers、parallelsAlwaysFailFast、preserveStashes、quietPeriod、rateLimitBuilds、retry、script、skipDefaultCheckout、skipStagesAfterUnstable、timeout、waitUntil、warnError、withContext、withCredentials , withEnv, ws]

4) 将 GitHubProjectProperty 视为可以实例化但在脚本块内(因为根据尝试 #3,脚本应该是有效的)

options {
    script {
        // Set the URL for the GitHub project option
        GithubProjectProperty('https://myServer/myOrg/myRepo')
    }
}

错误:选项定义不能有块

office-365-connector-plugin 是一个工作插件,在 Jenkinsfile 的选项块中受支持。我将其代码与GitHub 上的 github-plugin 源代码进行了比较,并注意到以下行:

@Extension
public static final class DescriptorImpl extends JobPropertyDescriptor {

该代码缺少 office365ConnectorWebhooks 似乎在其代码中提供的 @Symbol 指令:

@Extension
@Symbol("office365ConnectorWebhooks")
public final class WebhookJobPropertyDescriptor extends JobPropertyDescriptor {

是否有一些特殊的语法可用于将 GitHub URL 添加到多分支管道,或者该插件不支持通过 Jenkinsfile 管理它?

标签: jenkinsjenkins-pipelinejenkins-pluginsjenkins-groovymultibranch-pipeline

解决方案


使用 Jenkinsfile 在管道中指定选项的能力需要一个符号。Jenkins github-plugin 中有一个建议的修复程序,它添加了必要的 Symbol 指令,但它目前不是 1.30.0 版插件的一部分。

见:https ://issues.jenkins-ci.org/browse/JENKINS-62339

同时,开发人员可以通过更新以下文件来构建自己的更新插件: src/main/java/com/coravy/hudson/plugins/github/GithubProjectProperty.java来源:https://github .com/jenkinsci/github-plugin

添加符号:

    import org.jenkinsci.Symbol;
...
    @Extension
    @Symbol("githubProjectProperty")
    public static final class DescriptorImpl extends JobPropertyDescriptor {
...

为了更好地衡量,请确保代码为newInstance指定正确的函数签名:

   @Override
   public JobProperty<?> newInstance(@Nonnull StaplerRequest req, JSONObject formData)
           throws Descriptor.FormException {

Jenkins 管理员可以使用插件管理器中的高级选项安装更新的插件,以从中央插件存储库外部上传 .hpi 文件


推荐阅读