API Examples

2 minute readReference

To use the CloudBees CD Groovy API, you must first create an instance of ElectricFlow class. All CloudBees CD APIs for ec-groovy can be invoked through that instance.</p> Here is a basic script for invoking a CloudBees CD API.

// All the CloudBees CD APIs are accessible through the // ElectricFlow groovy class so you start by importing the class. import com.electriccloud.client.groovy.ElectricFlow // Instantiate ElectricFlow instance ElectricFlow ef = new ElectricFlow() // Invoke API def result = ef.getVersions() // Handle response println result.serverVersion.version

Specifying arguments

Most APIs expect one or more arguments. Arguments are specified as named parameters to the API.

// All the CloudBees CD APIs are accessible through the // ElectricFlow groovy class so you can start by importing the class. import com.electriccloud.client.groovy.ElectricFlow // Instantiate ElectricFlow instance ElectricFlow ef = new ElectricFlow() // Invoke API with applicable arguments def result = ef.getApplication(applicationName: 'Test Application', projectName: 'Default') // Handle response println result.application

Using argument models

// All the CloudBees CD APIs are accessible through the // ElectricFlow groovy class so you can start by importing the class. import com.electriccloud.client.groovy.ElectricFlow // Import the models for the API arguments from the client models package. import com.electriccloud.client.groovy.models.ActualParameter import com.electriccloud.client.groovy.models.Credential // Instantiate ElectricFlow instance ElectricFlow ef = new ElectricFlow() // In order to invoke runProcedure, first, build the list of actual parameters def params = [ new ActualParameter('param1', 'test12'), new ActualParameter(value: 'test12', actualParameterName: 'param1'), new ActualParameter(actualParameterName: 'param2', value: 'test24'), new ActualParameter(actualParameterName: 'testCred', value: 'testCred')] def creds = [new Credential(credentialName: 'testCred', userName: 'zzzxxx', password: 'pwd')] // Invoke API ef.runProcedure(projectName: 'Groovy Demo', procedureName: 'testProc', actualParameters: params, credentials: creds)

Error handling

If the API call to the CloudBees CD server fails, by default, it causes the groovy script to fail. If you want to handle the error and continue processing, you can register custom success and failure handlers with the API invocation.

import com.electriccloud.client.groovy.ElectricFlow ElectricFlow ef = new ElectricFlow() def result ef.getPipeline(pipelineName: 'Test Pipeline', projectName: 'Default', /*success handler*/ { response, data -> println('Found pipeline!') result = data }, /*failure handler*/ { response, data -> //assuming pipeline does not exist hence the failure println('Creating pipeline...') result = ef.createPipeline(pipelineName: 'Test Pipeline', projectName: 'Default') }) println('Pipeline retrieved or created: ' + result?.pipeline?.pipelineName)