사후 처리 스크립트의 예

사후 처리 스크립트는 JavaScript로 작성됩니다.
사후 처리 스크립트는 스크립트의 상태를 지정하기 위한 Status 특성이 설정되어 있어야 합니다. 스크립트는 Status 특성에 대해 임의의 값을 지정할 수 있습니다. 예를 들어, 스크립트가 성공했음을 지정하려면 사후 처리 스크립트에서 다음 명령을 실행하십시오.
properties.put("Status","Success");

단계의 결과 확인

사후 처리 스크립트는 단계의 출력 로그를 검사하고 결과를 기반으로 하여 명령을 실행할 수 있습니다. 다음 코드 단편에서 scanner.register()error at linethe value is 문자열을 정규식 엔진에 등록한 다음 해당 문자열이 발견되면 명령을 실행합니다. 모든 문자열이 등록된 후에 단계의 출력 로그에서 한 라인씩 scanner.scan()을 호출합니다.

     properties.put("Status", "Success");

     //
     // Evaluate the built-in exitCode property, which indicates the exit code
     // of the script called by the plug-in step. Typically, if the value of
     // the exitCode property is non-zero, the plug-in step failed.
     //
     if (properties.get("exitCode") != 0) {
          properties.put("Status", "Failure");
     }
     else {

          //
          // Register a scanner to search for the text "error at line" in the log.
          // The first argument is a regular expression.
          //
          // The second argument, an inline function, is invoked once for
          // every line in the log output that matches the pattern. The "lineNumber"
          // variable contains the line number where the match occurred, and the
          // "line" variable is the full text of the line.
          //
          scanner.register("(?i)ERROR at line",  function(lineNumber, line) {

              //
              // In this case, we build up an "Error" property which
              // contains the text of all errors that are found. We find every
              // line starting with "error at line" and add it to this list.
              //
              var errors = properties.get("Error");
              if (errors == null) {
                  errors = new java.util.ArrayList();
              }
              errors.add(line);


              //
              // If a line starting with "error at line" is found, the step has
              // failed, so we set the special "Status" property to "Failure",
              // indicating to the UrbanCode Deploy server that the step should
              // be marked as a failure.
              //
              properties.put("Status", "Failure");
          });
         
          //
          // Multiple searches can be registered with the scanner. We add a
          // second search to look for some interesting text to set as an output
          // property.
          //
          // For example, if there is a line "The value is BLUE", then we end up
          // with an output property, "Value", with a value of "BLUE".
          //
          scanner.register("The value is", function(lineNumber, line) {
              var value = line.replace("The value is ", "");
              properties.put("Value", value);
          });
         
          scanner.scan();


          //
          // Convert the collected list of error strings into a single string and
          // set that as an output property.
          //
          var errors = properties.get("Error");
          if (errors == null) {
              errors = new java.util.ArrayList();
          }
          properties.put("Error", errors.toString());
     }

이전 단계에서 특성 검색

다음 스크립트에서는 동일한 프로세스 내에 두 단계가 있다고 가정합니다. 첫 번째 단계에 대한 사후 처리 스크립트는 StatusStep1Prop라는 이름의 두 출력 특성을 작성합니다.
if (properties.get("exitCode")==0)
  { properties.put("Status","Success"); }
else
  { properties.put("Status","Failure"); }

properties.put("Step1Prop","value1");
그러면 두 번째 단계가 해당 특성에 액세스할 수 있습니다. 예를 들어, 첫 번째 단계의 이름이 Step1로 지정되고 출력 특성의 이름이 StatusStep1Prop로 지정되면 다음 코드가 있는 쉘 단계를 작성하여 해당 특성에 액세스할 수 있습니다.
echo Properties of previous step:
echo "${p:Step1/Status}"
echo "${p:Step1/Step1Prop}"
두 번째 단계도 사후 처리 스크립트에서 이러한 특성을 사용할 수 있습니다. 예를 들어, 다음과 같은 사후 처리 스크립트는 이전 단계의 Status 특성을 두 번째 단계에 대한 특성에 포함시킬 수 있습니다.
{ 
properties.put("Step2Prop","New property");
properties.put("Status","OK");
properties.put("StatusOfPreviousStep","${p:Step1/Status}");
}

피드백