From 3bd332f52ae3070809427d9ccee2702ac2a32363 Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Thu, 16 Jan 2020 20:35:24 +0100 Subject: [PATCH] add jenkins snippet --- README.md | 10 ++++ snippets/jenkins/index.js | 56 +++++++++++++++++++ snippets/jenkins/template/general/Jenkinsfile | 39 +++++++++++++ snippets/jenkins/template/node/Jenkinsfile | 40 +++++++++++++ snippets/jenkins/template/node/jenkins.js | 7 +++ 5 files changed, 152 insertions(+) create mode 100644 snippets/jenkins/index.js create mode 100644 snippets/jenkins/template/general/Jenkinsfile create mode 100644 snippets/jenkins/template/node/Jenkinsfile create mode 100644 snippets/jenkins/template/node/jenkins.js diff --git a/README.md b/README.md index 867a19f..c47d01a 100644 --- a/README.md +++ b/README.md @@ -40,3 +40,13 @@ Creates a new nodejs project ```sh snippeteer node [name] ``` + +### Jenkins + +Adds files necessary for jenkins + +optionally with a js script for npm modules by adding 'node' as argument + +```sh +snippeteer jenkins [node] +``` diff --git a/snippets/jenkins/index.js b/snippets/jenkins/index.js new file mode 100644 index 0000000..5c85860 --- /dev/null +++ b/snippets/jenkins/index.js @@ -0,0 +1,56 @@ +/* eslint-disable no-sync */ +/* eslint-disable no-console */ + +const fs = require ('fs-extra'); +const path = require ('path'); + +/** + * copies the full template to a new folder named after arg[0] + * + * @param {string} folder folder to run in + * @param {Array} args function arguments + */ +function run (folder, args) { + const template = path.join ( + __dirname, + 'template', + args.length === 1 && (/^node$/ui).test + ? 'node' + : 'general' + ); + + for (const f of fs.readdirSync (template)) + fs.copy ( + path.join (template, f), + path.join (folder, f), + { filter: (src, dest) => !fs.existsSync (dest) } + ); +} + +/** + * checks if the arguments meet the requirements + * + * @param {string} folder folder to run in + * @returns {boolean} true if arguments match requirements + */ +function assert (folder) { + const tests = [ + { + f: () => (typeof folder === 'string'), + reason: 'cwd is not a folder (internal error)' + }, + { + f: () => (fs.existsSync (folder)), + reason: 'cwd does not exist (internal error)' + } + ]; + for (const test of tests) + if (!test.f ()) { + console.log (test.reason); + return false; + } + + return true; +} + +module.exports = { run, assert }; diff --git a/snippets/jenkins/template/general/Jenkinsfile b/snippets/jenkins/template/general/Jenkinsfile new file mode 100644 index 0000000..b6628f1 --- /dev/null +++ b/snippets/jenkins/template/general/Jenkinsfile @@ -0,0 +1,39 @@ +pipeline { + agent any + + environment { + VERSION = VersionNumber([ + versionNumberString: + '${BUILDS_ALL_TIME}', + versionPrefix: '1.0.', + worstResultForIncrement: 'SUCCESS' + ]) + publish = 0 + } + + stages { + stage('Setup') { + steps { + echo 'Setting up test environment' + sh 'echo setup' + script { + currentBuild.displayName = env.VERSION + } + } + } + } + post { + success { + script { + publish = sh script: "git log -1 | grep '\\[no publish\\]'", returnStatus: true + if (publish != 0) { + echo 'Deploying' + sh 'echo deploy' + } else { + echo 'Build successful, Commit not marked for deploying' + currentBuild.result = "UNSTABLE" + } + } + } + } +} diff --git a/snippets/jenkins/template/node/Jenkinsfile b/snippets/jenkins/template/node/Jenkinsfile new file mode 100644 index 0000000..bbc6e92 --- /dev/null +++ b/snippets/jenkins/template/node/Jenkinsfile @@ -0,0 +1,40 @@ +pipeline { + agent any + + environment { + VERSION = VersionNumber([ + versionNumberString: + '${BUILDS_ALL_TIME}', + versionPrefix: '1.0.', + worstResultForIncrement: 'SUCCESS' + ]) + publish = 0 + } + + stages { + stage('Setup') { + steps { + echo 'Setting up test environment' + sh 'npm ci' + sh 'nodejs jenkins.js ${VERSION}' + script { + currentBuild.displayName = env.VERSION + } + } + } + } + post { + success { + script { + publish = sh script: "git log -1 | grep '\\[no publish\\]'", returnStatus: true + if (publish != 0) { + echo 'Deploying' + sh 'npm publish' + } else { + echo 'Build successful, Commit not marked for deploying' + currentBuild.result = "UNSTABLE" + } + } + } + } +} diff --git a/snippets/jenkins/template/node/jenkins.js b/snippets/jenkins/template/node/jenkins.js new file mode 100644 index 0000000..c5e4547 --- /dev/null +++ b/snippets/jenkins/template/node/jenkins.js @@ -0,0 +1,7 @@ +const fs = require ('fs'); + +const pkg = JSON.parse (fs.readFileSync ('package.json', 'utf-8')); +[ + ,, pkg.version +] = process.argv; +fs.writeFileSync ('package.json', JSON.stringify (pkg, null, 2));