2017-04-24 56 views
0

我想在我的Jenkinsfile中聲明一個多維數組,這個數組包含不同環境的部署數據。在PHP中,我會這樣聲明:Jenkinsfile中的多維數組(Groovy)

$deployData = []; 

$deployData['test']['user'] = 'testuser'; 
$deployData['test']['host'] = 'testhost'; 
$deployData['test']['path'] = 'testpath'; 

$deployData['acceptance']['user'] = 'accuser'; 
$deployData['acceptance']['host'] = 'acchost'; 
$deployData['acceptance']['path'] = 'accpath'; 

$deployData['production']['user'] = 'produser'; 
$deployData['production']['host'] = 'prodhost'; 
$deployData['production']['path'] = 'prodpath'; 

如何在Jenkinsfile(Groovy)中實現相同的結果?我試圖創建一個新的String數組:

def deployData = new String[3][3][1] 

deployData['test']['user'] = 'testuser' 
deployData['test']['host'] = 'testhost' 
deployData['test']['path'] = 'testpath' 

deployData['acceptance']['user'] = 'accuser' 
deployData['acceptance']['host'] = 'acchost' 
deployData['acceptance']['path'] = 'accpath' 

deployData['production']['user'] = 'produser' 
deployData['production']['host'] = 'prodhost' 
deployData['production']['path'] = 'prodpath' 

當詹金斯運行上面的代碼中,我得到了以下錯誤:

java.lang.IllegalArgumentException: argument type mismatch

完全跟蹤:https://pastebin.com/ZEBRFuAW

有誰知道如何創建多維數組像第一個PHP示例(包括可讀性相同的值賦值)?

回答

2

這不是一個數組,這是一個地圖的地圖。你可以做一個在一行像這樣:

def deployData = [test:  [user: 'testuser', 
           host: 'testhost', 
           path: 'testpath'], 
        acceptance: [user: 'accuser', 
           host: 'acchost', 
           path: 'accpath'], 
        production: [user: 'produser', 
           host: 'prodhost', 
           path: 'prodpath']] 

或者:

def deployData = [:] 

deployData.test  = [user: 'testuser', host: 'testhost', path: 'testpath'] 
deployData.acceptance = [user: 'accuser', host: 'acchost', path: 'accpath'] 
deployData.production = [user: 'produser', host: 'prodhost', path: 'prodpath'] 
+0

非常感謝!任何想法如何在我的例子中使用PHP分配語法?爲了便於閱讀,我想實現它。 – Gun5m0k3

+0

@ Gun5m0k3增加了一種不同的方式來表達它......我認爲它比問題中更具可讀性,但是美麗是我猜想的旁觀者的眼睛;-) –