Functions

Updated 5 days ago by Copado Solutions

Introduction

A function is a script that contains a series of commands and can be executed from Copado. These commands are executed in the Copado GCP backend. There are two types of functions:

  • Standard functions: Functions that are provided by Copado out of the box.
  • Custom functions: Functions that you can build to cover scenarios that are not supported by a standard function.

A function can be used in multiple scenarios. Here are some interesting use cases:

  • Run tests in tools that are open source and not available as a cloud service.
  • Deploy to other clouds.

Requirements

In order for you to be able to build and run a function, you need the following:

  • The Copado Functions permission set.
  • An API key.
  • An Admin or User license.
  • The Flow User checkbox selected for your user (this is required for non-sys admin users only).

Let’s go ahead and see how you can build a function.

Building a Function

Now that you know what a function is, let’s take a look at the tabs and fields included in the Function record and see how you can create a new function.

To create a function follow these steps:

  1. Navigate to the Functions tab and click on New.
  2. In the Information section you can provide general information about your function:
    1. Give your function a name and provide a unique API name.
    2. Select a function type. The available options are Standard or Custom.
    3. Add a description if desired.
    4. Add a version number, if desired.
      Function Information
  3. Next, provide the script with the commands you would like the function to execute in the Definition section. Scripts support bash language:
    Script Definition

    The script provided in the example above clones a public Git repository and reviews its size.

    Let’s take a look at some of the out-of-the-box parameters you can include in your script:
    1. copado -progress ‘{label}’
      1. Update the Progress Status field value in the Result record in Salesforce.
      2. Example: copado -progress ‘Retrieving files from git repository’ 
    2. copado git-get -depth 1 {branchName}
      1. Clone the remote branch to local file system.
      2. Example: copado git-get -depth 1 master.
    3. copado -u {filePath}
      1. Attach the local file to the Result record in Salesforce.
      2. Example: copado -u git-sizer.txt

This is what the Result record would look like if you are using these parameters:

Result Record
  1. In the Configuration section, you can further configure your function:
    1. Image Name: Provide the Copado image that will be used to run the function. This image includes a set of pre-installed apps and features required for the function to be executed, for instance, a Git library. This way, you don’t need to install and configure these applications prior to the function execution.
      1. Copado Function Core image
        1. URL: us.gcr.io/copado/copado-function-core
          The image contains the following:
          Python
          Bash
          Ruby
          NodeJS
          Git
          Git-sizer
          DPL
        2. This image contains an out-of-the-box communication layer with your Copado Salesforce org. As the job runs, the image sends platform events and can attach files to the result records. This is provided so you don’t need to invest time building this logic.
    2. Timeout: Every time a function is executed, Copado creates a Result record that includes the name of the function in the Job Type field and sets the status of the job to In Progress. Once the job is completed, Copado sends a platform event to Salesforce, and the Result record is updated accordingly. If you want the Result record to be updated to Failed if there is no response from the function’s job after a certain period of time (e.g. 60 minutes) you can define a timeout:
      Configuration Section
  2. Copado enables you to execute an action once the function has run. In the Callback section, you can specify whether you want to call an Apex class or a Salesforce flow. For instance, if you are running a scan, you can trigger a flow afterwards that sends an email to a user if the scan has been successful. Copado will pass the resultId and the functionId to the flow as input variables:
    Callback

  1. The minimum requirements to run a function are the script and the image. However, some functions require additional parameters. For instance, for the Git repository clone example above, you need to provide a public repository. You can add this in the Parameters tab:
    Parameters Tab


How did we do?