Your First CI/CD Plugin

Learn how to create your first Harness CI / Drone plugin to deploy on the Harness CD Platform leveraging Harness' APIs.

One of the items that make Harness CI / Drone so flexible is the ability to quickly author plugins to accomplish goals. Let’s dive into making a plugin that will trigger a Harness Workflow directly. If this is your first time looking at the Harness Platform, accomplishing even complicated goals is easy and convention-based.

The goal of this example is to have Harness CI / Drone build a Python-based plugin then push a GoLang Docker Image to Docker Hub. Once the push step is over, Harness CI / Drone will call Harness CD directly and deploy the artifact to an awaiting Kubernetes cluster. Supercharging your DevOps skills to an elite level going from idea to production.

Let’s Get Started – Background and Approach

There are a few moving pieces that we will be working through in the example. You need a Harness CI / Drone instance, a Docker Hub Account, a GitHub account, and of course a Kubernetes cluster, which could even be Minikube.

Like always, you can follow along with the blog and/or watch the video and as a special treat, even look at the final product before diving in.

 

Harness CI / Drone has a robust plugin architecture that allows for almost anyone to create and leverage plugins. Harness CI / Drone plugins are pre-defined tasks that are configured as steps in your CI/CD pipeline. In this example, we are going to create a Harness CI / Drone plugin in Python that will call the Harness CD API to trigger a Harness Workflow.

If leveraging the example Harness CI / Drone GitHub project, you will have to edit the information to be your own Docker Registry for where the Docker Push goes. I will be referencing mine below.

DockerHub

If leveraging the example, we will assume that we know the name of the artifact that eventually we want to deploy. If you bring your own, can modify the Harness CD steps to match your own artifact.

Harness CD Prep

If this is your first time using the Harness Platform, the quickest way to get started is to leverage a Kubernetes Delegate.

The first step of getting a Harness Delegate installed in your Kubernetes Cluster is pretty straight forward. Once signed up / signed up, can install a Harness Delegate.

Setup -> Harness Delegates -> Install Delegate. Then Select Kubernetes YAML and give a name such as “cicd”.

Install Kubernetes Delegate

Once downloaded, we can expand the TAR and install the Harness Kubernetes Delegate. Instructions to install will be in a README.txt inside the expanded folder.

TAR Package

To Install the Kubernetes Delegate, run kubectl apply -f harness-delegate.yaml.

Kubernetes Delegate Install

After a few moments, your Harness Delegate will be ready. You can validate by going back to Setup -> Harness Delegates and seeing your new Delegate on the list.

New Delegate Is Here!

Next can wire up a Kubernetes cluster for Harness to deploy to by adding the Kubernetes cluster as a Cloud Provider.

Setup -> Cloud Providers + Add Cloud Provider -> Kubernetes Cluster.

Since the Harness Delegate is deployed on the cluster, we can easily inherit the details from the Delegate itself.  Name the cluster something such as “CICD Kubernetes Cluster”, hit Test, then Next, then Submit.

Add Kubernetes Cluster Cloud Provider

With the Kubernetes cluster wired into Harness, the next step is to start to define the Workflow that Harness will execute. Harness works on an abstraction model where you define the pieces needed to complete your Workflow. The first step of that is to create a Harness Application, which is the home to all of the associated abstraction pieces.

Setup -> Applications + Add Application. Can name the Harness Application “DevOps Dojo”.

DevOps Dojo - Add Application

Once the Harness Application has been created, can start to build out the abstraction model. A good starting point is the Harness Service.

Setup -> DevOps Dojo -> Services + Add Service. Can name the Harness Service “Amazing App”. The Deployment Type will be Kubernetes.

DevOps Dojo - Add Service

Inside the Amazing App Service, + Add Artifact Source from a Docker Registry.

Service Overview

Here you can wire together our example application/project or one that you have created. If you don’t have an existing artifact that you want to build, can return to this step later on during the Harness CI / Drone configuration.  I have an application on public Docker Hub called “rlachhman/amazingapp”.

Artifact Source - Docker Registry

Once you hit Submit, the scaffolding will be there for the deployment and no need for additional configuration.

Service Overview

With the Service out of the way, we can wire a Harness Environment to deploy to.

Setup -> DevOps Dojo -> Environments + Add Environment with the name of “The Greatest Environment”.

Add Environment

Once you hit Submit, next can wire an Infrastructure Definition to define an environment to deploy to.

Environment Overview

Next, click + Add Infrastructure and add the Kubernetes cluster.  The Cloud Provider Type and Deployment Type will be Kubernetes. Can give the name “The Greatest K8s Cluster”.

Infrastructure Definition

Once you hit Submit, can wire together a Harness Workflow to define the steps to deploy.

Setup -> DevOps Dojo -> Workflows + Add Workflow. The Workflow Type will be Rolling Deployment. Select the Environment, Service, and Infrastructure Definition that was created in the previous steps. Give the name “Deploy Amazing App.”

DevOps Dojo - Add Workflow

Once you hit Submit, the Workflow Overview should look like the following.

Workflow Overview

Lastly, let’s create a Harness Trigger so that our Harness CI / Drone Plugin has an endpoint to execute on. The Harness Trigger will execute the Workflow that has just been created.

Setup -> DevOps Dojo-> Triggers + Add Trigger with a name of “Web Deploy Trigger”.

DevOps Dojo - Add Trigger to Your Plugin

Once you hit Next, the Condition will be on a Webhook Event, which we will generate from the Harness CI / Drone Plugin that we will be creating.

Condition

Once you click Next, the Action will be to execute the Harness Workflow which was just created. In the payload of the webhook will define the version/tag.

Trigger Action for Your First Plugin

Once you click Next, we can validate the Harness Trigger configuration.

Trigger Configuration

When you click Submit, the trigger will be created and expose the Payload Information you need.

We can get the Payload information which we will build the Harness CI / Drone Plugin to by clicking on Manual Trigger.

Manual Trigger

Lastly, we can set up an API Key to authenticate a remote call to the Harness Trigger. To create an API Key, head to Security -> Access Management -> API Keys + Add API Key. Can name the API Key “Harness CI / Drone API Key.”

Add API Key to Your First Plugin

Once you hit Submit, you can view the masked key.  

Masked API Key

With the Harness CD steps out of the way, its time to look at what you could/will be building to deploy then looking at creating a plugin to trigger the deployment.

Creating an Application to Deploy

If you do not have a don’t have a running Harness CI / Drone instance don’t worry, not difficult to accomplish. We have a detailed blog and video to get you started. We will split the GitHub projects into two. One will be for the creation of the Harness CI /Drone to Harness CD Trigger Plugin and another will be a more typical project publishing an artifact and leveraging your newly created plugin.

If you do not have an existing application to deploy and want to seed one in your Docker Registry, you can modify a few lines in the example repository. Fork the repository into your own GitHub and can make a few changes to the Drone.yaml. If you do that, make sure to follow the activating Harness CI / Drone steps for your specific repository and credentials.

To kick off an initial Harness CI / Drone Build and Push, simply modify the configuration / Drone.yaml. To explicitly build to a specific version, can add tags to Drone.yaml which works well with the example. In the example, let’s start by pushing build version 1.0.1. Make sure to update your repository information also.

Docker Publish

Once you make a change to the Drone.yaml you are on your way to starting to automate the deployment.

Update Drone.yaml

When the publish step completes, a new version of an artifact will be on your Docker Registry.

New Artifact

With the ability to create an artifact and the Harness CD wirings in place, now it is time to create your first Harness CI / Drone Plugin.

Harness CI / Drone Plugin Creation

The example plugin will be a Dockerized Python application that will formulate a request to call the Harness CD API to deploy. If you are new to writing Python, a good development environment for Python is Pycharm.

Pycharm

There are several ways to call the Harness API, for example, you can use the GraphQL API – but in my example, I am taking advantage of CURL like payload with JSON.

The main mechanism of passing items between your YAML configuration and the plugin itself can be described in the GoLang example in the documentation. In the “settings” section of the YAML configuration that references the plugin, getting environmental variables that follow “PLUGIN_FIELD” is the wiring between the two.

Pipeline Variables

Looking at the completed example plugin project, the structure is almost identical to if you are writing a custom application and leveraging Harness CI / Drone to deploy. In the plugin Drone.yaml we have easily switched to a Python Build and publishing our newly minted artifact to a new location.

Publish

Making modifications or initially committing your Harness CI / Drone YAML will kick off a build once wired to the repository.

Update Drone.yaml

The plugin is ready to rock.

Your First Plugin Is Ready!

With the plugin all ready, you can now start to leverage the plugin in your other projects.

Configure Your First Plugin

After all the work you put into the plugin creation, time to enjoy the spoils of victory. For this Harness specific example, there are a few fields that have to be wired in through the plugin. You can decide what makes sense to expose in the YAML vs using the Harness CI / Drone secrets facilities. Items such as the build/tag ID are safe to include but most account specific items should be referenced as a secret.

A reminder of the Manual Trigger pane which contains most of the details.

Harness CD -> Setup -> DevOps Dojo -> Triggers -> Web Deploy Trigger then click on Manual Trigger.

Manual Trigger

FieldWhere to Find Account_IDIn Harness CD, your Harness Account ID, in the URL of your account, or the Manual Trigger view of your Harness Trigger. API_KeyIn Harness CD, under Security -> Access Management -> API KeysApplication_IDIn Harness CD, in the Manual Trigger view of your Harness Trigger. Artifact_VersionEither defined by what you want to deploy. Usually in your Docker Registry or Harness CI / Drone configuration. Harness_Webhook_IDIn Harness CD, in the Manual Trigger view of your Harness Trigger. Harness_Service_NameYour Service you are deploying to. In this example was “Amazing App”.Harness_Artifact_NameThe artifact name given to your Service. In this example was “rlachhman_amazingapp” representing the repository and image.

With the prudent details, you can start to build in the scaffolding on how the plugin will execute.

First Plugin Scaffolding

With the scaffoldings in place, you can now start to wire in the secrets into the repository that you wish to build and publish.

Secrets

With the secrets wired in, all that is left is to include the scaffolding into an existing or sample Harness CI / Drone Pipeline.

Executing Your First Pipeline

Coming to the conclusion of this example. All we need to do now is append the scaffolding/plugin configuration at the end of a Harness CI / Drone Pipeline. Will be leveraging the same pipeline that produced version 1.0.1 of the Amazing App. Let’s let the newly minted plugin take care of deploying version 1.0.2.

Commit Your First Plugin

Commit and you are off to the races!

Updated Drone.yaml - the Last Update Needed for Your First Plugin

Great job sticking through the example, can validate in the Harness CD Platform that your deployment was kicked off.

Your First Plugin Is Completed!

Partner With Harness

No matter where you are in your CI/CD journey, Harness can simplify and scale your capabilities. With the lightweight and convention-based Harness CI / Drone and the power of the Harness Platform, achieving CI/CD nirvana has never been easier. Get started with Harness CI / Drone and sign up for a Harness Account today. If you create a cool plugin, don’t be shy about contributing it back!

The Modern Software Delivery Platform™

Loved by Developers, Trusted by Businesses
Get Started

Need more info? Contact Sales