-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathJenkinsfile
140 lines (134 loc) · 3.51 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
pipeline {
agent any
tools {
maven "Maven-3.3.9"
jdk "Java-8"
}
environment{
DEVELOPERS_EMAIL="musema.hassen@gmail.com"
BUILD_FROM_BRANCH="master"
REPO_URL="https://github.com/musema/jenkins-pipeline-examples.git"
}
options{
timeout(time:48,unit:'HOURS')
skipDefaultCheckout(true)
disableConcurrentBuilds()
buildDiscarder(logRotator(numToKeepStr: '5'))
timestamps()
}
stages {
stage('Prepare'){
parallel{
stage('Code-Checkout'){
steps{
checkoutCode()
}
}
stage('Prepare-Workspace'){
steps{
prepareWorkspace()
}
}
}
}
stage("Package") {
steps {
packageArtifact()
}
}
stage("Test"){
steps{
runTest()
}
}
stage("Upload to Repository"){
steps{
uploadToRepository();
}
}
stage("Decide-Deployment"){
agent none
steps{
script{
env.DEPLOY_TO_DEV= input message: 'Approval is required',
parameters: [
choice(name: 'Do you want to deploy to DEV?', choices: 'no\nyes',
description: 'Choose "yes" if you want to deploy the DEV server')
]
}
}
}
stage('Deploy To Dev'){
when{
environment name:'DEPLOY_TO_DEV', value:'yes'
}
steps{
deployToServer("DEV")
}
}
stage('Archive'){
steps{
archiveTheBuild()
}
}
}
post {
always {
finalizeWorkflow()
}
success {
successMessage()
}
failure {
failureMessage()
}
}
}
//define groovy functions here
def prepareWorkspace(){
echo 'Check here if everything is ready to make a build'
sh 'mvn --version'
sh 'java -version'
sh 'git --version'
}
def checkoutCode(){
count=1
retry(3){
echo "trying to checkout the code from SCM, Trial:${count}"
//git branch: "${BUILD_FROM_BRANCH}",credentialsId: "${GITHUB_CREDENTIALS_ID}",url: "${REPO_URL}"
git url:"${REPO_URL}"
}
}
def packageArtifact(){
sh 'mvn clean install -Dmaven.test.failure.ignore=true'
}
def runTest(){
sh 'mvn test'
}
def uploadToRepository(){
echo "We are about to publish artifacts to remote repository"
//mvn deploy
}
def archiveTheBuild(){
archive '*/target/**/*'
//junit '*/target/surefire-reports/*.xml'
}
def deployToServer(deployTo){
echo "We are deploying to : ${deployTo}"
}
def successMessage(){
echo "Sending SUCCESS email to ${env.DEVELOPERS_EMAIL}"
notifyBuild('SUCCESS')
}
def failureMessage(){
echo "Notifying build failure"
notifyBuild('FAILED')
}
def finalizeWorkflow(){
echo "Cleaning up the workspace"
}
def notifyBuild(bStatus) {
def subject = "${bStatus}: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'"
def details = """STARTED: Job ${env.JOB_NAME} [${env.BUILD_NUMBER}] \n Check console output at: ${env.BUILD_URL}"""
mail to:"${env.DEVELOPERS_EMAIL}",subject:"${subject}",body:"${details}"
}