-
Notifications
You must be signed in to change notification settings - Fork 80
/
Jenkinsfile.demo
91 lines (76 loc) · 2.58 KB
/
Jenkinsfile.demo
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
def gitCommit() {
sh "git rev-parse HEAD > GIT_COMMIT-${env.BUILD_NUMBER}"
def gitCommit = readFile("GIT_COMMIT-${env.BUILD_NUMBER}").trim()
sh "rm -f GIT_COMMIT-${env.BUILD_NUMBER}"
return gitCommit
}
def gitEmail() {
sh "git --no-pager show -s --format='%ae' ${gitCommit()} > GIT_EMAIL-${env.BUILD_NUMBER}"
def gitEmail = readFile("GIT_EMAIL-${env.BUILD_NUMBER}").trim()
sh "rm -f GIT_EMAIL-${env.BUILD_NUMBER}"
return gitEmail
}
def ipAddress() {
sh "docker inspect test-container-${env.BUILD_NUMBER} | jq -r '.[0].NetworkSettings.IPAddress' > IP_ADDRESS-${env.BUILD_NUMBER}"
sh "docker inspect test-container-${env.BUILD_NUMBER} | jq -r '.[0].NetworkSettings'"
def ipAddress = readFile("IP_ADDRESS-${env.BUILD_NUMBER}").trim()
sh "rm -f IP_ADDRESS-${env.BUILD_NUMBER}"
return ipAddress
}
try {
node {
// Checkout source code from Git
stage 'Checkout'
checkout scm
// Build Docker image
stage 'Build'
sh "docker build -t mesosphere/cd-demo-app:${gitCommit()} ."
// Test Docker image
stage 'Test'
sh "docker run -d --name=test-container-${env.BUILD_NUMBER} mesosphere/cd-demo-app:${gitCommit()} jekyll serve"
sh "docker run mesosphere/linkchecker linkchecker --no-warnings http://${ipAddress()}:4000/"
// Log in and push image to Docker Hub
stage 'Publish'
withCredentials(
[[
$class: 'UsernamePasswordMultiBinding',
credentialsId: 'docker-hub-credentials',
passwordVariable: 'DOCKER_HUB_PASSWORD',
usernameVariable: 'DOCKER_HUB_USERNAME'
]]
) {
sh "docker login -u '${env.DOCKER_HUB_USERNAME}' -p '${env.DOCKER_HUB_PASSWORD}' -e [email protected]"
sh "docker push mesosphere/cd-demo-app:${gitCommit()}"
}
// Deploy
stage 'Deploy'
marathon(
url: 'http://marathon.mesos:8080',
forceUpdate: false,
credentialsId: 'dcos-token',
filename: 'marathon.json',
id: 'jenkins-deployed-app',
docker: "mesosphere/cd-demo-app:${gitCommit()}".toString(),
labels: [[name: 'lastChangedBy', value: "${gitEmail()}".toString()]]
)
}
} catch (Exception e) {
println 'Build Failed'
currentBuild.result = 'FAILURE'
stage 'Publish'
println 'Skipped'
stage 'Deploy'
println 'Skipped'
}
try {
node {
// Clean up
stage 'Clean'
sh "docker ps"
sh "docker kill test-container-${env.BUILD_NUMBER}"
sh "docker rm test-container-${env.BUILD_NUMBER}"
}
} catch (Exception e) {
println 'Cleanup failed'
currentBuild.result = 'FAILURE'
}