CI: Move test group config files to Jenkins
[asterisk/asterisk.git] / tests / CI / periodics-daily.jenkinsfile
1 /*
2  * This pipeline is the "template" for the Asterisk Periodic Tests multi-branch
3  * parent job.  Jenkins will automatically scan the branches in the "asterisk"
4  * or "Security-asterisk" projects in Gerrit and automatically create a branch-
5  * specific job for each branch it finds this file in.
6  *
7  * This file starts as a declarative pipeline because with a declarative
8  * pipeline, you can define the trigger in the pipeline file.  This keeps
9  * everything in one place.  We transition to scripted pipeline later on because
10  * we need to dynamically determine which docker image we're going to use and
11  * you can't do that in a delcarative pipeline.
12  */
13 def timeoutTime = 3
14 def timeoutUnits = 'HOURS'
15 if (env.TIMEOUT_DAILIES) {
16         def _timeout = env.TIMEOUT_DAILIES.split()
17         timeoutTime = _timeout[0].toInteger()
18         timeoutUnits = _timeout[1]
19 }
20
21 pipeline {
22         options {
23                 timestamps()
24                 timeout(time: timeoutTime, unit: timeoutUnits)
25         }
26         triggers {
27                 cron 'H H(0-4) * * *'
28         }
29
30         agent {
31                 /* All of the stages need to be performed on a docker host */
32                 label "swdev-docker"
33         }
34
35         stages {
36                 stage ("->") {
37                         steps {
38                                 /* Here's where we switch to scripted pipeline */
39                                 script {
40                                         manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Host: ${NODE_NAME}", false)
41
42                                         stage ("Checkout") {
43                                                 sh "sudo chown -R jenkins:users ."
44                                                 sh "printenv | sort"
45                                                 sh "sudo tests/CI/setupJenkinsEnvironment.sh"
46                                         }
47
48                                         def images = env.DOCKER_IMAGES.split(' ')
49                                         def r = currentBuild.startTimeInMillis % images.length
50                                         def ri = images[(int)r]
51                                         def randomImage = env.DOCKER_REGISTRY + "/" + ri
52                                         def dockerOptions = "--privileged --ulimit core=0 --ulimit nofile=10240 " +
53                                                 " --tmpfs /tmp:exec,size=1G -v /srv/jenkins:/srv/jenkins:rw -v /srv/cache:/srv/cache:rw " +
54                                                 " --entrypoint=''"
55                                         def bt = env.BUILD_TAG.replaceAll(/[^a-zA-Z0-9_.-]/, '-')
56                                         def outputdir = "tests/CI/output/Testsuite"
57
58                                         manager.createSummary("/plugin/workflow-job/images/48x48/pipelinejob.png").appendText("Docker Image: ${randomImage}", false)
59                                         def img = docker.image(randomImage)
60                                         img.pull()
61
62                                         img.inside(dockerOptions + " --name ${bt}-build") {
63                                                 stage ("Build") {
64                                                         echo 'Building..'
65                                                         env.CCACHE_DIR = "/srv/cache/ccache"
66                                                         sh "./tests/CI/buildAsterisk.sh --branch-name=${BRANCH_NAME} --output-dir=${outputdir} --cache-dir=/srv/cache"
67
68                                                         archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: false,
69                                                                 artifacts: "${outputdir}/*"
70                                                 }
71                                                 stage ("Docs") {
72
73                                                         sh "sudo ./tests/CI/installAsterisk.sh --branch-name=${BRANCH_NAME}  --user-group=jenkins:users"
74
75                                                         def docUrl = env.GIT_URL.replaceAll(/\/[^\/]+$/, "/publish-docs")
76                                                         checkout scm: [$class: 'GitSCM',
77                                                                 branches: [[name: "master"]],
78                                                                         extensions: [
79                                                                                 [$class: 'RelativeTargetDirectory', relativeTargetDir: "tests/CI/output/publish-docs"],
80                                                                                 [$class: 'CloneOption',
81                                                                                         noTags: true,
82                                                                                         depth: 10,
83                                                                                         honorRefspec: true,
84                                                                                         shallow: true
85                                                                                 ],
86                                                                         ],
87                                                                         userRemoteConfigs: [[url: docUrl]]
88                                                                 ]
89
90                                                         sh "./tests/CI/publishAsteriskDocs.sh --user-group=jenkins:users --branch-name=${BRANCH_NAME} --wiki-doc-branch-regex=\"${WIKI_DOC_BRANCH_REGEX}\""
91                                                 }
92                                         }
93
94                                         def testGroups
95                                         configFileProvider([configFile(fileId: 'asterisk_daily_test_groups', variable: 'DAILY_TEST_GROUPS')]) {
96                                         echo "Retrieved config file from ${env.DAILY_TEST_GROUPS}"
97                                                 testGroups = readJSON file: env.DAILY_TEST_GROUPS
98                                         }
99
100                                         def parallelTasks = [ : ]
101
102                                         for (def testGroup in testGroups) {
103                                                 /*
104                                                  * Because each task is a Groovy closure, we need to
105                                                  * keep local references to some variables.
106                                                  */
107                                                 def groupName = testGroup.name
108                                                 def groupDir = testGroup.dir
109                                                 def groupTestcmd = testGroup.testcmd
110                                                 def groupRunTestsuiteOptions = testGroup.runTestsuiteOptions
111                                                 def testsuiteUrl = env.GIT_URL.replaceAll(/\/[^\/]+$/, "/testsuite")
112
113                                                 parallelTasks[groupName] = {
114                                                         stage (groupName) {
115
116                                                                 img.inside("${dockerOptions} --name ${bt}-${groupName}") {
117
118                                                                         lock("${JOB_NAME}.${NODE_NAME}.installer") {
119                                                                                 sh "sudo ./tests/CI/installAsterisk.sh --uninstall-all --branch-name=${BRANCH_NAME} --user-group=jenkins:users"
120                                                                         }
121
122                                                                         sh "sudo rm -rf ${groupDir} || : "
123
124                                                                         checkout scm: [$class: 'GitSCM',
125                                                                                 branches: [[name: "${BRANCH_NAME}"]],
126                                                                                         extensions: [
127                                                                                                 [$class: 'RelativeTargetDirectory', relativeTargetDir: groupDir],
128                                                                                                 [$class: 'CloneOption',
129                                                                                                         noTags: true,
130                                                                                                         depth: 10,
131                                                                                                         honorRefspec: true,
132                                                                                                         shallow: true
133                                                                                                 ],
134                                                                                         ],
135                                                                                         userRemoteConfigs: [[url: testsuiteUrl]]
136                                                                                 ]
137
138                                                                         sh "sudo tests/CI/runTestsuite.sh ${groupRunTestsuiteOptions} --testsuite-dir='${groupDir}' --testsuite-command='${groupTestcmd}'"
139
140                                                                         archiveArtifacts allowEmptyArchive: true, defaultExcludes: false, fingerprint: true,
141                                                                                 artifacts: "${groupDir}/asterisk-test-suite-report.xml, ${groupDir}/logs/**, ${groupDir}/core*.txt"
142
143                                                                         junit testResults: "${groupDir}/asterisk-test-suite-report.xml",
144                                                                                 healthScaleFactor: 1.0,
145                                                                                 keepLongStdio: true
146
147                                                                         echo "Group result d: ${currentBuild.currentResult}"
148                                                                 }
149                                                                 echo "Group result s: ${currentBuild.currentResult}"
150                                                         }
151                                                 }
152                                         }
153                                         parallel parallelTasks
154                                 }
155                         }
156                 }
157         }
158         post {
159                 cleanup {
160                         sh "sudo make distclean >/dev/null 2>&1 || : "
161                         sh "sudo rm -rf tests/CI/output >/dev/null 2>&1 || : "
162                 }
163                 success {
164                         echo "Reporting ${currentBuild.currentResult} Passed"
165                 }
166                 failure {
167                         echo "Reporting ${currentBuild.currentResult}: Failed: Fatal Error"
168                 }
169                 unstable {
170                         echo "Reporting ${currentBuild.currentResult}: Failed: Tests Failed"
171                 }
172         }
173 }