2016-07-26 119 views
3

說明繼續:與超時輸入步驟,使用默認設置詹金斯管道所需詹金斯管道

  • 等待1分鐘輸入(「這是一個發佈版本是/否?」)
    • 如果輸入爲是,則做一個釋放型版本(帶版本號和部署)
    • 如果超時或用戶說沒有然後做一個測試建立
    • 如果用戶按下中止做正常中止

我當前的代碼是:

try { 
    timeout(1) { 
     input message: 'Do you want to release this build?', 
       parameters: [[$class: 'BooleanParameterDefinition', 
          defaultValue: false, 
          description: 'Ticking this box will do a release', 
          name: 'Release']] 
    } 
} catch (err) { 
    def hi = err.getCauses() 
    echo "Exception thrown:\n ${hi}" 

    echo err.getLocalizedMessage() 
    echo err.getCause() 
    echo err.toString() 
    echo err.getClass().getName() 
} 

但是這給了相同的(只要我可以告訴)的行爲,並抓住按下「取消」,並輸入超時爲用戶錯誤。

這裏是超時輸出:

[Pipeline] timeout 
[Pipeline] { 
[Pipeline] input 
Input requested 
[Pipeline] } 
[Pipeline] // timeout 
[Pipeline] echo 
Exception thrown: 
[[email protected]94906] 
[Pipeline] echo 
null 
[Pipeline] echo 
null 
[Pipeline] echo 
org.jenkinsci.plugins.workflow.steps.FlowInterruptedException 
[Pipeline] echo 
org.jenkinsci.plugins.workflow.steps.FlowInterruptedException 
[Pipeline] End of Pipeline 
Finished: SUCCESS 

當我按下「中止」這是除了拒絕後六角@

同樣這將是巨大的,如果輸入步驟可以可選在超時後繼續使用默認選項。

編輯:添加更多的打印到錯誤,試圖確定它的類型

+0

在手動中止和超時時引發的異常類是否正確? – izzekil

+0

我已經添加了一些打印到上面的代碼,嘗試打印所有我能想到的值,但它們看起來完全相同。也許當他們超時超時它取消輸入節點產生相同的輸出作爲按abort? –

回答

1

我最近實現了類似的行爲。
聲明式管道代碼請求BRANCH_TO_BUILD在啓動時提供的參數(只有在手動啓動時纔會使用,否則將使用默認值),那麼會激活Git分支的交互輸入。
所以用戶有多種選擇:

  • 內不進行任何操作(默認值將被使用)
  • 確認默認值
  • 輸入新值
  • 中止執行preccing [中止]

是的,請記住確認Groovy的功能Blazej寫道

pipeline { 
    agent none 

    parameters { 
     string(name: 'BRANCH_TO_BUILD', defaultValue: "develop", description: 'GIT branch to build') 
    } 

    environment { 
     GIT_URL = 'ssh://[email protected]/repo.git' 
     BRANCH_TO_BUILD_DEFAULT = 'develop' 
     BRANCH_TO_BUILD_REQUESTED = "${params.BRANCH_TO_BUILD}" 
    } 


    stage('Configure the build') { 
     agent none 
     steps { 
      echo "Prompt user for a branch to build (default: ${BRANCH_TO_BUILD_DEFAULT})" 
      script { 
       try { 
        timeout(time:30, unit:'SECONDS') { 
        BRANCH_TO_BUILD_REQUESTED = input(
         message: 'Input branch to build', 
         parameters: [ 
           [$class: 'TextParameterDefinition', 
           defaultValue: BRANCH_TO_BUILD_DEFAULT, 
           description: 'Branch name', name: 'Enter branch name (or leave default) and press [Proceed]:'] 
          ]) 
         echo ("User has entered the branch name: " + BRANCH_TO_BUILD_REQUESTED) 
        } 
       } catch(err) { // timeout reached or input Aborted 
        def user = err.getCauses()[0].getUser() 
         if('SYSTEM' == user.toString()) { // SYSTEM means timeout 
          echo ("Input timeout expired, default branch will be used: " + BRANCH_TO_BUILD_DEFAULT) 
          BRANCH_TO_BUILD_REQUESTED = BRANCH_TO_BUILD_DEFAULT 
         } else { 
          echo "Input aborted by: [${user}]" 
          error("Pipeline aborted by: [${user}]") 
         } 
       } 
      } 
     } 
    } 

    stage('Checkout') { 
     agent{node {label 'worker-1'}} 

     steps { 
      echo "Checkout will be done for Git branch: ${BRANCH_TO_BUILD_REQUESTED}" 
      checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_TO_BUILD_REQUESTED}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: ${GIT_URL]}]]) 
      } 
    } 

} 
1

你可以提取有關從異常原因拒絕用戶的信息。 org.jenkinsci.plugins.workflow.support.steps.input.Rejection有一個getUser()方法(它返回'系統'的超時和完整的用戶名中止)。

try { 
    timeout(time: 15, unit: 'SECONDS') { 
     input message: 'Do you want to release this build?', 
       parameters: [[$class: 'BooleanParameterDefinition', 
          defaultValue: false, 
          description: 'Ticking this box will do a release', 
          name: 'Release']] 
    } 
} catch (err) { 
    def user = err.getCauses()[0].getUser() 
    echo "Aborted by:\n ${user}" 
} 

請注意,這需要批准groovy沙箱(Manage Jenkins>進程內腳本審批)。