2017-06-15 102 views
1

Jenkins聲明式管道中的參數可以是動態的嗎?Jenkins動態聲明式管道參數

我想要一個函數在運行時填充選項選項值。下面的代碼確實生成了一個選項列表,但它們似乎過時了 - 可能是在我第一次運行此代碼時生成的。如果AMI列表發生變化,則選擇保持不變。每當我選擇build with parameters時,我都希望它能運行。

def findAMIs() { 
    // Find relevant AMIs based on their name 
    def sout = new StringBuffer(), serr = new StringBuffer() 
    def proc = '/usr/bin/aws --region eu-west-1 ec2 describe-images \ 
       ' --owners OWNER --filter Name=name,Values=PATTERN \ 
       ' --query Images[*].{AMI:Name} --output text'.execute() 
    proc.consumeProcessOutput(sout, serr) 
    proc.waitForOrKill(10000) 
    return sout.tokenize() 
} 

def AMIs = findAMIs().join('\n') 

pipeline { 
    // a declarative pipeline 
    agent any 

    parameters { 
     choice(name: 'Release', 
       choices: AMIs) 
    } 
    ... 
} 

編輯 我結束了使用jenkins-job-builder,具有廣泛選擇的參數。它不支持在目前的groovyScript參數,所以我修改了它https://review.openstack.org/#q,I0c6ac0b49c24b8d3afbc06b003847de2e043c2b8,n,z

+0

它只是不會立即改變,或者它永遠不會改變(在應該更改之後,您運行構建了多少次)?我想我已經看到它在第一次運行時不會生效。 – burnettk

+0

它永不改變。每次使用參數單擊構建時,我都會得到完全相同的選項,儘管我知道腳本返回不同的選項(手動運行時)。 – jarondl

+0

每次構建運行時都可以重新定義參數。沒有建立,沒有重新定義。 –

回答

5

對於用戶自定義輸入:

def findAMIs() { 
    return UUID.randomUUID().toString().split('-').join('\n') 
} 

node{ 
    def userInput = input(
     id: 'userInput', message: 'input parameters', parameters: [ 
      [ 
       $class: 'ChoiceParameterDefinition', 
       name: 'ami', 
       choices: findAMIs(), 
       description: 'AMI', 
      ], 
     ] 
    ) 

    echo ("Selected AMI :: "+userInput) 
} 
+0

嗨@daggett,感謝您的幫助。這是迄今爲止最接近的,但有兩個缺點1.它不適用於聲明式管道(我會嘗試修改它的工作)。 2.用戶流程比使用參數頁面構建要好得多。 我會試着讓它在聲明式管道上工作並回來給你 – jarondl

+0

@jarondl - 你是否得到了一個Declarative語法的例子工作?如果是這樣,請分享。 –

+0

嗨@EldadAK不,不幸的是我無法得到它的工作。看看編輯我的問題 - 我結束了使用JJB – jarondl

3

對於任何人都需要一個聲明管道語法選項,我發現了一個很好的解決方案在another question ,這對我有幫助。

這是我的建議。您應該能夠生成的代碼更動態的列表創建${WORKSPACE}/list文件

pipeline { 
    agent any 
    stages { 
     stage("Release scope") { 
      steps { 
       script { 
        // Prepare a list and write to file 
        sh "echo \"patch\nminor\nmajor\" > ${WORKSPACE}/list" 

        // Load the list into a variable 
        env.LIST = readFile (file: "${WORKSPACE}/list") 

        // Show the select input 
        env.RELEASE_SCOPE = input message: 'User input required', ok: 'Release!', 
          parameters: [choice(name: 'RELEASE_SCOPE', choices: env.LIST, description: 'What is the release scope?')] 
       } 
       echo "Release scope selected: ${env.RELEASE_SCOPE}" 
      } 
     } 
    } 
} 

我希望這有助於

2

還有另一種解決辦法:你可以「管道」之前使用「屬性」步 - 你也可以使用主動選擇插件:

properties([ 
    parameters([ 
     [ 
      $class: 'ChoiceParameter', 
      choiceType: 'PT_SINGLE_SELECT', 
      description: '', 
      filterable: false, 
      name: 'Release', 
      randomName: 'choice-parameter-21337077649621572', 
      script: [ 
       $class: 'GroovyScript', 
       fallbackScript: '', 
       script: '''// Find relevant AMIs based on their name 
        def sout = new StringBuffer(), serr = new StringBuffer() 
        def proc = '/usr/bin/aws --region eu-west-1 ec2 describe-images \ 
          ' --owners OWNER --filter Name=name,Values=PATTERN \ 
          ' --query Images[*].{AMI:Name} --output text'.execute() 
        proc.consumeProcessOutput(sout, serr) 
        proc.waitForOrKill(10000) 
        return sout.tokenize()''' 
      ] 
     ] 
    ]) 
]) 
pipeline { 
    ... 
} 

唯一的是你第一次開始你的構建,它會失敗。第二次啓動它應該是「帶參數的構建」。

希望它有幫助。

+0

這根本不工作。獲取'java.lang.IllegalArgumentException:無法實例化'錯誤,並且它不會像你寫的那樣第二次運行。 – yorammi

+0

剛剛在jenkins 2.73.2上進行了測試(但最初是爲jenkins 2.6x.x編寫的),它採用了Active Choice Plugin 1.4 - 它可以工作。 您是否編寫了一些流水線階段和步驟? –

+0

我已經解決了它,因爲我評論。切換到其他的東西;-) – yorammi