後処理スクリプトの例

後処理スクリプトは、JavaScript で作成されます。

後処理スクリプトには、スクリプトの状況を指定する Status プロパティーを設定する必要があります。 スクリプトは、Status プロパティーに任意の値を指定できます。例えば、スクリプトが成功したことを指定するには、後処理スクリプトで次のコマンドを実行します。
properties.put("Status","Success");

ステップの結果の確認

後処理スクリプトは、ステップの出力ログを調べて、その結果に基づいてコマンドを実行することができます。 次のコード・フラグメントで、scanner.register() は、 error at linethe value is というストリングを正規表現エンジンに登録し、次に、これらのストリングが見つかった場合はコマンドを実行します。 すべてのストリングが登録されたら、ステップの出力ログで 1 行ずつ 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());
     }

前のステップからのプロパティーの取得

以下のスクリプトでは、同一プロセス内に 2 つのステップがあります。 最初のステップの後処理スクリプトでは、2 つの出力プロパティーを作成し、これは StatusStep1Prop という名前になります。
if (properties.get("exitCode")==0)
  { properties.put("Status","Success"); }
else
  { properties.put("Status","Failure"); }

properties.put("Step1Prop","value1");
そして、2 番目のステップでこれらのプロパティーにアクセスできます。 例えば、最初のステップに Step1 という名前が付けられ、これにより出力プロパティーの StatusStep1Prop が指定されたとすると、それらのプロパティーにアクセスする以下のコードを含む Shell ステップを作成できるようになります。
echo Properties of previous step:
echo "${p:Step1/Status}"
echo "${p:Step1/Step1Prop}"
2 番目のステップは、その後処理スクリプトでこれらのプロパティーを使用することもできます。 例えば、以下の後処理スクリプトは、2 番目のステップのプロパティーに前のステップの Status プロパティーを含んでいます。
{ 
properties.put("Step2Prop","New property");
properties.put("Status","OK"); 
properties.put("StatusOfPreviousStep","${p:Step1/Status}"); 
}

フィードバック