Jenkins:使用作业 dsl 为 github 组织配置分支发现

Jenkins: configure branch discovery for github organization with job dsl

我在为 github 组织文件夹配置作业 dsl 时遇到问题。我试图根据打开的拉取请求自动发现分支。我尝试使用 deprecated: buildOriginBranchWithPR(true) 但没有成功。我也尝试过:gitBranchDiscovery()gitTagDiscovery() 也没有太多结果。

我知道更改有几个未解决的问题,并且已经提出了一些解决方法。其中之一是使用作业 dsl 中的配置块直接与 xml 交互。我还没用好呢

如果我直接在 Jenkins 中使用 GUI 配置它,我已经设法让它工作,但我想尽可能避免这种情况。

请在此处查看我要定义的工作示例:

  organizationFolder('example-org') {
    description('This contains branch source jobs for example-org GitHub')
    displayName('example-org')
    triggers {
      periodic(2400)
    }
    organizations {
      github {
        repoOwner("example-owner")
        apiUri("https://api.github.com")
        credentialsId('jenkins-token')
        traits {
          publicRepoPullRequestFilterTrait()
        }
      }
    }
    configure {
      def traits = it / sources / data / 'jenkins.branch.BranchSource' / source / traits
      traits << 'org.jenkinsci.plugins.github__branch__source.BranchDiscoveryTrait' {
        strategyId(2)
      }
      traits << 'org.jenkinsci.plugins.github__branch__source.OriginPullRequestDiscoveryTrait' {
       strategyId(2)
     }
    }
    projectFactories {
      workflowMultiBranchProjectFactory {
        // Relative location within the checkout of your Pipeline script.
        scriptPath("Jenkinsfile")
      }
    }
  }

我使用的是官方 jenkins docker 镜像的最新版本。

在此先感谢您的帮助。

为组织文件夹作业生成的 config.xml 不同于多分支管道作业。您需要将配置块修改为与此类似的内容(注意获取特征列表的路径发生变化):

configure {
    def traits = it / navigators / 'org.jenkinsci.plugins.github__branch__source.GitHubSCMNavigator' / traits
    traits << 'org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait' {
        strategyId 1
    }
    traits << 'org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait' {
        strategyId 2
        trust(class: 'org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait$TrustEveryone')
    }
    traits << 'org.jenkinsci.plugins.github__branch__source.OriginPullRequestDiscoveryTrait' {
        strategyId 2
    }
}