-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
88 lines (84 loc) · 2.82 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
podTemplate(label: 'user-service-pod-jenkins', containers: [
containerTemplate(name: 'docker', image: 'docker', command: 'cat', ttyEnabled: true),
containerTemplate(name: 'mongo', image: 'mongo',
ports: [portMapping(name: 'mongo', containerPort: 27017, hostPort: 27017)],
command: 'mongod'
),
containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat')
],
envVars: [
envVar(key: 'BRANCH_NAME', value: env.BRANCH_NAME),
envVar(key: 'BUILD_NUMBER', value: env.BUILD_NUMBER),
envVar(key: 'MONGO_HOST', value: 'localhost'),
envVar(key: 'MONGO_PORT', value: '27017')
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
]) {
node('user-service-pod-jenkins') {
def app
def userInput = true
def didTimeout = false
stage('checkout') {
checkout scm
}
stage('Check Mongo container') {
container('mongo') {
sh 'ps -ax | grep mongo'
}
container('docker') {
sh 'docker ps'
}
}
stage('Maven Build') {
container('maven') {
sh 'echo $BRANCH_NAME'
sh 'mvn clean install -X -Dspring.data.mongodb.host=$MONGO_HOST -Dspring.data.mongodb.port=$MONGO_PORT -Dspring.data.mongodb.username=root -Dspring.data.mongodb.password=pass'
}
}
stage('Build Docker Image') {
container('docker') {
sh 'docker build . -t debapriyalaha/user-service:$BRANCH_NAME'
}
}
stage('Publish Docker Image'){
container('docker') {
withCredentials([usernamePassword( credentialsId: 'docker-hub-credentials', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
sh 'docker login -u ${USERNAME} -p ${PASSWORD}'
sh 'docker push debapriyalaha/user-service:$BRANCH_NAME'
}
}
}
stage('Proceed to DEV?') {
try {
timeout(time: 60, unit: 'SECONDS') { // change to a convenient timeout for you
userInput = input(
id: 'Proceed1', message: 'Should proceed to DEV?', parameters: [
[$class: 'BooleanParameterDefinition', defaultValue: true, description: '', name: 'Please confirm you agree with this']
])
}
} catch(err) { // timeout reached or input false
def user = err.getCauses()[0].getUser()
if('SYSTEM' == user.toString()) { // SYSTEM means timeout.
didTimeout = true
} else {
userInput = false
echo "Aborted by: [${user}]"
}
}
}
stage('Deploy to DEV'){
if (didTimeout) {
// do something on timeout
echo "no input was received before timeout"
} else if (userInput == true) {
// do something
echo "this was successful"
} else {
// do something else
echo "this was not successful"
currentBuild.result = 'FAILURE'
}
}
}
}