Jenkins 和 Github 中的 DSL 作业

DSL Job in Jenkins and Github

我正在尝试获取 Github 中某个 repo 的分支,与执行相同:

curl -k -X GET https://api.github.com/repos/rackt/redux/forks

但是在 Jenkins 的 DSL 脚本中。

为什么?因为我想克隆所有人的叉子并在 job-dsl-plugin.

生成的单独作业上构建项目

当然这只是我找到的回购协议的一个例子。我正在尝试使用带有私有存储库的 SSH 凭据来做到这一点。

你知道哪种方法最好吗?

wiki which details how to create jobs for branches

中有一个真实世界的例子
def project = 'Netflix/asgard'
def branchApi = new URL("https://api.github.com/repos/${project}/branches")
def branches = new groovy.json.JsonSlurper().parse(branchApi.newReader())
branches.each {
    def branchName = it.name
    def jobName = "${project}-${branchName}".replaceAll('/','-')
    job(jobName) {
        scm {
            git("https://github.com/${project}.git", branchName)
        }
    }
}

您需要做的是将 job 部分移到 each 闭包之外,并使用分叉名称。

至于你对 ssh 和 private repos 的评论。使用 Credentials plugin like the wiki

将 ssh 密钥保留在脚本之外是个好主意

The first option involves the Credentials Plugin which manages credentials in a secure manner and allows Job DSL scripts to reference credentials by their identifier. It is also the most secure option because the credentials do not need to passed to the Job DSL script.

// use the github-ci-key credentials for authentication with GitHub
job('example-1') {
    scm {
        git {
            remote {
                github('account/repo', 'ssh')
                credentials('github-ci-key')
            }
        }
    }
}

我终于解决了 this (see the Gist I published):

import groovy.json.JsonSlurper

def owner = "<owner>"
def project = "<project>"

// curl -k -u <user>:<token> -X GET https://api.github.com/repos/<owner>/<repo>/forks > forks.txt

def fetch(addr, params = [:]) {
  def auth = "<personalAPIToken>" // see https://github.com/blog/1509-personal-api-tokens
  def json = new JsonSlurper()
  return json.parse(addr.toURL().newReader(requestProperties: ["Authorization": "token ${auth}".toString(), "Accept": "application/json"]))
}

def forks = fetch("https://api.github.com/repos/${owner}/${project}/forks")

forks.each {
    def fork = it.full_name
    def name = it.owner.login
    def jobName = "${project}-${name}".replaceAll('/','-')
    job(jobName) {
        scm {
            git {
              remote {
                github(fork, 'https')
                credentials('<idCredentials>')
              }
            }
        }
    }
}

谢谢!