From 8e58f24594d00e47b8ba0ed3f5abd4dbd86b9abf Mon Sep 17 00:00:00 2001 From: bohemian-coder Date: Mon, 5 Jun 2023 15:19:31 -0500 Subject: [PATCH 1/9] add dsl job --- job-dsl/nodejs.groovy | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 job-dsl/nodejs.groovy diff --git a/job-dsl/nodejs.groovy b/job-dsl/nodejs.groovy new file mode 100644 index 0000000..9c6d49e --- /dev/null +++ b/job-dsl/nodejs.groovy @@ -0,0 +1,17 @@ +job('NodeJS app') { + scm { + git('https://github.com/bohemian-coder/nodeJS-jenkins-docker.git') { node -> // is hudson.plugins.git.GitSCM + node / gitConfigName('bohemian-coder') + node / gitConfigEmail('jenniferejeme@gmail.com') + } + } + triggers { + scm('H/5 * * * *') + } + wrappers { + nodejs('nodejs') + } + steps { + shell("npm install") + } +} \ No newline at end of file From 29d806cd4148ad878f5173337940cda0909333e1 Mon Sep 17 00:00:00 2001 From: bohemian-coder Date: Mon, 5 Jun 2023 17:43:10 -0500 Subject: [PATCH 2/9] added node to docker groovy file --- job-dsl/nodedocker.groovy | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 job-dsl/nodedocker.groovy diff --git a/job-dsl/nodedocker.groovy b/job-dsl/nodedocker.groovy new file mode 100644 index 0000000..0d8c857 --- /dev/null +++ b/job-dsl/nodedocker.groovy @@ -0,0 +1,25 @@ +job('NodeJS Docker example') { + scm { + git('https://github.com/bohemian-coder/nodeJS-jenkins-docker.git') { node -> // is hudson.plugins.git.GitSCM + node / gitConfigName('bohemian-coder') + node / gitConfigEmail('jenniferejeme@gmail.com') + } + } + triggers { + scm('H/5 * * * *') + } + wrappers { + nodejs('nodejs') + } + steps { + dockerBuildAndPublish { + repositoryName('maybellemo/node-jenkins-docker') + tag('${GIT_REVISION,length=9}') + registryCredentials('dockercredentials') + forcePull(false) + forceTag(false) + createFingerprints(false) + skipDecorate() + } + } +} \ No newline at end of file From 625bbc7be96f41e5f24dd756d00e491a4a6d2c6e Mon Sep 17 00:00:00 2001 From: bohemian-coder Date: Mon, 5 Jun 2023 17:46:03 -0500 Subject: [PATCH 3/9] added node to docker groovy file --- job-dsl/nodedocker.groovy | 1 + 1 file changed, 1 insertion(+) diff --git a/job-dsl/nodedocker.groovy b/job-dsl/nodedocker.groovy index 0d8c857..8cec379 100644 --- a/job-dsl/nodedocker.groovy +++ b/job-dsl/nodedocker.groovy @@ -3,6 +3,7 @@ job('NodeJS Docker example') { git('https://github.com/bohemian-coder/nodeJS-jenkins-docker.git') { node -> // is hudson.plugins.git.GitSCM node / gitConfigName('bohemian-coder') node / gitConfigEmail('jenniferejeme@gmail.com') + branches('jenkins-dsl') } } triggers { From efa2d418fdda93c4b4d437f8978d00403c205a51 Mon Sep 17 00:00:00 2001 From: bohemian-coder Date: Mon, 5 Jun 2023 17:50:38 -0500 Subject: [PATCH 4/9] update dsl job name --- job-dsl/nodedocker.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/job-dsl/nodedocker.groovy b/job-dsl/nodedocker.groovy index 8cec379..73b3837 100644 --- a/job-dsl/nodedocker.groovy +++ b/job-dsl/nodedocker.groovy @@ -1,4 +1,4 @@ -job('NodeJS Docker example') { +job('NodeJS Docker Jenkins - DSL') { scm { git('https://github.com/bohemian-coder/nodeJS-jenkins-docker.git') { node -> // is hudson.plugins.git.GitSCM node / gitConfigName('bohemian-coder') From ad338a36a68782a78287c7b4a204c51cfaf965c7 Mon Sep 17 00:00:00 2001 From: bohemian-coder Date: Tue, 6 Jun 2023 11:58:16 -0500 Subject: [PATCH 5/9] initialize pipeline branch --- job-pipeline/Jenkinsfile | 21 +++++++++++++++++++++ package.json | 4 +++- test/test.js | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 job-pipeline/Jenkinsfile diff --git a/job-pipeline/Jenkinsfile b/job-pipeline/Jenkinsfile new file mode 100644 index 0000000..2d0e2fd --- /dev/null +++ b/job-pipeline/Jenkinsfile @@ -0,0 +1,21 @@ +node +{ + def git_commit_id + + stage('Preparation') { + checkout scm + sh "git rev-parse --short HEAD > .git/commit-id" + git_commit_id = readFile('.git/commit-id').trim() + } + stage('Testing') { + nodejs(nodeJSInstallationName: 'nodejs') { + sh 'npm install --only=dev' + sh 'npm test' + } + } + stage('Docker build/push') { + docker.withRegistry('https://index.docker.io/v2/', 'dockercredentials') { + def app = docker.build("maybellemo/node-jenkins-docker:${git_commit_id}", '.').push() + } + } +} \ No newline at end of file diff --git a/package.json b/package.json index 6021c89..0f648a1 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,9 @@ "express": "^4.18.2", "http-errors": "^2.0.0", "inherits": "^2.0.4", - "mocha": "^10.2.0", "mysql2": "^3.3.3" + }, + "devDependencies": { + "mocha": "^10.2.0" } } diff --git a/test/test.js b/test/test.js index 9592531..4ffbb66 100644 --- a/test/test.js +++ b/test/test.js @@ -6,3 +6,4 @@ describe('Array', function() { }); }); }); +/* For demo purposes: This test will always succeed*/ \ No newline at end of file From 217f492b2c1117a6d45304b45763de0f2ed7c5ef Mon Sep 17 00:00:00 2001 From: bohemian-coder Date: Tue, 6 Jun 2023 12:01:23 -0500 Subject: [PATCH 6/9] initialize pipeline branch --- job-dsl/createpipeline.groovy | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 job-dsl/createpipeline.groovy diff --git a/job-dsl/createpipeline.groovy b/job-dsl/createpipeline.groovy new file mode 100644 index 0000000..4a3e28e --- /dev/null +++ b/job-dsl/createpipeline.groovy @@ -0,0 +1,17 @@ +// job('DSL - Create Pipeline Job') { +// scm { +// git('https://github.com/bohemian-coder/nodeJS-jenkins-docker.git') { node -> // is hudson.plugins.git.GitSCM +// node / gitConfigName('bohemian-coder') +// node / gitConfigEmail('jenniferejeme@gmail.com') +// branches('jenkins-pipeline') +// } +// } +// triggers { +// scm('H/5 * * * *') +// } +// wrappers { +// nodejs('nodejs') +// } +// steps { +// } +// } \ No newline at end of file From 648ae2c09a29f2fb15e2aa41d6437fea01497be2 Mon Sep 17 00:00:00 2001 From: bohemian-coder Date: Tue, 6 Jun 2023 12:04:06 -0500 Subject: [PATCH 7/9] npm install dep --- job-pipeline/Jenkinsfile | 4 ++-- package.json | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/job-pipeline/Jenkinsfile b/job-pipeline/Jenkinsfile index 2d0e2fd..019da13 100644 --- a/job-pipeline/Jenkinsfile +++ b/job-pipeline/Jenkinsfile @@ -9,11 +9,11 @@ node } stage('Testing') { nodejs(nodeJSInstallationName: 'nodejs') { - sh 'npm install --only=dev' + sh 'npm install' sh 'npm test' } } - stage('Docker build/push') { + stage('Docker build and push') { docker.withRegistry('https://index.docker.io/v2/', 'dockercredentials') { def app = docker.build("maybellemo/node-jenkins-docker:${git_commit_id}", '.').push() } diff --git a/package.json b/package.json index 0f648a1..3e9fc49 100644 --- a/package.json +++ b/package.json @@ -21,9 +21,10 @@ "express": "^4.18.2", "http-errors": "^2.0.0", "inherits": "^2.0.4", - "mysql2": "^3.3.3" + "mysql2": "^3.3.3", + "mocha": "^10.2.0" }, "devDependencies": { - "mocha": "^10.2.0" + } } From a7efbbadbc4201bf87da342afd37e682875ee3b5 Mon Sep 17 00:00:00 2001 From: bohemian-coder Date: Tue, 6 Jun 2023 13:01:34 -0500 Subject: [PATCH 8/9] create jenkinsfile to run tests and connect with mysql db --- job-dsl/createpipeline.groovy | 3 +- job-pipeline/{Jenkinsfile => Jenkinsfile.v1} | 4 +-- job-pipeline/Jenkinsfile.v2 | 31 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) rename job-pipeline/{Jenkinsfile => Jenkinsfile.v1} (86%) create mode 100644 job-pipeline/Jenkinsfile.v2 diff --git a/job-dsl/createpipeline.groovy b/job-dsl/createpipeline.groovy index 4a3e28e..bb95e05 100644 --- a/job-dsl/createpipeline.groovy +++ b/job-dsl/createpipeline.groovy @@ -3,9 +3,10 @@ // git('https://github.com/bohemian-coder/nodeJS-jenkins-docker.git') { node -> // is hudson.plugins.git.GitSCM // node / gitConfigName('bohemian-coder') // node / gitConfigEmail('jenniferejeme@gmail.com') -// branches('jenkins-pipeline') +// branches('jenkins-pipeline') // } // } +// scriptPath('../Jenkinsfile') // triggers { // scm('H/5 * * * *') // } diff --git a/job-pipeline/Jenkinsfile b/job-pipeline/Jenkinsfile.v1 similarity index 86% rename from job-pipeline/Jenkinsfile rename to job-pipeline/Jenkinsfile.v1 index 019da13..ff1ff4a 100644 --- a/job-pipeline/Jenkinsfile +++ b/job-pipeline/Jenkinsfile.v1 @@ -5,9 +5,9 @@ node stage('Preparation') { checkout scm sh "git rev-parse --short HEAD > .git/commit-id" - git_commit_id = readFile('.git/commit-id').trim() + git_comit_id = readFile('.git/commit-id').trim() } - stage('Testing') { + stage('Tesmting') { nodejs(nodeJSInstallationName: 'nodejs') { sh 'npm install' sh 'npm test' diff --git a/job-pipeline/Jenkinsfile.v2 b/job-pipeline/Jenkinsfile.v2 new file mode 100644 index 0000000..ef4733b --- /dev/null +++ b/job-pipeline/Jenkinsfile.v2 @@ -0,0 +1,31 @@ +node { + def git_comit_id + stage('Preparation') { + checkout scm + sh "git rev-parse --short HEAD > .git/commit-id" + git_comit_id = readFile('.git/commit-id').trim() + } + stage('test') { + def dbTestContainer = docker.image('node:16') + dbTestContainer.pull() + dbTestContainer.inside { + sh 'npm install' + sh 'npm test' + } + } + stage('test with database') { + def mysql = docker.image('mysql').run("-e MYSQL_ALLOW_EMPTY_PASSWORD=yes") + def dbTestContainer = docker.image('node:16') + dbTestContainer.pull() + dbTestContainer.inside("--link ${mysql.id}:mysql") { //host: mysql, port: 3306 + sh 'npm install' + sh 'npm test' + } + mysql.stop() + } + stage('docker build/push') { + docker.withRegistry('https://index.docker.io/v2/', 'dockercredentials') { + def app = docker.build("maybellemo/node-jenkins-docker:${git_comit_id }", '.').push() + } + } +} \ No newline at end of file From d8771e775b6884d1878f8544ceebd1409b777fbc Mon Sep 17 00:00:00 2001 From: bohemian-coder Date: Tue, 6 Jun 2023 17:47:25 -0500 Subject: [PATCH 9/9] Added email notification update --- email-notif/Jenkinsfile | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 email-notif/Jenkinsfile diff --git a/email-notif/Jenkinsfile b/email-notif/Jenkinsfile new file mode 100644 index 0000000..445d916 --- /dev/null +++ b/email-notif/Jenkinsfile @@ -0,0 +1,36 @@ +node { + + // config + def to = emailextrecipients([ + [$class: 'CulpritsRecipientProvider'], + [$class: 'DevelopersRecipientProvider'], + [$class: 'RequesterRecipientProvider'] + ]) + + try { + stage('Build') { + println('Build stage passed...') + } + stage('Test') { + println('Test stage failed') + sh 'exit 1' + } + } catch(e) { + // Label build + currentBuild.result = "FAILURE"; + + // Email variables + def subject = "${env.JOB_NAME} - Build #${env.BUILD_NUMBER} ${currentBuild.result}" + def content = '${JELLY_SCRIPT,template="html"}' + + // Send email notification + if(to != null && !to.isEmpty()) { + emailext(body: content, mimeType: 'text/html', + replyTo: '$DEFAULT_REPLYTO', subject: subject, + to: to, attachLog: true ) + } + + // mark current build as a failure and throw the error + throw e; + } +} \ No newline at end of file