9阅网

您现在的位置是:首页 > 知识 > 正文

知识

jenkins - 在Jenkins多分支管道中指定GitHub项目的URL。

admin2022-11-07知识17

我试图在Jenkins多分支管道中指定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'
        ])
}

错误:"properties "部分在0.8版本时已重新命名。请使用 "选项 "代替。

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

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

ERROR.选项不能定义为地图。不能将选项定义为地图

3)把GitHubProjectProperty当作可以实例化的对象(就像office365ConnectorWebhooks)。

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

错误:无效选项类型 "GithubProjectProperty"。有效的选项类型:[authorizationMatrix, buildDiscarder, catchError, checkoutToSubdirectory, disableConcurrentBuilds, disableConcurrentBuilds]。授权矩阵、buildDiscarder、catchError、checkoutToSubdirectory、disableConcurrentBuilds、disableResume、d durabilityHint、newContainerPerStage、office365ConnectorWebhooks、overrideIndexTriggers、parallelsAlwaysFailFast、preservationStashes、quietPeriod、rateLimitBuilds、retry。脚本, 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')
    }
}

错误:选项定义不能有块。选项定义不能有块

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

@Extension
public static final class DescriptorImpl extends JobPropertyDescriptor {

该代码缺少一个@Symbol指令,而office365ConnectorWebhooks似乎在它的 编码:

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

有没有什么特殊的语法可以用来将GitHub URL添加到多分支管道中,还是那个插件就是不支持通过Jenkinsfile来管理?



【回答】:

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

请看 https:/issues.jenkins-ci.orgbrowseJENKINS-62339。

开发者可以通过更新以下文件,在这期间建立自己的更新插件。srcmainjavacomcoravyhudsonpluginsgithubGithubProjectProperty.java。 的源码中找到。https:/github.comjenkinscigithub-plugin。

添加符号。

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

为了更好地衡量,确保代码中的函数签名是正确的。新实例:

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

更新后的插件可以由Jenkins管理员使用Plugin Manager中的Advanced选项从中央插件库外上传一个.hpi文件来安装。