2017-11-25 157 views
0

有誰知道如何在基本身份驗證中使用Jenkinsfile管道中的httpRequest? 我檢查了https://github.com/jenkinsci/http-request-plugin,它們支持支持基本認證(請參閱全局配置)。但是當我在我的groovy腳本中實現時,我發現了以下錯誤。任何人都可以展示如何將用戶名和密碼傳遞給httprequest。如何在基本身份驗證中使用Jenkinsfile管道中的httpRequest

def masterDataReplicatorTriggerDeployment() { 
milestone 40 
VaultUtil vaultUtil = PipelineUtil.getInstance(this).getProdVaultUtil() 
def ROUTER_USER 
def ROUTER_PASS 
def baseurl="http://master-data-replicator.cfapps.us10.hana.ondemand.com" 
def sourcetenant="revcdevkp" 
def targettenant="revcdevpo" 
def copyBCdata="/replicator/v1/businessConfig/" 
def response 


ROUTER_USER=vaultUtil.readCredential 
("secret/landscapes/infrastructure/router").get("router.security.username") 
ROUTER_PASS=vaultUtil.readCredential 
("secret/landscapes/infrastructure/router").get("router.security.password") 
response = httpRequest url: 
baseurl+copyBCdata+sourcetenant+"/"+targettenant, 
httpMode: 'POST', 
authentication: [Username: ROUTER_USER, Password: ROUTER_PASS] 

}

org.kohsuke.stapler.NoStaplerConstructorException:有一個關於在org.kohsuke.stapler.ClassDescriptor.loadConstructorParamNames(ClassDescriptor.java類java.lang.String 的任何構造沒有@DataBoundConstructor: 247) 在org.jenkinsci.plugins.structs.describable.DescribableModel。(DescribableModel.java:122) 在org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:380) 在

 def creds = "ROUTER_USER:ROUTER_PASS" 
    String auth = creds.bytes.encodeBase64().toString() 
    httpRequest consoleLogResponseBody: true, 
     url: baseurl+copyBCdata+sourcetenant+"/"+targettenant,      
     customHeaders:[[name:'Authorization', value:"Basic ${auth}"]] 

新問題:401認證問題。 但我確實使用了正確的憑證並可以訪問它們。

我知道我的認證格式不正確,你知道哪個應該是正確的方法嗎? 謝謝。

最好的問候,

回答

1

使用證書插件Credential plugin存儲您的憑據。然後使用httpRequest中的ID。

Example : 

New credential 

Scope : Global 
Username : my_technical_user 
Password : ******* 
ID : my_user_id 

httpRequest 

httpRequest httpMode: 'POST', 
url: "${baseurl}${copyBCdata}${sourcetenant}/${targettenant}", 
authentication: 'my_user_id' 

或者您可以直接在標頭上注入您的用戶名和密碼。

def creds = "your_username:your_password" 
String auth = creds.bytes.encodeBase64().toString() 
httpRequest consoleLogResponseBody: true, 
      url: "your_url",      
      customHeaders:[[name:'Authorization', value:"Basic ${auth}"]] 
+0

非常感謝。除了http請求之外,還有其他方法可以實現訪問端點嗎?我希望我的用戶名和密碼存儲在Vault中,而不是憑據ID。 最好的問候, – neilxie

+0

我編輯我的答案與另一個例子。 –

+0

非常感謝。但是我遇到了一個新的問題,憑證憑證錯誤。 – neilxie

相關問題