Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

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.

Custom Gates and Steps

Override Deploy Target Group on All Projects with Plugin Suffix in Name

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.

snapshotProjects=stgexec.getSnapshotProjects();
for(snapshotProject in snapshotProjects)
{
    if(snapshotProject.getProjectName().endsWith("Plugin"))
    {
        stgexecinfo.setInstanceOverride(snapshotProject.getProjectId(), "WEBLOGIC")
    }
}

Override Deploy Target Group Based on Project Type

In this scenario, the pipeline should only be deploying to the EBS target group for projects with type EBS. This could be used as a custom gate or step.

def projects = stgexec.getSnapshotProjects();

for (def project in projects)
{
	if (project.getProjectType() != null)
	{
    	if ('EBS'.equals(project.getProjectType()))
    	{
        	stgexecinfo.setInstanceOverride(project.getProjectId(), project.getPackageName(), 'EBS');
    	}
	}
}

Validate Snapshot Contents

A pipeline can prevent deployments of any unexpected projects in the snapshot with a script similar to the following. This would best be implemented as a custom gate in the first stage of the pipeline.

expectedProjects = "HRWLSConfigurations,HRMDSObjects,HRSOAService,HRJavaApp,ValidatePaymentSB".split(",");

for (p in stgexec.getSnapshotProjects())
{
  if (!expectedProjects.contains(p.getProjectName()))
  {
    throw new RuntimeException("Project: " + p.getProjectName() + " was not expected.");
  }
}

return "SUCCESSFUL";

Deploy all projects sequentially across the target groups

This script will deploy the projects that are in the release one at a time to their target groups, one at a time. It will send all the projects and packages at the first priority level first, and then move on if none have failed to the next priority.

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);
  }
}

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.

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.

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.

def projectGroup = "Oracle Integrations"
return stgexec.getSnapshotProjects().findIndexOf { 
  projectGroup in it.getProjectGroupNames() 
} != -1
  • No labels