-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
79 lines (66 loc) · 2.37 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
import groovy.json.JsonSlurperClassic
node {
withEnv([
"REGISTRY=ricardodlc/minimal-nodejs-image"
]) {
def app
def scmVars
def MAJOR_VERSION
def MINOR_VERSION
def PATCH_VERSION
stage('Clone Repository') {
// Let's make sure we have the repository cloned to our workspace
scmVars = checkout scm
}
stage('Generate Version Tags') {
MAJOR_VERSION = '12'
MINOR_VERSION = '12.20'
PATCH_VERSION = '12.20.1'
}
stage('Build Image From Dockerfile') {
timeout(time: 6, unit: 'HOURS') {
sh "docker build -t ${REGISTRY} . --squash"
}
}
stage('List Docker Images After Build') {
sh 'docker images'
}
stage('Point To Recent Created Image') {
// This points the builded image to app
app = docker.image("${REGISTRY}")
}
stage('Test Image') {
/* Ideally, we would run a test framework against our image.
* For this example, we're using a Volkswagen-type approach ;-) */
app.inside {
sh 'node -v'
}
}
stage('Push Image to Docker Registry') {
/* Finally, we'll push the image with 3 or 4 tags:
* First, the 'latest' tag.
* Second, but not always, the incremental major version.
* Third, the incremental minor version.
* Finally, the incremental patch version.
* Pushing multiple tags is cheap, as all the layers are reused. */
docker.withRegistry('', 'docker-hub-credentials') {
app.push("latest")
// Not push the 0 major version
if (Integer.parseInt(MAJOR_VERSION) > 0) {
app.push(MAJOR_VERSION)
}
app.push(MINOR_VERSION)
app.push(PATCH_VERSION)
}
}
stage('Clean Up') {
/* Delete all tagged images:
* docker images | grep ${REGISTRY} | tr -s ' ' | cut -d ' ' -f 2 | xargs -I {} docker rmi ${REGISTRY}:{}
* Note: Using docker image prune -f -a instead in order to fully clean up (remove FROM images i.e.)
* */
sh '''
docker image prune -f -a
'''
}
}
}