Factory Procedures

On this page

Factory procedures allow dynamic behavior in process automation. To implement a factory procedure, you must first create a procedure that other procedures can use as a template. A procedure uses the template to construct new procedures at runtime. Instead of creating steps or complex scripts to account for all possible scenarios, procedures can be created dynamically based on user input.

This tutorial shows how to create factory (dynamic) procedures, which are procedures that modify themselves at runtime based on parameters passed into them.

To view an example procedure for this tutorial , go to the automation platform UI > Projects > EC-Tutorials- <version> , and click the procedure name.

An explanation for what you see and what is actually happening:

This tutorial contains 2 procedures and creates a third procedure "on the fly":

  • "Factory procedures"—this procedure uses the procedure template (Utility Procedure—Factory Template). This procedure has a parameter called " number of steps ". This parameter takes an integer and is used to specify how many new steps should be generated. Note: For this tutorial example, only one procedure is constructed to use the template procedure, but you may have multiple procedures that use the factory template procedure.

  • "Utility Procedure—Factory Template"—you do not see this procedure on this Procedure Details page, but it is included in the EC-Tutorials project. This is the factory procedure template used when generating new steps in the main procedure. This procedure implements a single step that writes some text to the log file. This procedure is designed to do a repetitive task, repeating the task a variable number of times. For example, you might need to run a test framework " n " number of times. Note: You can view this procedure in the EC-Tutorials project.

The main procedure, "Factory procedures", implements these steps:

  • The first step: "create dynamic procedure" is a Perl script that uses the Perl API to create a new procedure. The following script creates a new procedure containing the number of steps specified by the parameter:

my $procedureName = "Dynamically Created Procedure $[jobId]";

$ec→createProcedure("$[/myProject/projectName]",$procedureName);

for(my $i=0;$i<$[number of steps];$i++)

{

$ec→createStep("$[/myProject/projectName]", $procedureName, "Generated step $i", {subprocedure ⇒ "Utility Procedure—Factory Template",

actualParameter ⇒ {actualParameterName ⇒ 'text', value ⇒ "Executing generated step $i"}});

}

$ec→setProperty("/myJob/dynamicProcedure",$procedureName);

  1. First, the procedure generates a unique name for the new procedure to be created. The name is created by appending the value of the $[jobId] property string.

  2. A new procedure is created in the current project.

  3. Based on the number of steps to be created that were passed in as a parameter, a number of subprocedure steps are created in the procedure. The subprocedure steps call the utility procedure associated with this tutorial.

  4. The name of the newly created procedure is stored in /myJob/dynamicProcedure

    • The second step: "Run dynamically created procedure" calls the procedure that was created in Step 1. Step 2 appears to call nothing, but in fact it is calling a procedure referred by $[/myJob/dynamicProcedure].

Click Run to run this sample procedure and go to the Job Details page.

Running the procedure and specifying 3 steps to be generated, results in the following job being executed:

factoryproc

Notice the steps that execute in parallel in the dynamically created procedure: Generated step 0, Generated step 1, and Generated step 2.

Implementation

To create a "factory procedure" process in your project:

  1. Create a utility procedure—this procedure performs a task.

    • Create the steps you need for your task.

  2. Create a second procedure—the "factory procedure," providing a name of your choice for this procedure.

    • Create a minimum of 3 steps in this procedure.

    • step 1—create a new procedure that contains steps, each calling the utility template procedure.

    • step 2—this step executes the procedure created in step 1.

    • step 3—this step deletes the procedure created in step 1.

Each time you run the "factory procedure," it creates a temporary procedure to run the utility procedure.

Any changes you make within this tutorial will not be saved when you upgrade CloudBees CD/RO.