【Jenkins】パイプラインで、ドロップダウンリストでユーザからのパラメータ入力を受け付ける
はじめに
Jenkinsのパイプラインで、Linux上のどのフォルダを対象にジョブを動かすかをユーザに選択してもらいたかったので、ドロップダウンリストの出し方を調べてメモ。
内容
node('master') { stage('select directory path') { # ディレクトリ一覧を取得 def directoryPathList = sh( script: 'find /aaa/bbb/ccc/* -maxdepth 0 -type d' ,returnStdout: true ) def userInput = input( id: 'userInput', message: 'フォルダを選択して下さい', parameters: [ [$class: 'ChoiceParameterDefinition', name: 'directoryPath', choices: directoryPathList, description: 'a description.'] ]) echo "リカバリ対象:${userInput}" } }
↑のように、ChoiceParameterDefinitionを定義すればドロップダウンリストが出てきます。
choices
に、ドロップダウンリストの候補を改行区切りで指定します。
これでドロップダウンリストの候補からパラメータを選択してもらえるようになりました!
終わりに
シェルの実行結果を変数に保存したかったのですが、そもそも「シェルを実行した際の標準出力の内容を変数に格納する」かどうかを引数で指定(returnStdout)できたようです。
Pipeline: Nodes and Processes
ドロップダウンリストの定義は、ChoiceParameterDefinitionクラスのコンストラクタを指定しています。 ChoiceParameterDefinition (Jenkins core 2.107 API)
なので、同じようにコンストラクタを指定すればPasswordParameterDefinitionなどの他のDefinitionクラスも対応可能だと思います。