This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Jenkinsfile.daily
221 lines (196 loc) · 6.11 KB
/
Jenkinsfile.daily
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// vim: ts=2:sw=2:expandtab:syntax=groovy
/* https://build.libelektra.org/job/elektra-jenkinsfile-daily/ */
// Set properties for buildjob
properties([
// Discard all but the latest 30 logs
buildDiscarder(logRotator(numToKeepStr: '30', artifactNumToKeepStr: '30')),
// Don't run concurrently
disableConcurrentBuilds()
])
// Stages
stage("Maintain Reference Repositories") {
parallel generateRepositoryMaintanenceStages()
}
stage("Maintain docker nodes") {
parallel generateDockerMaintanenceStages()
}
stage("Maintain docker repositories") {
parallel generateDockerRepoMaintanenceStages()
}
stage("Cleanup Files") {
parallel generateFileCleanupStages()
}
/**************/
/* Generators */
/**************/
/* Makes sure all 'gitmirror' nodes have up2date git repositories */
def generateRepositoryMaintanenceStages() {
// Get all nodes with label 'gitmirror' and the master
// DO NOT TAG master WITH 'gitmirror' as it will only show up as (null)
def nodes = nodesByLabel label:"gitmirror"
nodes.add("master")
def repos = [
[
url: "https://github.com/ElektraInitiative/libelektra.git",
dest: "libelektra"
]
]
def tasks = [:]
nodes.each { targetNode ->
repos.each { targetRepo ->
tasks << mirrorRepoOnNode(targetNode, targetRepo)
}
}
return tasks
}
def mirrorRepoOnNode(targetNode, targetRepo) {
def taskname = "git/${targetNode}/${targetRepo.dest}"
return [(taskname): {
stage(taskname) {
node(targetNode) {
syncRepository targetRepo
}
}
}]
}
/* cleanup temp docker files on all docker nodes */
def generateDockerMaintanenceStages() {
def nodes = nodesByLabel label:"docker"
def tasks = [:]
nodes.each { targetNode ->
tasks << cleanupDocker(targetNode)
}
return tasks
}
def cleanupDocker(targetNode) {
def taskname = "cleanupDocker/${targetNode}"
return [(taskname): {
stage(taskname) {
node(targetNode) {
dockerImages().each {
if (it ==~ /^hub.libelektra.org.*-release$/) {
// keep latest release and master image
retainNewestImages(it, 5, '', 'master\\|release')
retainNewestImages(it, 1, 'release', 'none')
retainNewestImages(it, 1, 'master', 'none')
} else {
retainNewestImages(it, 5)
}
}
sh "docker system prune -f"
}
}
}]
}
/* Cleanup Docker registries */
def generateDockerRepoMaintanenceStages() {
def nodes = nodesByLabel label:"docker&®istry"
def tasks = [:]
nodes.each { targetNode ->
tasks << cleanupDockerRegistry(targetNode)
}
return tasks
}
def cleanupDockerRegistry(targetNode) {
def taskname = "cleanupDockerRegistry/${targetNode}"
return [(taskname): {
stage(taskname) {
node(targetNode) {
withCredentials([usernameColonPassword(credentialsId:
'docker-hub-elektra-jenkins', variable: 'AUTH')]) {
// Remove all but the latest 10 versions of each image in
// the registry
docker.image('anoxis/registry-cli')
.run('--rm', '-l $AUTH -r https://hub.libelektra.org --delete')
// remove all images with Elektra packages installed in the registry
docker.image('anoxis/registry-cli')
.run('--rm', '-l $AUTH -r https://hub.libelektra.org --delete-all --images-like ".*-installed$"')
}
def frontendFolder = "${env.HOME}/compose/frontend"
dir(frontendFolder){
// Run garbage collect on the registry
sh 'COMPOSE_INTERACTIVE_NO_CLI=1 docker-compose run -T --rm registry bin/registry garbage-collect /etc/docker/registry/config.yml'
}
}
}
}]
}
def generateFileCleanupStages() {
def tasks = [:]
tasks << cleanupDoc('coverage/')
tasks << cleanupDoc('api/pr/')
tasks << cleanupAptlyDb()
return tasks
}
def cleanupDoc(path) {
def cleanupCommand = """
find /srv/libelektra/${path} \
-mindepth 1 \
-maxdepth 1 \
-type d \
-ctime +30 \
-print0 \
| xargs -r -0 rm -R
"""
def taskname = "cleanup-${path}"
return runCommandOnCommunity(taskname, cleanupCommand)
}
def cleanupAptlyDb() {
def cleanupCommand = 'aptly db cleanup -verbose'
return runCommandOnCommunity('cleanupAptlyDb', cleanupCommand)
}
/***********/
/* Helpers */
/***********/
def sshExec(remoteConfig, command) {
sshPublisher(publishers: [
sshPublisherDesc(
configName: remoteConfig,
transfers: [
sshTransfer(
execCommand: command
)
],
verbose: true
)
])
}
def runCommandOnCommunity(taskname, cleanupCommand) {
return [(taskname): {
stage(taskname) {
node("master") {
sshExec(
'doc.libelektra.org',
cleanupCommand
)
}
}
}]
}
def dockerImages() {
def s = /docker images --format "{{.Repository}}" | awk '!seen[$0]++'/
def r = sh returnStdout: true, script: s
return r.tokenize('\n')
}
def retainNewestImages(image, n, withTag='', excludeTag='none') {
sh """docker images ${image} --format \"{{.Repository}} {{.Tag}}\" \
| grep -v \"${excludeTag}\" \
| grep \"${withTag}\" \
| sort -k 2,2 \
| head -n -${n} \
| awk \'BEGIN{OFS=\":\"} {print \$1,\$2}' \
| xargs -r -n1 docker rmi -f"""
}
def syncRepository(Map repo) {
dir("${env.HOME}/git_mirrors") {
if(fileExists(repo.dest)) {
echo "Found existing repository mirror"
dir(repo.dest){
sh "git fetch --prune"
}
} else {
echo "Create new repository mirror"
sh "git clone --mirror ${repo.url} ${repo.dest}"
}
}
}