Plugin Development
About 418 wordsAbout 1 min
In Cloud Native Build, a plugin is essentially a Docker image. Below is how to develop an image plugin from scratch using Bash.
This example plugin prints hello world. Basic Docker knowledge is assumed.
Designing the Plugin
Parameter Design
Design the plugin parameters:
text: The text content to be printed to the console
These parameters are passed to the container as environment variables, converted to uppercase with a PLUGIN_ prefix.
Supported Parameter Types
Parameter types support string, number, boolean, one-dimensional array, and plain object.
Configuration example:
main:
push:
- stages:
- name: hello world
image: cnbcool/hello-world
settings:
text: hello world
boolean: true
number: 123
array: [hello, world]
map:
key: valueThese parameters are converted to the following environment variables inside the container:
PLUGIN_TEXT='hello world'
PLUGIN_BOOLEAN='true'
PLUGIN_NUMBER='123'
PLUGIN_ARRAY='hello,world'
PLUGIN_MAP='{"key":"value"}'- Arrays are comma-separated when passed to the container
- Plain objects are converted to JSON strings
If the parameter structure is overly complex, consider storing them in a file and loading at runtime, simplifying the design, or splitting into multiple plugins.
Writing the Script
Write a Bash script to print the parameters:
#!/bin/sh
echo "$PLUGIN_TEXT"Building the Plugin Image
The plugin is packaged as a Docker image for distribution. Create a Dockerfile to package the script and set it as the Entrypoint.
FROM alpine
ADD entrypoint.sh /bin/
RUN chmod +x /bin/entrypoint.sh
ENTRYPOINT /bin/entrypoint.shBuild the image:
docker build -t cnbcool/hello-world .Testing the Plugin
Test the plugin locally using docker run, passing parameters via environment variables:
docker run --rm \
-e PLUGIN_TEXT="hello world" \
cnbcool/hello-worldTesting Filesystem Access
The plugin can read the build process workspace directory. By default, the build directory is mapped to a plugin directory and set as the workspace:
docker run --rm \
-e PLUGIN_TEXT="hello world" \
-v $(pwd):$(pwd) \
-w $(pwd) \
cnbcool/hello-worldExporting Variables
If the plugin needs to return results and export them as variables for subsequent tasks, refer to exports
Publishing the Plugin
A plugin is a Docker image — publishing a plugin means publishing the image.
You can publish to the built-in Artifact Registry of Cloud Native Build, or to Docker Hub for global availability.
Pushing the Image
docker push cnbcool/hello-world