[Triggers] One Base Trigger for multiple Services, Artifact Sources, Environments, etc - with Templates

Howdy, gang! :rocket:

Introduction

This article is going to walk you through the steps required to make on base Custom Trigger handle multiple Services and other overridable parameters, with the help of a Templated (“Templatized”) Workflow and/or Pipeline.
We will rely on the power of Workflow Variables and Harness variable notation.
I will focus on two very common parameters to override: the Service, and the Infrastructure Definition. Buckle up! :rocket:

Tutorial

First Step

Let’s create two different services on Harness, as our starting point to the tutorial. I’ll use a K8s one, with our old friend Nginx Image.

Second Step

I’ll use one environment, with two Infrastructure Definitions. One will represent my GCP K8s Cluster, and the other my MBP Docker + K8s.

Third Step

Now, it’s time to create our Workflow! And enable the Template on my desired parameters
This way, we are going to create our Workflow Variables, which is mandatory to achieve what we want.

Fourth Step

Now, let’s head to the Triggers. We are going to create one like this:

  • Condition → Type → On Webhook Event
  • Actions
    • Execution Type: Workflow
    • Execute Workflow: <OUR_TUTORIAL_ONE>
  • Workflow Variables:

This way, we are creating a mechanism to provide those desired parameters on the Trigger POST Payload! Harness engine will understand that notation you can see on the screenshot.

Now, just go to Manual Trigger, and get the Show Curl Command available:

Fifth Step

This part is very important! Let’s work on the HTTP POST payload. I’ll change the values to fit our purpose.

I normally use Postman to test this kind of stuff. I grabbed my curl command and imported it into the tool.

BASE CURL CMD WITHOUT ANY CHANGES:

curl -X POST -H 'content-type: application/json' --url https://app.harness.io/gateway/api/webhooks/eXTksatNGTjqXpQMAvdtVTuEP8lG9e3CIliXjouZ?accountId=SSHyJhwkS1ym9wSLGyw2aw -d '{"application":"F6JWzq3dTwaaR8YGQarz6A","parameters":{"Environment":"Environment_placeholder","Service":"Service_placeholder","InfraDefinition_KUBERNETES":"InfraDefinition_KUBERNETES_placeholder"},"artifacts":[{"artifactSourceName":"Service_ARTIFACT_SOURCE_NAME_PLACE_HOLDER","service":"Service_PLACEHOLDER","buildNumber":"Service_BUILD_NUMBER_PLACE_HOLDER"}]}'

POSTMAN PARSED AND CONFIGURED CMD:

POST:
    https://app.harness.io/gateway/api/webhooks/eXTksatNGTjqXpQMAvdtVTuEP8lG9e3CIliXjouZ?accountId=SSHyJhwkS1ym9wSLGyw2aw

HEADERS:

BODY:
{"application":"F6JWzq3dTwaaR8YGQarz6A","parameters":{"Environment":"prod","Service":"Tutorial_Service_A","InfraDefinition_KUBERNETES":"gabs_mbp_k8s"},"artifacts":[{"artifactSourceName":"library_nginx","service":"Tutorial_Service_A","buildNumber":"stable-alpine"}]}

Last Step - Let’s test that!

I’ll first strike against Service_A, with that same payload, just to test if everything is working as expected. And it is!

But now, I’ve added another Artifact Source into Service_B. I want to deploy it on GCP, with my PROD environment, and using one of our example images instead of nginx. Take a look:

So, let’s change our payload:

And voilà:

Outcome

With that done, now you are able to use one base Custom Trigger to call a Templated Workflow and override parameters such as Service, Environment, Artifact Repo, Artifact Build, etc.

Further reading

Tags

<cloud: aws, azure, tanzu, gcp, on-prem, kubernetes>
<function: ci,cd>
<role: swdev,devops,secops,itexec>
<type: howto>
<category: triggers, gitops, templates>

2 Likes