add jenkins snippet

This commit is contained in:
Timo Hocker 2020-01-16 20:35:24 +01:00
parent c59432b53d
commit 3bd332f52a
5 changed files with 152 additions and 0 deletions

View File

@ -40,3 +40,13 @@ Creates a new nodejs project
```sh ```sh
snippeteer node [name] 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]
```

56
snippets/jenkins/index.js Normal file
View File

@ -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 };

View File

@ -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"
}
}
}
}
}

View File

@ -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"
}
}
}
}
}

View File

@ -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));