Write custom plugins
Plugins perform predefined tasks. They are essentially templated scripts that can be written in any programming language. While you can run scripts in CI pipelines, if you reuse a script, it is easier to maintain a single plugin rather than modify multiple instances of a script.
You can write your own plugins and run them in a Plugin step in your Harness CI pipelines. There are also many preexisting plugins you can use.
You can write plugins for anything. If it can be scripted, it can be a plugin. For example, you could write plugins that:
- Scrape commit information from a Git repo.
- Extract Jira issue numbers from Git commit messages.
- Print an extended build history.
- Create a file, write to it, and store it remotely.
Create your plugin
To create a plugin, you need to prepare a script, create a Docker image to run the script, and then build and publish the plugin image to a Docker registry.
-
Write the script that you want the plugin to run. You can write plugins in any programming language. For example, the following Bash script, called
clone.sh
, clones a Git repo and prints the latest commit information.#!/bin/sh
set -xe
# If path setting is not set, then use current directory
path=${PLUGIN_PATH:-.}
mkdir -p ${path}
cd ${path}
# Clones the public git repo and checkout to a branch
git clone ${PLUGIN_REPO_URL} .
git checkout ${PLUGIN_BRANCH}
# Prints the last commit
git log -1 --statVariablesThe above example script includes variable inputs:
PLUGIN_PATH
,PLUGIN_REPO_URL
, andPLUGIN_BRANCH
. Variables in plugin scripts become the plugin's Settings when you add the Plugin step to your CI pipeline. -
Create a Docker image to run your script by specifying the script as the ENTRYPOINT in a Dockerfile. For example, the following Dockerfile runs the
clone.sh
script:FROM alpine/git
# Copies the clone script to the Docker image
COPY clone.sh /usr/local/bin/
# Makes the clone script executable
RUN chmod +x /usr/local/bin/clone.sh
ENTRYPOINT [ "/usr/local/bin/clone.sh" ] -
Using the method of your choice, build and publish the plugin image to a Docker registry. Take note of the Docker repo and image name, such as
my-docker-repo/git-clone-plugin
. You need it when you add the Plugin step to your CI pipeline.docker build -t my-docker-repo/git-clone-plugin .
docker push my-docker-repo/git-clone-plugin
Test plugins locally
You can test your plugin in a local environment by running it as a Docker container. For example, the following Docker command runs the clone.sh
plugin locally by supplying the required inputs (PLUGIN_PATH
, PLUGIN_REPO_URL
, and PLUGIN_BRANCH
) and specifying the plugin's Docker repo and image (my-docker-repo/git-clone-plugin
).
docker run --rm \
-e PLUGIN_PATH=codebase \
-e PLUGIN_REPO_URL=https://github.com/<some-account>/<some-repo>.git \
-e PLUGIN_BRANCH=main \
my-docker-repo/git-clone-plugin