Building a DevOps Automation in a Flow

Updated 1 month ago by Copado Solutions

Introduction

Copado enables you to easily integrate with third-party tools and execute different automations as part of your DevOps process by means of automation templates and Salesforce Flow deployment tasks or steps.

In this guide, we will show you how to build a DevOps automation leveraging a Salesforce flow and execute it with Copado using different options.

But first things first. When building an automation, there are several questions you should be asking yourself to ensure you have everything you need before you get down to actually building your automation: 

  1. What kind of job do you want to execute?
  2. What endpoints do you need?
  3. Do you have a Swagger or Rest API to trigger this job?
  4. Do you have an endpoint to get the job status?
  5. What are the required parameters for executing the job?
  6. Where do you get these required parameters from?
  7. How do you implement these variables in the Salesforce Flow?
  8. How does the authentication to the API work?
  9. What if the Rest API is behind a firewall?
Selecting the Job to be Triggered

There are a wide range of jobs that you can execute as part of your DevOps process. Selecting the job you want to trigger should be your first step, as this will guide all the other steps you need to complete and will tell you which endpoints you need to look for.

In this guide you will be building a flow that will interact with the Copado API to trigger a Git snapshot to backup an org’s metadata and then check the status of the job to update the results in a Copado record.

The flow logic will be as follows:

Getting the Required Endpoints

Once you have decided the job you want to execute, you need to determine which APIs are necessary for your DevOps automation. Most DevOps tools have an API documentation that is publicly available so all you need to do is check the external tool’s API guide to see what actions are available. If there is no public API documentation, simply reach out to the vendor to request access to the information you need.

Copado has its own API guide which you can find here.

After reviewing the guide we have determined we need two endpoints for our example DevOps automation:

  1. Take a Git Snapshot: This endpoint executes the Git snapshot job and requires 2 parameters:
  • The Git Snapshot record Id.
  • A Copado API key. This key is used by Copado to authenticate your user and execute the job with your Copado org credential.

This endpoint returns the Copado backend job Id which is required by the next endpoint.

  1. Check Job Status: This endpoint checks the status of a Copado backend job and requires 2 parameters
  • The Copado backend job Id: This value is returned when the Git snapshot is executed.
  • A Copado API key.

This endpoint returns 3 important values:

  • isFinished (true/false)
    • This determines if the backend job has completed.
  • isSuccess (true/false)
    • This determines if the backend job completed successfully or not.
  • error
    • If the job completed unsuccessfully, an error message will be returned.

Now that you know which endpoints you need, you need to get the OpenAPI (OAS 2.0) schema of the endpoints. This information should be provided by the third-party vendor in the form of a URL (Service Schema Relative URL) or a json file. For this use case, download the schema json file for the Copado endpoints here.

Registering the External Service in Your Salesforce Org

Salesforce External Services allows you to connect Salesforce with an external source. In order to execute any of the operations your third-party tool offers in the Salesforce platform, you need to configure two things:

  • A Named Credential record. The named credential specifies the URL of a callout endpoint and its required authentication parameters.
  • An External Service record. This external service will be later used as an Action element in the flow.

This needs to be configured in the org where you will be later building your flow.

Configuring the Named Credential

Let’s create a named credential to authenticate to the API. APIs generally have two kinds of authentication methods:

  • API key or username and password values sent in the headers:
    • Set the Identity Type field to Named Principal and the Authentication Protocol field to Password Authentication.
    • Enter the username and password values.
    • Check the Generate Authorization Header checkbox.
  • API key or username and password sent as parameters:
    • In this case, your named credential should have the Identity Type field set to Anonymous and the Authentication Protocol field set to No Authentication.

The Copado API requires an API key sent as a parameter in order to authenticate the user. So in this case, you need to create the named credential with Identity Type set to Anonymous.

Follow the steps below to create the named credential:

  1. Go to Setup > Named Credentials and click on New Named Credential.
  2. Give your name credential a name, e.g. Copado API.
  3. Select Anonymous in the Identity Type field.
  4. Set the Authentication Protocol field to No Authentication.
  5. Select the Generate Authorization Header checkbox.

Your record should look like this:

Setting Up the External Service

Now that you have the named credential, you need to create the external service that will be used to create the actions in your flow. To do this, follow the steps below:

  1. Go to Setup > External Services and click on New External Service.
  2. Give your external service a name, e.g. CopadoAPI and provide a description if desired.
  3. Select the name credential you created in the previous section.
  4. Next, select Service Schema Complete JSON. Open the downloadable API schema json file provided in the section Getting the Required Endpoints with a text editor and copy and paste the content in the box.
  5. Click on Next.
    If you get an error message, this is most likely due to a formatting issue after the json file download or to blank spaces, use any free json validation tool to validate and copy the json content once it gets successfully validated.
  6. A screen with the actions generated by the schema will be displayed. Confirm everything looks good. In this case, there are two actions, one for the Git snapshot and another one to get the job status. Let’s click on Done.

Now that we have the named credential and the external service, let’s get down to the nitty gritty and build the flow.

Building the Salesforce Flow

We briefly described the flow logic in a previous section, buy let’s take a closer look at the different actions that will occur within the flow:

Git Snapshot Action

The first action the flow will trigger is the Git snapshot. Drag an Action element and select the external service you previously configured. In this case you could have used a Copado Apex action, but for third-party tool jobs, you need to use an external service.

As mentioned in the Introduction, the take Git snapshot endpoint needs two parameters (aka variables):

  • The Id of the Git Snapshot record in Copado.
  • The Copado API key. 

These two variables should be configured as text variables and should have the Available for input checkbox selected.

This is how the action element should look like:

Once the Git snapshot is executed, Copado will automatically generate a Result record with information about the execution of this job.

Updating the Result Record

Alright, the Git snapshot has been triggered, and Copado has created a Result record to store the job result. Now, you need to update the Result record to ensure it includes some relevant information. The Update Records element should look like this:

Have you noticed the Link field? This field allows you to easily navigate from the Result record to the job in the third-party tool. In this case, you want to navigate to the Git Snapshot record in Copado, where you can see the Git snapshot execution. To populate this field you need to combine a new resource of type Formula and the snapshot Id variable you have previously created. The formula should look like this:

LEFT({!$Api.Partner_Server_URL_340}, FIND( '/services', {!$Api.Partner_Server_URL_340}))

As you can see, the input value contains both the formula {!SalesforceOrgURL} and the {!snapshotId} variable.

Git Snapshot Triggered/Not Triggered

Now you need to check whether or not the Git snapshot has been successfully triggered. There will be two different paths depending on the outcome, so you need a Decision element with two outcomes:

  • Snapshot started
  • Snapshot not started

If the Git snapshot has started, you will then need a Pause element to wait for the job to finish. For this outcome, you need a Date/Time formula that will be used to specify the pause’s resume time. Let’s say you want to resume the flow after 3 minutes, this is how your formula would look like:

{!$Flow.CurrentDateTime}+(3/1440)

If the Git snapshot has not started, the Result record should be updated to Failed and an error message should be displayed.

Get Job Status Action

If the Git snapshot action has been successfully triggered, you need to move to the next step in the flow, which is to get the job status. Let’s add a new Action element to trigger the check job status action you configured in the external service. As mentioned in the Introduction, this action needs two parameters:

  • The Copado API key.
  • The Copado backend job Id. This Id is returned when the Git snapshot is executed.

Git Snapshot Completed/Not Completed

Alright, you have checked the status of the Git snapshot. So what’s next? Has the Git snapshot completed? If so, has it succeeded or failed? You need to add a Decision element with three outcomes:

  • Snapshot not completed
  • Snapshot completed with errors
  • Snapshot completed successfully

If the Git snapshot has not completed, you need to pause the flow and check again the job status after the pause, so connect the Decision element to the Pause element you added in a previous step.

If the Git snapshot has completed, you then need to update the Result record with the job result. If it has been successful, you need to configure the Update Records element as follows:

If the Git snapshot has failed, the status should be set to Failed and an error message should be added:

Triggering the Flow in Copado

Copado enables you to trigger flows and automations in different parts within your DevOps process. In this guide, you will be triggering the flow as part of a connection behavior, but you could also trigger it in the following instances:

  • As part of a deployment. You can add a Salesforce Flow deployment step or combine a group of automations in an automation template.
  • From a user story deployment task. The flow will be triggered every time the user story gets promoted to an upper environment in the pipeline.
  • From a custom button on the user story layout. This can be very helpful if you want, for instance, to execute a scan on the metadata you have just committed.
  • From a process builder, another flow or an Apex class using the Execute Automation Template Apex method.

Let’s go ahead and add the flow to a connection behavior. In this example, you will be doing a backup of your production org, so you will need to edit your production connection behavior or create a new one if you don’t have one. You can find the step-by-step process to build a new connection behavior in the Pipeline Configuration article. Here, we will just edit an existing connection behavior.

  1. Navigate to the production Connection Behavior record.
  2. In the Automations tab, click on New to add a new automation.
  3. Give your automation a name, e.g. Git Snapshot Flow and select Automation in the Type drop-down menu.
  4. The Execution Sequence field enables you to specify whether the automation should be triggered before or after the Git Promotion deployment. In this case, select After.
  5. Next, you need to select the metadata that should trigger the flow. In this case you can create a metadata group that includes all the metadata items.
  6. Finally, select the automation template that includes the flow step. If you don’t have one yet, click on New Deployment in the Automation Template lookup field to build a new automation template:
    1. Give your template a name and provide a description, if desired.
    2. Select the Active checkbox. Otherwise, you won't be able to use it.
    3. In the Steps tab, add a new step of type Salesforce Flow.
    4. In the Salesforce Flow drop-down menu, select the flow you have built in the previous section.
    5. This flow needs three parameters:
      1. Git snapshot Id
      2. Copado API key
      3. Deployment job Id
    6. Click on Save.

Your Salesforce Flow step should look like this:

This is what the automation in your connection behavior should look like:

From now one, whenever you deploy changes to production, a Git snapshot will be executed.


How did we do?