add jenkins snippet
This commit is contained in:
parent
c59432b53d
commit
3bd332f52a
10
README.md
10
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]
|
||||
```
|
||||
|
56
snippets/jenkins/index.js
Normal file
56
snippets/jenkins/index.js
Normal 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 };
|
39
snippets/jenkins/template/general/Jenkinsfile
vendored
Normal file
39
snippets/jenkins/template/general/Jenkinsfile
vendored
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
40
snippets/jenkins/template/node/Jenkinsfile
vendored
Normal file
40
snippets/jenkins/template/node/Jenkinsfile
vendored
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
7
snippets/jenkins/template/node/jenkins.js
Normal file
7
snippets/jenkins/template/node/jenkins.js
Normal 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));
|
Loading…
x
Reference in New Issue
Block a user