Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

A groovy script can be used in custom gates and steps, as well as for many configuration values on specific steps or gates. Additionally, a gate or step can be skipped by providing a Precondition groovy script. This page includes some sample scripts for these scenarios. See Pipeline Groovy Variables and Methods for a full listing of the accessible variables and methods in these groovy scripts.

Table of Contents

Custom Gates and Steps

...

The following script uses the setInstanceOverride method to set the deploy target group on the stage for each project in the snapshot with 'Plugin' at the end of the name. This could function as a custom gate or step.

Override deploy target group on all projects with Plugin suffix in name
Code Block
languagegroovy
snapshotProjects=stgexec.getSnapshotProjects();
for(snapshotProject in snapshotProjects)
{
    if(snapshotProject.getProjectName().endsWith("Plugin"))
    {
        stgexecinfo.setInstanceOverride(snapshotProject.getProjectId(), "WEBLOGIC")
    }
}

...

Code Block
import flexagon.fd.model.pojos.workflowrequest.*;
import flexagon.fd.model2.pojo.*;
import flexagon.fd.model.jaxb.releaseautomation.model.*;

def checkRunning(Long requestId){
  if(requestId == -1){
    return "DONE";
  }
    sleep(10000);
    List<WorkflowExecutionDataObject> wfes = FLEXDEPLOY.getWorkflowExecutionByWorkflowRequestId(requestId);
    for(wfe in wfes){
        if(wfe.getExecutionStatus() == 'Running' || wfe.getExecutionStatus() == 'Pending'){
            
            return "WAIT";
        }
        if(wfe.getExecutionStatus() == 'Failure'){
          return ("Deployment workflow execution " + wfe.getWorkflowExecutionId() + " for project " + wfe.getProjectId() + " failed.");
        }
    }
    return "DONE";
}

Long safeDeploy(Long projectId, String environmentCode, DeployOptions deployOptions){
  if(stgexecinfo.isUserSkipDeploy(projectId)){
    return -1;
  }

  deployOptions.setForce(stgexecinfo.isUserForceDeploy(projectId));
  
  try{
    return FLEXDEPLOY.deployProject(projectId,environmentCode,deployOptions);
  }catch(Exception e){
    if(e.getMessage() != null && e.getMessage().contains("FDML-00169")){
      return -1
    }
    return -2;
  }
}

RelSnapshotVersionDataObject findProjectVersionInfo(ReleaseProjectType p, projects){
  for (RelSnapshotVersionDataObject x in projects){
    if(x.getProjectId() == p.getProjectId() && x.getPackageName() == p.getPackageName()){
      return x;
    }
  }
  return null;
}

Map<Long, List<RelSnapshotVersionDataObject>> priorityLevels = new HashMap<>();
List<RelSnapshotVersionDataObject> projects = FLEXDEPLOY.getSnapshotDetails(SnapshotId).getVersionDataObjects()
for (p in stgexecinfo.getExecutionData().getReleaseProjects().getProject())
{
  RelSnapshotVersionDataObject rsvdo = findProjectVersionInfo(p, projects);
  if(rsvdo == null){
    //Skipping all utility projects. Those will not be run from this, so this is like deployall step.
    continue;
  }

  List<RelSnapshotVersionDataObject> prorityLevel = priorityLevels.getOrDefault(rsvdo.getPriority(), []);
  prorityLevel.add(rsvdo);
  priorityLevels.put(rsvdo.getPriority(), prorityLevel);
}
for (Map.Entry<Long, List<RelSnapshotVersionDataObject>> priorityLevel in priorityLevels)
{
  failed = [];
  for(rsvdo in priorityLevel.getValue())
  {
    DeployOptions deployOptions = new DeployOptions();
    deployOptions.setProjectStreamId(rsvdo.getProjectStreamId());
    deployOptions.setProjectVersionId(rsvdo.getProjectVersionId());
    if(!stgexecinfo.getInstanceOverride(rsvdo.getProjectId(),rsvdo.getPackageName()).isEmpty())
    {
      for( target in stgexecinfo.getInstanceOverride(rsvdo.getProjectId(),rsvdo.getPackageName()))
      {

        deployOptions.getInstanceIds().clear();
        deployOptions.getInstanceIds().add(FLEXDEPLOY.getTargetGroupByCode(target).getTargetGroupId())
        
        requestId = safeDeploy(rsvdo.getProjectId(),stgexec.getEnvironmentCode(),deployOptions);
        if(requestId == -2){
          failed += ("Could not deploy project " + rsvdo)
        }
        allDone = "WAIT";
        while (allDone == "WAIT")
        {
          //wait for it to finish before moving on to the next.
          allDone = checkRunning(requestId);
        }
        if(allDone != "DONE"){
          failed += allDone;
        }
      }
    } else
    {
      requestId = safeDeploy(rsvdo.getProjectId(),stgexec.getEnvironmentCode(),deployOptions);
        if(requestId == -2){
          failed += ("Could not deploy project " + rsvdo)
        }
      allDone = "WAIT";
      while (allDone == "WAIT")
      {
        //wait for it to finish before moving on to the next.
        allDone = checkRunning(requestId);
      }
      if(allDone != "DONE"){
        failed += allDone;
      }
    }
  }
  if (!failed.isEmpty()){
    throw new RuntimeException("" + failed.size() + " requests in priority group " + priorityLevel.getKey() + " failed. " + failed);
  }
}

Precondition Scripts

Execute Step Only for Specific Object Type

This could be used to conditionally call a Restart OACore or Generate Jar utility workflow only if OAF_JAVA files are deployed.

Code Block
languagegroovy
def deployedTypes = stgexec.getProjectWorkflowOutputValues("DeployedTypes");

for (String listItem : deployedTypes)
{   
  if (listItem.contains('OAF_JAVA'))
  {
    return true;
  }
}

return false;

Execute Gate only if Snapshot Projects contain a specific Project Group

This is useful when you could want technology/team specific approvals which would only make sense to do when that particular Project Group/Technology is included in the snapshot.

...

languagegroovy

...

Change Workflows and Target Groups for all the projects in a specific folder.

This is useful when you have many projects in a folder, that will have the same build/deploy workflow, and the same build and deploy instance.

Code Block
import flexagon.fd.model.pojos.rest.topology.integrations.SCMInstancePojo;
import flexagon.fd.model.pojos.rest.properties.PropertyValuePojo;
import flexagon.fd.model.pojos.rest.project.*;
import flexagon.fd.core.enums.SCMTypeEnum;
import flexagon.fd.core.enums.ProjectTypeEnum;
import flexagon.fd.model.pojos.rest.properties.PropertyValuePojo;
import flexagon.fd.model.pojos.folder.*;
import flexagon.fd.model2.pojo.ProjectDataObject;

import flexagon.fd.services.groovy.functions.*;
 
FlexDeploy2Functions FLEXDEPLOY = new FlexDeploy2Functions();

def projectDataObject = new ProjectDataObject();
projectDataObject.setFolderId(8996557);

def resProjects = FLEXDEPLOY.searchProjects(projectDataObject,10,0).getItems();

def proj = new ProjectPojo();
 
def buildInfo = new ProjectBuildInfo();
def deployInfo = new ProjectDeployInfo();
 
buildInfo.setWorkflowId(675528L);
buildInfo.setInstanceId(303656L);
 
deployInfo.setWorkflowId(5885144L);
deployInfo.setInstanceIds([303656L]);
 
proj.setBuildInfo(buildInfo);
proj.setDeployInfo(deployInfo);

for(def project:resProjects)
{
def projectId = project.getProjectId();
FLEXDEPLOY.patchProject(projectId, proj);
}

Precondition Scripts

Execute Step Only for Specific Object Type

This could be used to conditionally call a Restart OACore or Generate Jar utility workflow only if OAF_JAVA files are deployed.

Code Block
languagegroovy
def deployedTypes = stgexec.getProjectWorkflowOutputValues("DeployedTypes");

for (String listItem : deployedTypes)
{   
  if (listItem.contains('OAF_JAVA'))
  {
    return true;
  }
}

return false;

Execute Gate only if Snapshot Projects contain a specific Project Group

This is useful when you could want technology/team specific approvals which would only make sense to do when that particular Project Group/Technology is included in the snapshot.

Code Block
languagegroovy
def projectGroup = "Oracle Integrations"
return stgexec.getSnapshotProjects().findIndexOf { 
  projectGroup in it.getProjectGroupNames() 
} != -1

Create Comments

Use FLEXDEPLOY function createComment with FdCommentDataObject as an argument to create comment. FdCommentDataObject contains commentId, objectId, commentText, parentObjectId, objectType and versionNumber.

The below table will explain the Object Types supported and inputs that needs to be passed based on the type.

Object Type

Object Id

Parent Object Id

PIPELINE_GATE_EXEC

Pipeline stage gate id

Pipeline exec id

PIPELINE_STEP_EXEC

Pipeline stage step id

Pipeline exec id

PIPELINE_GATE

Pipeline stage gate id

 

PIPELINE_STEP

Pipeline stage step id

 

RELEASE

Release definition id

 

WORKITEM

Flx WorkItem id

 

Below sample script creates a comment for pipeline gate execution object type.

Code Block
languagegroovy
import flexagon.fd.model2.pojo.*;

FdCommentDataObject dObj = new FdCommentDataObject();
dObj.setObjectId(<Pipeline stage gate id>);
dObj.setParentObjectId(<Pipeline exe id>);
dObj.setObjectType("PIPELINE_GATE_EXEC");
dObj.setCommentText("New comment in PIPELINE GATE EXECECUTION through webhook");
FdCommentDataObject result = FLEXDEPLOY.createComment(dObj);

Release Methods from FlexDeploy2Functions class

public List<RelProjectDataObject> createReleaseProjects(Long pReleaseId, List<RelProjectDataObject> pPojos)

Use FLEXDEPLOY function createReleaseProjects with List<RelProjectDataObject> as an argument to create release projects.
RelProjectDataObject contains RelProjectId, BuildEnvironmentId, ProjectId, ProjectBranchId, RelDefinitionId, PackageName, ProjectGroups, DeployPriority

Prepare the RelProjectDataObject with above details and pass into the createReleaseProjects.

Above method can create multiple release projects in the release. If you want to create only one project in the release, then you can use the below method.

public RelProjectDataObject createReleaseProject(RelProjectDataObject pPojo)

Below sample script adds the project to release.

Code Block
import flexagon.fd.model2.pojo.*;

//For package based project 
RelProjectDataObject dataObj = new RelProjectDataObject();
def streamId = FLEXDEPLOY.findStreamId(<project id>,<stream name>);
dataObj.setBuildEnvironmentId(<build env id>); // not mandatory
dataObj.setDeployPriority(<set any integer number>);  // not mandatory
dataObj.setPackageName(<package name>);
dataObj.setProjectId(<project id to be added>);
dataObj.setProjectBranchId(<streamId>);
dataObj.setRelDefinitionId(<release definition id>);
dataObj.setProjectGroups(<project group id>); //set project group id as string
LOG.info("Release Project added to release ---- "+FLEXDEPLOY.createReleaseProject(dataObj));

public RelProjectDataObject patchReleaseProject(RelProjectDataObject pRelProjectDataObj)

Use FLEXDEPLOY function to patch the Release Project details. It should take RelProjectDataObject as an argument to patch the release project details.

Prepare the RelProjectDataObject with RelDefinitionId and other required filed to be patched.

Below sample script can patch the release project details.

Code Block
import flexagon.fd.model2.pojo.*;

RelProjectDataObject dataObj = new RelProjectDataObject();
def streamId = FLEXDEPLOY.findStreamId(<project id>,<stream name>);
dataObj.setBuildEnvironmentId(<build env id>);
dataObj.setDeployPriority((<set any integer number>);
dataObj.setProjectBranchId(streamId);
dataObj.setRelProjectId(<release project id to be patched>);
dataObj.setProjectGroups(<project group id>); //set project group id as string
LOG.info("Release Project patched ---- "+FLEXDEPLOY.patchReleaseProject(dataObj));

public RelDefinitionDataObject copyRelease(CopyReleaseInput pCopyReleaseInput)

Use FLEXDEPLOY function copy Release that copies from source release details to the target or new release. created through copyRelease function.
It takes CopyReleaseInput as an argument. CopyReleaseInput contains SourceRelDefinitionId, TargetFolderId, TargetReleaseName along with boolean type inputs are CopyTags, CopyWorkItems, CopyProjects, CopyOverridenRoleMembers, CopyOverridenSettings. Boolean type fields are optional, by default they are set to false so if any of those fields are set to true then those will be copied from source release to target release.

Below sample script can create a target release from the source release details.

Code Block
import flexagon.fd.model2.pojo.*;

RelDefinitionDataObject dataObj = new RelDefinitionDataObject();
CopyReleaseInput input = new CopyReleaseInput();
input.setSourceRelDefinitionId(<source release efinition id>);  //required
input.setTargetFolderId(<folder id where you want to create a release>);  //required
input.setTargetReleaseName(<target release name>);  //required
input.setCopyTags(true); // as per requirement set it to true, default is false
input.setCopyWorkItems(true); // as per requirement set it to true, default is false
input.setCopyProjects(true); // as per requirement set it to true, default is false
input.setCopyOverridenRoleMembers(true); // as per requirement set it to true, default is false
input.setCopyOverridenSettings(true); // as per requirement set it to true, default is false
dataObj = FLEXDEPLOY.copyRelease(input);
LOG.info("created release id = "+dataObj.getRelDefinitionId() + " release name = "+dataObj.getRelName() + " release status = "+dataObj.getRelStatus());