Google Cloud Functions with Harness Custom Deployment

Google Cloud Functions with Harness Custom Deployments

Hey canaries, your fellow canary @luisredda has come up with an awesome implementation with our new Custom Deployments feature. We are super excited to see what you all come up with. @luisredda has actually leveraged this feature to empower our users to deploy Google Cloud Functions!

Pre-Requisites

  • Permission to Manage Delegate Profiles
  • Permission to Create a Workflow, Service, and Environment
  • Have the Custom Deployment’s feature enabled (this feature is behind a feature flag, to enable, please contact Harness support and give it a spin!)

Delegate Profile needs to have the gcloud-cli configured

# gcloud cli
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key --keyring /usr/share/keyrings/cloud.google.gpg  add - && apt-get update -y && apt-get install google-cloud-sdk -y

1. Creating the Template

Navigate to the Harness Template Library, and Select “Add Template” and click and click on Custom Deployment Type

2. Configure the Custom Deployment Type Form

  • We will be configuring how Harness will map to the user’s infrastructure and query for deployed instances on the configured infrastructure.

  • User should configure the Display Name
  • Any variables needed for Infrastructure, could be ProjectID, Region
  • In the Fetch Instance field, we will provide the script to collect the instances deployed by Harness on the target infrastructure
  • The user will need to provide a Host Array path, this is the path in the JSON to the host object we are looking for.
#
# Script is expected to query Infrastructure and dump json
# in $INSTANCE_OUTPUT_PATH file path
#
# Harness is expected to initialize ${INSTANCE_OUTPUT_PATH}
# environment variable - a random unique file path on delegate,
# so script execution can save the result.
#
#

cat > $INSTANCE_OUTPUT_PATH <<_EOF_
{
  "data": [
    {
      "functionName": "${serviceVariable.functionName}"
    }
  ]
} 
_EOF_

This is what @luisredda configures for us down below:

@luisredda took it a step further and configured the Service Commands to deploy the Google Cloud Function. These commands enable the Google Cloud Function artifact to be pulled down the Google Cloud Storage bucket artifact and be deployed into the user’s Google Cloud Account.

Here are the commands strung together as a Service Command (Google Function Deploy):

Here are the step contents:

Setup Paths

export GOOGLE_APPLICATION_CREDENTIALS=/tmp/sales.json
mkdir -p /tmp/${service.name}
cd /tmp/${service.name}

Download Artifact

  • Harness will download configured artifact into a defined temp directory

Copy Artifact

Prepare Function

  • We will prepare the function by unzipping it in the target directory

Deploy Function

  • This is the command that actually deploys the function. Using the gcloud cli @luisredda scripts out the deployment command that Harness needs to deploy. It’s templatized with Harness variables so it can be leveraged by other Services of type Custom Deployment - Google Cloud Function
echo "deploying"

#gcloud config set project ${infra.custom.vars.ProjectID}

gcloud functions deploy ${serviceVariable.functionName} --runtime=${serviceVariable.runtime} --entry-point=${serviceVariable.entryPoint} --trigger-http --set-env-vars=NODE_ENV="${serviceVariable.runtime}" ${serviceVariable. extraArgs}

Cleanup

rm -rf /tmp/${service.name}

  • In this step, we clean up the temp folder after we are done with the deployment.

3. Configure the Service

  • User will navigate to their Harness Application and create a new service. This service will be of the type of Custom Deployment. So since we called the template Google Cloud Function, the Deployment type should appear like this:

Google Cloud Function (Custom)

@luisredda configures the artifact source like the Google Cloud Storage bucket to pull the Cloud Function artifact from. He has also set service variables to make the deployment configuration relatively generic. Meaning you can reuse this workflow without any hard-coded values for the deployment steps.

4. Configure the Environment

  • Where are we going to deploy this service? You should navigate to your environment section and configure an Environment and an Infrastructure Definition.
  • The Infrastructure definition takes in the Infrastructure variables you configured in the Custom Deployment Template in step 1.

@luisredda configures it like so:

5. Configure the Workflow

  • We are going to deploy this new service to the target infrastructure we defined. In order to do so, we will create a basic workflow.
  • This workflow will already come with Fetch Instances, which allows the user to track and manage the deployed instance. It also lets the user perform CV on their deployed instances as well.
  • In this example, we actually wired up Stackdriver now Google Operations to verify our deployment after deployment.

@luisredda configures the workflow for us down below:

  • We wired up our Service Commands to the workflow and it’s templatized so you can reuse it with other services.

  • He wires up the CV Step for Stackdriver (Google Operations) like so:

  • Once the Steps are all configured its time for the best part! DEPLOYMENT

6. Time to Deploy

  • Finally, after the configuration, we can deploy our Google Cloud Function Service!
  • Hit Deploy and you should see something like this!

@luisredda has already deployed this :rocket:

  • You can see that we are tracking the instance, “instance deployed is 1”
  • In the verification step we are able to identify the deployed function and run stack driver verification against it.

Conclusion

The new Harness Custom Deployments Type allows our users to bring in their own deployment styles and types and leverage Harness to manage the workloads and track the instances that are deployed. Users can now define how they want to deploy and how they want to monitor and verify it!

Check out the docs on Custom Deployments: https://docs.harness.io/article/g7m5a380kl-create-a-custom-deployment

2 Likes