2017-07-02 454 views
0

我一直在搜索整个网页以获取有关如何使用groovy创建GitLab API凭证的片段。并使用该API凭证创建Gitlab连接以用于“构建合并请求”目的,这将非常有帮助。在此先感谢Jenkins凭证 - Gitlab API令牌

更新: 无论如何我找到了解决方案。我手动创建了GitlabAPI信誉,并将其XML与jinja2解析成动态。那么我已经将它传递给Jenkins CLI通过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)" 

回答

1

我遇到过类似的需要通过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)