From c43b403d1ada28cf9adbaf1f4cdb7f875b8652a7 Mon Sep 17 00:00:00 2001 From: Shashank Kaul Date: Mon, 28 Oct 2019 00:07:40 +0530 Subject: [PATCH] Working version. --- Dockerfile | 4 ++++ README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ deploy.js | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 deploy.js diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ef19ee2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM node:latest +COPY deploy.js . +RUN npm install -g caprover +CMD ["node", "deploy.js"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..a1ddea2 --- /dev/null +++ b/README.md @@ -0,0 +1,46 @@ +# drone-caprover-deploy + +Drone plugin to deploy to a caprover app using a tar of the required files. + +**Note**: You must have a captain-definition file present in the root of your repository. + +## Configuration + +A sample configuration which includes all settings will look like this + +```yaml +{ + name: "deploy", + image: "drone-caprover-deploy", + settings: { + app_name: "core", # Required: Caprover application name + password: { # Required: Caprover root password + from_secret: "cap_password" + }, + host: { # Required: Caprover url + from_secret: "cap_url" + }, + tar_include: [ # Optional: Items to include in the tar file. Will zip the entire repo if not specified. + "file1", + "file2" + ], + suffixes: { # Optional: If provided, when the current branch for the job matches some "branch_name" key, the "suffix field" will be appended to the "app_name" value. This is useful if you want to condense your repo deploys into a single CI step. + "branch_name": "suffix field", + "develop": "-staging", + ... + }, + }, + when: { + status: [ + "success" + ], + ... + } +} +``` + +What the above example will do: + +- Build a tar file which contains 'file1' and 'file2' +- Once built, will deploy to the host specified in the 'cap_url' secret using the 'cap_password' secret. +- When running on branch 'develop' it will deploy to the 'core-staging' application. diff --git a/deploy.js b/deploy.js new file mode 100644 index 0000000..5d3c9c1 --- /dev/null +++ b/deploy.js @@ -0,0 +1,35 @@ +const { execSync } = require("child_process"); + +const isNotValidString = str => !(typeof str === "string" && str.length > 1); + +let APP_NAME = process.env.PLUGIN_APP_NAME; +const CAP_PASSWORD = process.env.PLUGIN_PASSWORD; +const CAP_HOST = process.env.PLUGIN_HOST; + +const INCLUDE_FILES = process.env.PLUGIN_TAR_INCLUDE + ? process.env.PLUGIN_TAR_INCLUDE.split(",").join(" ") + : "."; + +const SUFFIXES = process.env.PLUGIN_SUFFIXES + ? JSON.parse(process.env.PLUGIN_SUFFIXES) + : ""; + +const DRONE_BRANCH = process.env.DRONE_BRANCH; + +if ( + isNotValidString(APP_NAME) || + isNotValidString(CAP_PASSWORD || isNotValidString(CAP_HOST)) +) { + console.error("Invalid usage. Please check documentation"); + process.exit(1); +} + +if (SUFFIXES !== "") { + APP_NAME = APP_NAME + SUFFIXES[DRONE_BRANCH]; +} + +execSync(`tar -cf app.tar ${INCLUDE_FILES}`); + +execSync( + `caprover deploy --caproverUrl ${CAP_HOST} --capPassword ${CAP_PASSWORD} --caproverApp ${APP_NAME} --tarFile app.tar`, +);