job-dsl - 如何在从 gitlab repo 分支创建作业时传递凭据?

job-dsl - How to pass credentials while creating jobs from gitlab repo branches?

我正在为 github 的每个应用程序分支创建工作。 我不确定如何将凭据传递给存储库 link?

import groovy.json.*

def project = 'app-ras'
def branchApi = new URL("https://gitlab.etctcssd.com/sdadev/${project}/branches")
def branches = new JsonSlurper().parse(branchApi.newReader())
branches.each {
def branchName = it.name
def jobName = "${project}-${branchName}".replaceAll('/','-')
job(jobName) {
    scm {
        git("https://gitlab.etctcssd.com/sdadev/${project}.git", branchName)
    }
  }
}

我们的项目是gitlab中的安全项目,那么在这种情况下如何传递凭据?

我确定它会重定向到登录页面。但我不确定如何处理。任何帮助将不胜感激。

我希望它能按以下方式工作:

import groovy.json.JsonSlurper

def project = 'app-ras'
def branchApi = new URL("https://gitlab.etctcssd.com/sdadev/${project}/branches")
def branches = new JsonSlurper().parse(branchApi.newReader())

branches.each {
    def branchName = it.name
    String jobName = "${project}-${branchName}".replaceAll('/', '-')
    job(jobName) {
        scm {
            git {
                branch(branchName)
                remote {
                    url("https://gitlab.etctcssd.com/sdadev/${project}.git")
                    credentials("HERE")
                }
            }
        }
    }
}

尝试将 HERE 替换为在 Jenkins -> Credentials.

还有,你用的是gitlab还是github?

编辑

据我了解,您在使用 Jenkins DSL 获取分支名称时遇到了问题。 Here 你可以看到如何从gitlab中获取分支。在groovy中可以通过以下方式完成:

URLConnection connBranches = new URL("https://gitlab.etctcssd.com/sdadev/${project}/branches").openConnection()
connBranches.setRequestProperty("PRIVATE-TOKEN", "PASTE TOKEN VALUE HERE")
new JsonSlurper().parse(new BufferedReader(new InputStreamReader(connBranches.getInputStream())))