2017-07-02 161 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)