-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Jenkinsfile
70 lines (66 loc) · 2.02 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!groovy
// Jenkinsfile pipeline file
import groovy.json.JsonOutput
import jenkins.model.*
node {
def stepsForParallel = [:]
currentBuild.result = "SUCCESS"
env.BUILD_MSG = ''
try {
stage('Checkout') {
checkout scm
def names = nodeNames()
print "Name of Nodes: ${names}"
}
stage('Print Env and Branch') {
env.NODE_ENV = "test"
print "Environment will be: ${env.NODE_ENV}"
print "Job name: ${env.JOB_NAME}"
}
stage('Archive Artifacts') {
archiveArtifacts([artifacts: '**/Jenkinsfile', fingerprint: true, onlyIfSuccessful: true])
stash(allowEmpty: false, includes: 'Jenkinsfile', name: 'pipeline-sample')
}
stage('Send Notice') {
echo 'Send success email'
}
} catch (err) {
currentBuild.result = "FAILURE"
env.BUILD_MSG = err
throw err
} finally {
// perform workspace cleanup only if the build have passed
// if the build has failed, the workspace will be kept
step([$class: 'WsCleanup', cleanWhenFailure: false])
def payload = env.BUILD_MSG
sendEmail()
def options = JsonOutput.toJson([
result: currentBuild.result,
number: env.BUILD_NUMBER,
payload: [reason: env.BUILD_CAUSE, body: payload]
])
postToES(options)
}
}
// This method collects a list of Node names from the current Jenkins instance
@NonCPS
def nodeNames() {
return {
Jenkins.instance.nodes.collect { i ->
[ i.name ]
}
}
}
def postToES(options) {
print "${options}"
sh "curl -XPOST http://localhost:9200/jenkins/jobs/${env.BUILD_NUMBER} -d '${options}' -s || true"
}
def sendEmail() {
return {
mail body: "project build ${currentBuild.result} is here: ${env.BUILD_URL}" ,
from: "${env.EMAIL_REPO}",
replyTo: "${env.EMAIL_REPO}",
subject: "${currentBuild.result}: project ${env.JOB_NAME} build ${env.BUILD_NUMBER}",
to: "${env.EMAIL_PROJECT}"
}
}