2017-05-25 59 views
0

我想通過Jenkinsfile獲取用戶輸入,但我無法在shell中使用它。下面是代碼:如何在shell中獲取用戶輸入數據(Jenkinsfile)

def userInput = timeout(time:60, unit:'SECONDS') {input(
    id: 'userInput', message: 'URL Required', parameters: [ 
    [$class: 'TextParameterDefinition', defaultValue: '', description: 'URL', name: 'url'], 
    ]) 
    } 

node{ 
     echo "Env jsfsjaffwef:"+userInput) //this works 
     echo "${userInput}" //but this does not 
     sh ''' 
      python test.py ${userInput} 
     ''' 
} 
+0

你真的確定'echo'$ {userInput}「'does not _ work does not work?什麼是「不工作」順便說一句? – StephenKing

回答

2

要小心string interpolation:變量將雙引號("..",或者多行變種"""內被替換),但不在單引號內('..''''..''')。所以sh步驟不應該取代它,上面的echo應該正確。

def userInput = timeout(time:60, unit:'SECONDS') {input(
    id: 'userInput', message: 'URL Required', parameters: [ 
    [$class: 'TextParameterDefinition', defaultValue: '', description: 'URL', name: 'url'], 
    ]) 
    } 

node { 
     echo "Env jsfsjaffwef:"+userInput) //this works 
     echo "${userInput}" // this should have also worked before 
     sh """ 
      python test.py ${userInput} 
     """ 
} 

因此,請確保您正確應用正確的引號,而不是隻是將其替換爲與人們在此處提出的建議相比。

0

以下爲我工作的詹金斯2.62:

def userInput = input(
    id: 'userInput', message: 'URL Required', parameters: [ 
    [$class: 'TextParameterDefinition', defaultValue: '', description: 'URL', name: 'url'], 
    ]) 

node('master') { 
    sh "echo userInput is: ${userInput}" 
} 
+0

我試圖用類似的東西,但對我沒有工作:SH「」「 回聲‘$ {} userInput’ ‘’」 –

+0

如果您刪除在$ {} userInput引號會發生什麼? – Jacob