Build a Custom Drone.io Telegram Notification Plugin with Node.js and Docker
This tutorial walks you through creating a Drone CI plugin in Node.js that sends Telegram notifications, covering environment variable handling, plugin code, Dockerfile optimization, image building, testing, secret management, and common pitfalls for seamless CI integration.
Introduction
The author, a former front‑end engineer at Qiwuchuan, shares how to write a Drone.io plugin in Node.js that sends Telegram messages, a solution especially useful for users behind Chinese firewalls who need to proxy Telegram API requests.
Principle
Drone runs each plugin inside a Docker container and injects build information and plugin configuration as environment variables (e.g., PLUGIN_TOKEN , DRONE_COMMIT ). Any language can read these variables, enabling language‑agnostic plugin development.
Plugin configuration variables are prefixed with PLUGIN_ (e.g., PLUGIN_TOKEN ).
Build‑related information is prefixed with CI_ or DRONE_ (e.g., DRONE_COMMIT ).
Write the Plugin
The plugin reads required configuration from environment variables and uses node-telegram-bot-api to send a message. The core code is straightforward:
const render = require('drone-render');
const TelegramBot = require('node-telegram-bot-api');
const {
PLUGIN_TOKEN,
PLUGIN_TO,
TELEGRAM_TOKEN,
TELEGRAM_TO,
PLUGIN_LANG,
PLUGIN_MESSAGE,
PLUGIN_BASE_API_URL
} = process.env;
const TOKEN = PLUGIN_TOKEN || TELEGRAM_TOKEN;
const TO = PLUGIN_TO || TELEGRAM_TO;
if (PLUGIN_LANG) {
render.locale(PLUGIN_LANG);
}
const bot = new TelegramBot(TOKEN, {
baseApiUrl: PLUGIN_BASE_API_URL
});
bot.sendMessage(TO, render(PLUGIN_MESSAGE));The code is intentionally minimal.
Dockerfile
After writing the plugin, create a Dockerfile. To keep the image size small, install dependencies in an early layer and copy the script later:
FROM mhart/alpine-node:8.9.3
WORKDIR /telegram-node
COPY package.json /telegram-node/package.json
RUN npm install
COPY index.js /telegram-node/index.js
ENTRYPOINT [ "node", "/telegram-node/index.js" ]Build and Publish
Build the image and push it to Docker Hub so others can use the plugin:
docker build lizheming/drone-telegram-node .
docker push lizheming/drone-telegram-nodeTesting
Run the image locally with the required environment variables to verify it works:
docker run --rm \
-e PLUGIN_TOKEN=xxxxxxx \
-e PLUGIN_TO=xxxxxxx \
-e PLUGIN_MESSAGE=test \
-e PLUGIN_BASE_API_URL=xxxx \
lizheming/drone-telegram-nodeSecrets Management
To avoid exposing sensitive data in .drone.yml , store them as Drone secrets. The secret name becomes the environment variable name inside the plugin (e.g., TELEGRAM_TOKEN ).
pipeline:
telegram:
image: lizheming/drone-telegram-node
secrets: [ telegram_token, telegram_to ]
message: helloCommon Issue: Plugin Not Running
If the Dockerfile sets WORKDIR , Drone may override it during execution, causing the entrypoint script to be missing. Adjust the ENTRYPOINT to use an absolute path (as shown above) to resolve this.
Conclusion
The complete source code is available on GitHub ( https://github.com/lizheming/drone-telegram-node ) and the Docker image is published on Docker Hub. This plugin improves on the official one by allowing a custom Telegram API base URL, making it suitable for deployments behind proxies.
360 Zhihui Cloud Developer
360 Zhihui Cloud is an enterprise open service platform that aims to "aggregate data value and empower an intelligent future," leveraging 360's extensive product and technology resources to deliver platform services to customers.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.