API Triggers: Dynamic List of Artifact Deployments

Hey all!

One of the things that I hear often is whether or not Harness has the ability to take an input which declares a dynamic set of services to be deployed (Example: I have 10 potential services and want to deploy any combination or count of those 10 services via a Trigger).

Although Harness does not have a way to take in a document (XML, JSON, YAML, etc.) and parse that for the dynamic execution type, I recently created a script that allowed a customer to do that.

Harness Setup
The first thing you will need is the Artifacts/Services set-up in Harness with the appropriate Environment and Service Infrastructure assigned to it (Make sure that the Service Infrastructure has an easy to use name).

SERVICES:
image
image
image

ENVIRONMENT/SERVICE INFRASTRUCTURE

Then you’ll need to create a Workflow (Rolling or Blue/Green) and then when you edit it, select the triple-dots on the right side of the overview to “Edit”

You’ll then want to click all of the image and turn them into image. This will allow you to templatize the workflow so that you can require those values in the Trigger to execute this Workflow.

The next thing you will need to do is create a Trigger to execute the Workflow with the following custom variables in the trigger (This will allow you to pass variables to the Workflow):

After this is done, you’ll need to grab the API cURL command

Script Setup
Here is the script to use (make sure you customize it with the appropriate pieces):
#!/usr/bin/env bash

################################################
#
#    CREATED BY: Bryan Feuling
#    DATE: 6/18/2019
#    PURPOSE: Demo Conditional API calls
#
################################################

# Services: docker, alpine, pov-docker-nginx
# Environments: alpine, docker, nginx
# Workflows: template-wf

WHURL="<webhook URL>?accountId=<accountId>"
APPID="<ApplicationID>"

nginx=("<env>" "nginx" "nginx")
alpine=("<env>" "alpine" "alpine")
docker=("<env>" "docker" "docker")

# Build NO:
# docker = 17-git
# nginx = stable
# alpine = 3.9

echo "Which Services would you like to deploy? "
echo "nginx"
echo "alpine"
echo "docker"
echo " "
read -a arr

APICALL() {
     curl -X POST -H 'content-type: application/json' --url "${1}" -d '{"application":"'${2}'","parameters":{"Environment":"'${3}'","Service":"'${4}'","ServiceInfra_Kubernetes":"'${5}'"},"artifacts":[{"service":"'${4}'","buildNumber":"'${6}'"}]}'
}

for i in "${arr[@]}"
do
     case "${i}" in
          "nginx")
               echo -e "\n\nNGINX Build/Version Number:"
               read nginxBN
               APICALL "${WHURL}" "${APPID}" "${nginx[0]}" "${nginx[1]}" "${nginx[2]}" "${nginxBN}"
               sleep 1
               ;;
          "alpine")
               echo -e "\n\nAlpine Build/Version Number:"
               read alpineBN
               APICALL "${WHURL}" "${APPID}" "${alpine[0]}" "${alpine[1]}" "${alpine[2]}" "${alpineBN}"
               sleep 1
               ;;
          "docker")
               echo -e "\n\nDocker Build/Version Number:"
               read dockerBN
               APICALL "${WHURL}" "${APPID}" "${docker[0]}" "${docker[1]}" "${docker[2]}" "${dockerBN}"
               sleep 1
               ;;
          *) echo -e "${i} is invalid";;
     esac
done

Execute Script
When you run the script, you’ll need to put in the desired services with a space in-between
Example:

Which Services would you like to deploy?
nginx
alpine
docker

nginx alpine

This will then run the templatized workflow twice with the designated options.

Don’t forget to Comment/Like/Share!