後處理 Script 的範例

後處理 Script 在 JavaScript 中撰寫。
後處理 Script 必須設定 Status 內容,以指定 Script 的狀態。Script 可以為 Status 內容指定任何值。例如,若要指定 Script 成功,請在後處理 Script 中執行下列指令:
properties.put("Status","Success");

檢查步驟的結果

後處理 Script 可以檢查步驟的輸出日誌,並根據該結果執行指令。在下列程式碼片段中,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());
     }

從前一個步驟擷取內容

下列 Script 假設同一程序中有兩個步驟。第一個步驟的後處理 Script 會建立兩個輸出內容,名為 StatusStep1Prop
if (properties.get("exitCode")==0)
  { properties.put("Status","Success"); }
else
  { properties.put("Status","Failure"); }

properties.put("Step1Prop","value1");
然後,第二個步驟可以存取那些內容。例如,如果第一個步驟名為 Step1,並指定輸出內容 StatusStep1Prop,則您可以使用下列程式碼來建立 Shell 步驟,以存取那些內容:
echo Properties of previous step:
echo "${p:Step1/Status}"
echo "${p:Step1/Step1Prop}"
第二個步驟也可以在其後處理 Script 中使用那些內容。例如,下列後處理 Script 在第二個步驟內容中包括前一個步驟的 Status 內容:
{ 
properties.put("Step2Prop","New property");
properties.put("Status","OK"); 
properties.put("StatusOfPreviousStep","${p:Step1/Status}"); 
}

意見