forked from d2iq-archive/cd-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
73 lines (58 loc) · 2.17 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
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}"
def ipAddress = readFile("IP_ADDRESS-${env.BUILD_NUMBER}").trim()
sh "rm -f IP_ADDRESS-${env.BUILD_NUMBER}"
return ipAddress
}
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()}"
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',
appid: 'jenkins-deployed-app',
docker: "mesosphere/cd-demo-app:${gitCommit()}".toString(),
labels: ['lastChangedBy': "${gitEmail()}".toString()]
)
// Clean up
stage 'Clean'
sh "docker kill test-container-${env.BUILD_NUMBER}"
sh "docker rm test-container-${env.BUILD_NUMBER}"
}