后处理脚本的示例

后处理脚本是使用 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,那么可使用以下代码创建 Shell 步骤以访问这些属性:
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}"); 
}

反馈