1

我一直在整个网络上搜索有关如何使用 groovy 创建 GitLab API 凭据的片段。并使用该 API 凭据创建 Gitlab 连接以用于“构建合并请求”目的,这将非常有帮助。提前致谢

更新:无论如何我找到了解决方案。我手动创建了 GitlabAPI 凭据并获取其 XML 并使用 jinja2 对其进行解析以使其动态化。然后我将它传递给 Jenkins CLI create creds by xml

cat /tmp/gitlab-credential.xml | \
java -jar {{ cli_jar_location }} \
-s http://{{ jenkins_hostname }}:{{ http_port }} \
create-credentials-by-xml  "SystemCredentialsProvider::SystemContextResolver::jenkins" "(global)"
4

1 回答 1

3

我遇到了类似的需要通过 groovy 创建 gitlab api 凭证。下面是我设法弄清楚的解决方案,改编自https://gist.github.com/iocanel/9de5c976cc0bd5011653

import jenkins.model.*
import com.cloudbees.plugins.credentials.*
import com.cloudbees.plugins.credentials.common.*
import com.cloudbees.plugins.credentials.domains.*
import com.cloudbees.plugins.credentials.impl.*
import com.dabsquared.gitlabjenkins.connection.*
import hudson.util.Secret

domain = Domain.global()
store = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()

token = new Secret("my-token")

gitlabToken = new GitLabApiTokenImpl(
  CredentialsScope.GLOBAL,
  "gitlab-token",
  "token for gitlab",
  token
)

store.addCredentials(domain, gitlabToken)
于 2018-01-16T16:32:20.953 回答