forked from dhis2/dhis2-android-capture-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
204 lines (194 loc) · 7.22 KB
/
Jenkinsfile
File metadata and controls
204 lines (194 loc) · 7.22 KB
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
//Sets cron schedule just for PUSH job
String cron_string = JOB_NAME.startsWith('android-multibranch-PUSH') ? '0 0 * * *' : ''
pipeline {
agent {
label "ec2-android"
}
triggers {
cron(cron_string)
}
options {
buildDiscarder(logRotator(daysToKeepStr: '5'))
disableConcurrentBuilds(abortPrevious: true)
skipStagesAfterUnstable()
}
stages {
stage('Check for [skip ci]') {
when {
expression {
return isSkipCI()
}
}
steps {
script {
currentBuild.result = 'UNSTABLE' // Mark build as a warning instead of an error
echo "⚠️ Warning: Skipping CI because '[skip ci]' was found in the PR title or description."
}
}
}
stage('Change to JAVA 17') {
steps {
script {
echo 'Changing JAVA version to 17'
sh 'sudo update-alternatives --set java /usr/lib/jvm/java-17-openjdk-amd64/bin/java'
env.JAVA_HOME = '/usr/lib/jvm/java-17-openjdk-amd64'
}
}
}
stage('Check PR Size') {
when {
expression {
return !isSkipSizeCheck()
}
}
environment {
GIT_BRANCH = "${env.CHANGE_BRANCH}"
GIT_BRANCH_DEST = "${env.CHANGE_TARGET == null ? '' : env.CHANGE_TARGET}"
}
steps {
script {
echo "Checking PR Size against ${env.CHANGE_TARGET ?: 'None (CI build)'}"
sh 'chmod +x ./scripts/check_pr_size.sh'
sh './scripts/check_pr_size.sh'
}
}
}
stage('Lint Check') {
steps {
script {
echo 'Running Ktlint'
sh './gradlew ktlintCheck'
}
}
}
stage('Unit tests') {
environment {
ANDROID_HOME = '/opt/android-sdk'
}
steps {
script {
echo 'Running unit tests'
sh './gradlew testDebugUnitTest testDhis2DebugUnitTest --stacktrace --no-daemon'
}
}
}
stage('Build Test APKs') {
steps {
script {
echo 'Building UI APKs'
sh './gradlew :app:assembleDhis2Debug :app:assembleDhis2DebugAndroidTest :form:assembleAndroidTest'
}
}
}
stage('Run Form Tests') {
environment {
BROWSERSTACK = credentials('android-browserstack')
form_apk = sh(returnStdout: true, script: 'find form/build/outputs -iname "*.apk" | sed -n 1p')
form_apk_path = "${env.WORKSPACE}/${form_apk}"
buildTag = "${env.GIT_BRANCH} - form"
}
steps {
dir("${env.WORKSPACE}/scripts"){
script {
echo 'Browserstack deployment and running Form module tests'
sh 'chmod +x browserstackJenkinsForm.sh'
sh './browserstackJenkinsForm.sh'
}
}
}
}
stage('Run UI Tests in portrait') {
environment {
BROWSERSTACK = credentials('android-browserstack')
app_apk = sh(returnStdout: true, script: 'find app/build/outputs/apk/dhis2/debug -iname "*.apk"')
test_apk = sh(returnStdout: true, script: 'find app/build/outputs/apk/androidTest -iname "*.apk"')
app_apk_path = "${env.WORKSPACE}/${app_apk}"
test_apk_path = "${env.WORKSPACE}/${test_apk}"
buildTag = "${env.GIT_BRANCH}"
}
steps {
dir("${env.WORKSPACE}/scripts"){
script {
echo 'Browserstack deployment and running tests'
sh 'chmod +x browserstackJenkins.sh'
sh './browserstackJenkins.sh'
}
}
}
}
stage('Run UI Tests in Landscape') {
environment {
BROWSERSTACK = credentials('android-browserstack')
app_apk = sh(returnStdout: true, script: 'find app/build/outputs/apk/dhis2/debug -iname "*.apk"')
test_apk = sh(returnStdout: true, script: 'find app/build/outputs/apk/androidTest -iname "*.apk"')
app_apk_path = "${env.WORKSPACE}/${app_apk}"
test_apk_path = "${env.WORKSPACE}/${test_apk}"
buildTag = "${env.GIT_BRANCH}"
}
steps {
dir("${env.WORKSPACE}/scripts"){
script {
echo 'Browserstack deployment and running tests'
sh 'chmod +x browserstackJenkinsLandscape.sh'
sh './browserstackJenkinsLandscape.sh'
}
}
}
}
stage('JaCoCo report') {
steps {
script {
echo 'Running JaCoCo report on app module'
sh './gradlew jacocoReport --stacktrace --no-daemon'
}
}
}
stage('Sonarqube') {
environment {
GIT_BRANCH = "${env.GIT_BRANCH}"
// Jenkinsfile considers empty value ('') as null
GIT_BRANCH_DEST = "${env.CHANGE_TARGET == null ? '' : env.CHANGE_TARGET}"
PULL_REQUEST = "${env.CHANGE_ID == null ? '' : env.CHANGE_ID }"
SONAR_TOKEN = credentials('android-sonarcloud-token')
}
steps {
script {
echo 'Running Sonarqube'
sh 'chmod +x ./scripts/sonarqube.sh'
sh './scripts/sonarqube.sh'
}
}
}
}
post {
success {
sendNotification(env.GIT_BRANCH, '*Build Succeeded*\n', 'good')
}
failure {
sendNotification(env.GIT_BRANCH, '*Build Failed*\n', 'bad')
}
}
}
def sendNotification(String branch, String messagePrefix, String color){
if( !branch.startsWith('PR') ){
slackSend channel: '#android-capture-app-ci', color: color, message: messagePrefix+ custom_msg()
}
}
def custom_msg(){
def BUILD_URL= env.BUILD_URL
def JOB_NAME = env.JOB_NAME
def BUILD_ID = env.BUILD_ID
def BRANCH_NAME = env.GIT_BRANCH
def JENKINS_LOG= "*Job:* $JOB_NAME\n *Branch:* $BRANCH_NAME\n *Build Number:* $BUILD_NUMBER (<${BUILD_URL}|Open>)"
return JENKINS_LOG
}
def isSkipCI() {
def prTitle = env.CHANGE_TITLE ?: ""
def prDescription = env.CHANGE_DESCRIPTION ?: ""
return (prTitle.contains("[skip ci]") || prDescription.contains("[skip ci]"))
}
def isSkipSizeCheck() {
def prTitle = env.CHANGE_TITLE ?: ""
def prDescription = env.CHANGE_DESCRIPTION ?: ""
return (prTitle.contains("[skip size]") || prDescription.contains("[skip size]"))
}