2017-11-02 2 views
0

으로 읽습니다. Jenkins 파이프 라인에서 런타임에 대화 형 입력을 제공하는 옵션을 사용자에게 제공하려고합니다. Groovy 스크립트에서 사용자 입력을 읽을 수있는 방법을 알고 싶습니다.Jenkins 파이프 라인의 대화 형 입력을 변수

내가 다음 설명서를 참조하고 있습니다 : https://jenkins.io/doc/pipeline/steps/pipeline-input-step/

편집-1 :

pipeline { 
    agent any 

    stages { 

     stage("Interactive_Input") { 
      steps { 
       script { 
       def userInput = input(
       id: 'userInput', message: 'Enter path of test reports:?', 
       parameters: [ 
       [$class: 'TextParameterDefinition', defaultValue: 'None', description: 'Path of config file', name: 'Config'], 
       [$class: 'TextParameterDefinition', defaultValue: 'None', description: 'Test Info file', name: 'Test'] 
       ]) 
       echo ("IQA Sheet Path: "+userInput['Config']) 
       echo ("Test Info file path: "+userInput['Test']) 

       } 
      } 
     } 
    } 
} 
: 일부 시험 후

내가이 작업을 가지고 요청은 샘플 코드로 우리를 도와

이 예제에서는 사용자 입력 매개 변수를 에코 (인쇄) 할 수 있습니다.

echo ("IQA Sheet Path: "+userInput['Config']) 
echo ("Test Info file path: "+userInput['Test']) 

하지만 이러한 매개 변수를 파일에 쓰거나 변수에 할당 할 수 없습니다. 우리는 어떻게 이것을 할 수 있습니까?

+0

현재 코드를 표시하십시오. 최소한 기본 버전을 갖추는 것은 어렵지 않습니다. 맞습니까? – StephenKing

답변

0

이것은 input() 사용에 대한 가장 간단한 예입니다.

  • 무대보기에서 첫 번째 단계를 가리키면 '계속 하시겠습니까?'라는 질문이 나타납니다.
  • 작업이 실행되면 콘솔 출력에 비슷한 메모가 나타납니다.

진행 또는 중단을 클릭 할 때까지 작업이 일시 중지 된 상태에서 사용자 입력을 기다립니다.

pipeline { 
    agent any 

    stages { 
     stage('Input') { 
      steps { 
       input('Do you want to proceed?') 
      } 
     } 

     stage('If Proceed is clicked') { 
      steps { 
       print('hello') 
      } 
     } 
    } 
} 

매개 변수 목록을 표시하고 사용자가 하나의 매개 변수를 선택할 수있는 고급 사용법이 있습니다. 선택을 기반으로 계속해서 QA 또는 프로덕션에 배치 할 수있는 멋진 논리를 작성할 수 있습니다. 변수에 저장에 대한 링크

+0

자세한 설명은 @dot에 감사드립니다. 원래 질문에서 EDIT-1을 참조하십시오. 사용자 입력을 변수에 지정하거나 파일에 쓰고 싶습니다. 내가 어떻게 이걸 얻을 수 있니? – Yash

+0

사용자 입력을 변수에 할당 할 때 어떤 역할을합니까? 'def variable = userInput [ 'config']' – dot

0

에서 언급 한 바와 같이

는 다음 스크립트는 또한 StringParameterDefinition, TextParameterDefinition 또는 BooleanParameterDefinition 및 많은 다른 사람을 사용하여 사용자가

stage('Wait for user to input text?') { 
    steps { 
     script { 
      def userInput = input(id: 'userInput', message: 'Merge to?', 
      parameters: [[$class: 'ChoiceParameterDefinition', defaultValue: 'strDef', 
       description:'describing choices', name:'nameChoice', choices: "QA\nUAT\nProduction\nDevelop\nMaster"] 
      ]) 

      println(userInput); //Use this value to branch to different logic if needed 
     } 
    } 

} 

를 선택할 수있는 드롭 다운 목록을 렌더링 파일을 가지고 있다면 다음과 같은 것을 시도해보십시오.

pipeline { 

    agent any 

    stages { 

     stage("Interactive_Input") { 
      steps { 
       script { 

        // Variables for input 
        def inputConfig 
        def inputTest 

        // Get the input 
        def userInput = input(
          id: 'userInput', message: 'Enter path of test reports:?', 
          parameters: [ 

            string(defaultValue: 'None', 
              description: 'Path of config file', 
              name: 'Config'), 
            string(defaultValue: 'None', 
              description: 'Test Info file', 
              name: 'Test'), 
          ]) 

        // Save to variables. Default to empty string if not found. 
        inputConfig = userInput.Config?:'' 
        inputTest = userInput.Test?:'' 

        // Echo to console 
        echo("IQA Sheet Path: ${inputConfig}") 
        echo("Test Info file path: ${inputTest}") 

        // Write to file 
        writeFile file: "inputData.txt", text: "Config=${inputConfig}\r\nTest=${inputTest}" 

        // Archive the file (or whatever you want to do with it) 
        archiveArtifacts 'inputData.txt' 
       } 
      } 
     } 
    } 
} 
관련 문제