-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile.sample
110 lines (103 loc) · 4.14 KB
/
Jenkinsfile.sample
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
properties([[$class: 'GitLabConnectionProperty', gitLabConnection: 'GitLab']])
pipeline {
agent { label 'Slave_CentOS7' }
stages {
stage('JIRA') {
agent none
steps {
echo "Opening JIRA Ticket..."
script {
def issue = [fields: [ project: [key: 'DEVOPS'],
assignee: [name: 'devops'],
summary: "Jenkins Build ${env.JOB_NAME} #${env.BUILD_ID} Ticket",
description: "New JIRA Ticket created on ${env.BUILD_TIMESTAMP} from Jenkins. URL: ${env.JENKINS_URL}",
priority: [name: 'Medium'],
labels: ['Jenkins'],
issuetype: [name: 'Jenkins']]]
def newIssue = jiraNewIssue issue: issue, site: 'JIRA-DEVOPS'
env.jiraTicket = newIssue.data.key
echo "JIRA Ticket #${env.jiraTicket}"
}
}
}
stage('Build') {
steps {
checkout scm
echo 'Building..'
script { jiraAddComment idOrKey: "${env.jiraTicket}", comment: 'Start Building the project ....', site: 'JIRA-DEVOPS' }
sh 'ls -lrt'
}
}
stage('Test') {
steps {
echo 'Testing..'
script { jiraAddComment idOrKey: "${env.jiraTicket}", comment: 'Start Testing the project ....', site: 'JIRA-DEVOPS' }
sh 'ls -lrt'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
script { jiraAddComment idOrKey: "${env.jiraTicket}", comment: 'Start Deploying the project ....', site: 'JIRA-DEVOPS' }
sh 'ls -lrt'
}
}
stage('CleanUp') {
steps {
deleteDir()
}
}
}
post {
always {
echo 'Always'
}
success {
script { currentBuild.result = 'SUCCESS' }
script { def changeStringReturn = getChangeString()
env.changeStringEnv = changeStringReturn }
updateGitlabCommitStatus(name: 'build', state: 'success')
sendEmail("SUCCEEDED")
script {
// id 5->Resolved
def transitionInput = [ transition: [ id: '5' ], fields: [ resolution: [ name: 'Done' ] ] ]
jiraAddComment idOrKey: "${env.jiraTicket}", comment: 'Build Successful ....', site: 'JIRA-DEVOPS'
jiraTransitionIssue idOrKey: "${env.jiraTicket}", input: transitionInput, site: 'JIRA-DEVOPS'
}
}
failure {
script { currentBuild.result = 'FAILURE' }
updateGitlabCommitStatus(name: 'build', state: 'failed')
sendEmail("FAILED")
}
}
}
@NonCPS
def getChangeString() {
def changeString =""
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
changeString += "Commit: ${entry.commitId} by <b>${entry.author}</b> on ${new Date(entry.timestamp)} Build: ${entry.msg}<br/>"
def files = new ArrayList(entry.affectedFiles)
for (int k = 0; k < files.size(); k++) {
def file = files[k]
changeString += " ${file.editType.name} ${file.path}"
//echo "File(s) Modified: <b>${file.editType.name}</b> ${file.path}<br/>"
}
}
}
if (!changeString) {
changeString = "<td>No Changes<td>"
}
return changeString
}
def sendEmail(status) {
emailext(body: '${JELLY_SCRIPT,template="emailHtml"}',
replyTo: '[email protected]', subject: '[Jenkins] ${DEFAULT_SUBJECT}' + status + '!',
to: 'cc:[email protected]', mimeType: 'text/html',
recipientProviders: [[$class: 'CulpritsRecipientProvider'],
[$class: 'RequesterRecipientProvider']])
}