-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathJenkinsfile
153 lines (145 loc) · 4.77 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
140
141
142
143
144
145
146
147
148
149
150
151
152
def images = []
def image_versions = [:]
def removable_tags = []
pipeline {
agent {
label 'worker&&docker'
}
options {
buildDiscarder(logRotator(numToKeepStr: '1'))
}
triggers {
pollSCM("H */2 * * *")
cron('H H * * H')
}
environment {
SSH_KEY_FILE = "${env.HOME}/.ssh/id_worker"
IMAGE_DIR_BASE = "${env.WORKSPACE}/image"
EXPORT_DIR_BASE = "${env.WORKSPACE}/export"
}
parameters {
booleanParam(name: 'test', defaultValue: true, description: 'Test the image')
booleanParam(name: 'needAdminApproval', defaultValue: false, description: 'Wait for admin approval after testing')
booleanParam(name: 'release', defaultValue: true, description: 'Release the image')
string(name: 'logLevel', defaultValue: '3', description: 'Log level')
}
stages {
stage('Pull image source') {
steps {
dir('image') {
deleteDir()
checkout([
$class: 'GitSCM',
poll: false,
branches: [[name: env.IMAGE_GIT_BRANCH ?: 'master' ]],
userRemoteConfigs: [[url: env.IMAGE_GIT_SOURCE]]
])
}
}
}
stage('Set environment') {
steps {
script {
manifest = readFile("${env.IMAGE_DIR_BASE}/manifest.json")
manifest_data = new groovy.json.JsonSlurperClassic().parseText(manifest)
image_versions = manifest_data['images']
env.IMAGE_DIR = env.IMAGE_DIR_BASE + '/' + image_versions[env.IMAGE_VERSION]['directory']
env.MARKETPLACE_IMAGE_NAME = image_versions[env.IMAGE_VERSION]['marketplace-name']
env.BUILD_OPTS = "--pull"
env.LOG_LEVEL = params.logLevel
}
}
}
stage('Log into Scaleway') {
steps {
withCredentials([usernamePassword(credentialsId: 'scw-test-orga-token', usernameVariable: 'SCW_ORGANIZATION', passwordVariable: 'SCW_TOKEN')]) {
sh 'scw login -o "$SCW_ORGANIZATION" -t "$SCW_TOKEN" -s >/dev/null 2>&1'
}
}
}
stage("Create image on Scaleway") {
steps {
script {
for (String arch in image_versions[env.IMAGE_VERSION]['architectures']) {
echo "Creating image for $arch"
sh "make ARCH=$arch IMAGE_DIR=${env.IMAGE_DIR} EXPORT_DIR=${env.EXPORT_DIR_BASE}/$arch BUILD_OPTS='${env.BUILD_OPTS}' scaleway_image"
script {
imageId = readFile("${env.EXPORT_DIR_BASE}/$arch/image_id").trim()
docker_tags = readFile("${env.EXPORT_DIR_BASE}/$arch/docker_tags").trim().split('\n')
images.add([
arch: arch,
id: imageId,
docker_tags: docker_tags
])
}
}
}
}
}
stage('Test the images') {
when {
expression { params.test == true }
}
steps {
script {
for (Map image : images) {
sh "make tests IMAGE_DIR=${env.IMAGE_DIR} EXPORT_DIR=${env.EXPORT_DIR_BASE}/${image['arch']} ARCH=${image['arch']} IMAGE_ID=${image['id']} TESTS_DIR=${env.IMAGE_DIR}/tests NO_CLEANUP=${params.needAdminApproval}"
}
if (env.needsAdminApproval) {
input "Confirm that the images are stable ?"
}
}
}
post {
always {
script {
if (env.needsAdminApproval) {
for (Map image : images) {
sh "scripts/test_images.sh stop ${env.EXPORT_DIR_BASE}/${image['arch']}/${image['id']}.servers"
}
}
}
}
}
}
stage('Release the image') {
when {
expression { params.release == true }
}
steps {
script {
withCredentials([usernamePassword(credentialsId: 'dockerhub', usernameVariable: 'DOCKERHUB_USER', passwordVariable: 'DOCKERHUB_PASSWD')]) {
sh 'echo -n "$DOCKERHUB_PASSWD" | docker login -u "$DOCKERHUB_USER" --password-stdin'
}
for (image in images) {
for (tag in image['docker_tags']) {
sh "docker push ${tag}"
}
removable_tags.add(image['docker_tags'][-1])
image.remove('docker_tags')
}
message = groovy.json.JsonOutput.toJson([
type: "image",
data: [
marketplace_id: image_versions[env.IMAGE_VERSION]['marketplace-id'],
versions: images
]
])
versionId = input(
message: "${message}",
parameters: [string(name: 'image_id', description: 'ID of the new image version')]
)
}
echo "Created new marketplace version of image: ${versionId}"
}
}
}
post {
always {
deleteDir()
script {
sh "docker image rm ${removable_tags.join(' ')} && docker system prune -f"
}
}
}
}