2016-11-07 222 views
0

在Jenkins中使用withCredentials觸發傳遞shell命令的函數。例如,而不是這樣做Groovy將shell命令傳遞到函數

withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'amazon', 
         usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { 
      //Shell commands block 
      sh """ 
      echo 'hello world' 
      echo 'Good bye' 
      """ 
     } 

觸發它這樣

def performWithCredentials(command_block){ 
    withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'amazon', 
         usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { 
      //Passed command_block 
     } 
} 

會是怎樣的propper方法來調用它詹金斯作業步驟裏面?

感謝和歡呼聲

回答

0

只要傳遞塊withCredentials;

def performWithCredentials(command_block){ 
    withCredentials([[$class: 'UsernamePasswordMultiBinding', 
         credentialsId: 'amazon', 
         usernameVariable: 'USERNAME', 
         passwordVariable: 'PASSWORD']], command_block) 
} 
+0

你能看到更新併爲此提供一個調用者嗎?謝謝 – OdinRW

0

你也可以寫自己的代表在groovy/pipelines

你可以把它變成

performWithCredentials(delegate) { 
    withCredentials([[$class: 'UsernamePasswordMultiBinding', 
     credentialsId: 'amazon', 
     usernameVariable: 'USERNAME', 
     passwordVariable: 'PASSWORD']]) { 
     delegate() 
    } 
} 

,並調用它像

performWithCredentials { 
    ... 
    <your code here> 
    ... 
} 

你甚至可以使一個文件performWithCredentials.groovy具有功能call(..)(參見tutorial)並將其存儲在$JENKINS_HOME/workflow-libs/中。 重新啓動Jenkins,讓它在全球範圍內可用