Debian Codegraph @

sdk/jenkinsfile/Jenkinsfile_MR_iOS

raw
1 import groovy.json.JsonSlurperClassic
2
3 // Uploads a file to a Gitlab project
4 // Requires env.GITLAB_BASE_URL
5 String uploadFileToGitLab(String fileName, String projectId) {
6 String link = ""
7 String response = ""
8 withCredentials([string(credentialsId: 'gitlab-api-token', variable: 'TOKEN')]) {
9 response = sh(script: "curl -s --request POST --header 'PRIVATE-TOKEN:\$TOKEN' --form file=@${fileName} ${env.GITLAB_BASE_URL}/api/v4/projects/${projectId}/uploads", returnStdout: true).trim()
10 }
11 link = new JsonSlurperClassic().parseText(response).markdown
12 echo "Logs uploaded to: ${link}"
13 return link
14 }
15
16 // Downloads the console log from this Jenkins build
17 void downloadJenkinsConsoleLog(String fileName) {
18 withCredentials([usernameColonPassword(credentialsId: 'jenkins-ro', variable: 'CREDENTIALS')]) {
19 sh "curl -u \"\${CREDENTIALS}\" ${BUILD_URL}consoleText -o ${fileName}"
20 }
21 }
22
23 // Downloads the logs of the build, uploads them to the gitlab project
24 // And return the URL
25 String getLogsUrl(String projectId) {
26 String message = ""
27 String fileName = "build.log"
28 String logUrl = ""
29 downloadJenkinsConsoleLog(fileName)
30 return uploadFileToGitLab(fileName, projectId)
31 }
32
33 // Post a failure message in MR
34 void commentFailedBuild() {
35 logUrl = getLogsUrl(env.PROJECT_ID)
36 addGitLabMRComment(comment: ":red_circle: ${currentBuild.projectName} :green_apple: <b>SDK iOS</b> FAILURE :worried:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${env.GIT_COMMIT}<br/>Logs: ${logUrl}" )
37 updateGitlabCommitStatus(name: 'Build iOS', state: 'failed')
38 }
39
40 pipeline {
41 agent none
42 options {
43 buildDiscarder(logRotator(numToKeepStr: '135', daysToKeepStr: '21'))
44 gitLabConnection('GitLabConnectionJenkins')
45 ansiColor('xterm')
46 }
47
48 parameters {
49 booleanParam(name: 'IOS_REBUILD_3RDPARTY', defaultValue: false, description: 'Should we rebuild 3rdparty deps for iOS?')
50 }
51
52 environment {
53 BUILD_OPTIONS = ' '
54 }
55
56 stages {
57 stage('Update Gitlab commitStatus') {
58 agent any
59 steps {
60 updateGitlabCommitStatus(name: 'Build iOS', state: 'running')
61 script{
62 COMMIT_ID = env.GIT_COMMIT
63 println GIT_COMMIT
64 }
65 }
66 }
67 stage('Build'){
68 agent { label 'osx && arm64' }
69 stages {
70 stage('clean previous runs'){
71 steps{
72 deleteDir()
73 }
74 }
75 stage('Get build parameters'){
76 parallel{
77 stage('Get build options'){
78 when {
79 allOf {
80 expression { env.gitlabTriggerPhrase != null }
81 expression { env.gitlabTriggerPhrase.contains('BUILD_OPTIONS') }
82 }
83 }
84 steps {
85 script{
86 BUILD_OPTIONS = sh(script: 'echo "$gitlabTriggerPhrase" | grep BUILD_OPTIONS | awk -F "BUILD_OPTIONS=" \'{print \$2}\' | cut -d"\"" -f2', returnStdout: true).trim()
87 println BUILD_OPTIONS
88 }
89 }
90 }
91
92 stage('Get MEGAchat branch'){
93 steps {
94 script{
95 env.MEGACHAT_BRANCH = sh(script: 'echo "$gitlabMergeRequestDescription" | grep MEGACHAT_BRANCH_TO_TEST | awk -F "MEGACHAT_BRANCH_TO_TEST=" \'{print \$2}\' | cut -d" " -f1', returnStdout: true).trim()
96 if (MEGACHAT_BRANCH == ""){
97 echo "MEGACHAT_BRANCH was not found on description so develop will be used by default"
98 env.MEGACHAT_BRANCH = "develop"
99 }
100 println MEGACHAT_BRANCH
101 }
102 }
103 }
104
105 stage('Get iOS branch'){
106 steps {
107 script{
108 env.IOS_BRANCH = sh(script: 'echo "$gitlabMergeRequestDescription" | grep IOS_BRANCH_TO_TEST | awk -F "IOS_BRANCH_TO_TEST=" \'{print \$2}\' | cut -d" " -f1', returnStdout: true).trim()
109 if (IOS_BRANCH == ""){
110 echo "IOS_BRANCH was not found on description so develop will be used by default"
111 env.IOS_BRANCH = "develop"
112 }
113 println IOS_BRANCH
114 }
115 }
116 }
117 }
118 }
119
120 stage('Checkout SDK MEGAchat and iOS'){
121 steps {
122 //Clone iOS
123 sh "echo Cloning iOS branch \"${IOS_BRANCH}\""
124 checkout([
125 $class: 'GitSCM',
126 branches: [[name: "${IOS_BRANCH}"]],
127 userRemoteConfigs: [[ url: "${env.GIT_URL_IOS}", credentialsId: "12492eb8-0278-4402-98f0-4412abfb65c1" ]],
128 extensions: [
129 [$class: "UserIdentity",name: "jenkins", email: "jenkins@jenkins"]
130 ]
131 ])
132 withCredentials([gitUsernamePassword(credentialsId: 'jenkins_sdk_token_with_user', gitToolName: 'Default')]) {
133 sh "git submodule update --init --recursive"
134 }
135 script{
136 ios_sources_workspace = WORKSPACE
137 sdk_sources_workspace = "${ios_sources_workspace}/Modules/DataSource/MEGASDK/Sources/MEGASDK"
138 megachat_sources_workspace = "${ios_sources_workspace}/Modules/DataSource/MEGAChatSDK/Sources/MEGAChatSDK"
139 }
140 dir('Modules/DataSource/MEGAChatSDK/Sources/MEGAChatSDK'){
141
142 //Clone MEGAchat
143 sh "echo Cloning megachat branch \"${env.MEGACHAT_BRANCH}\""
144 checkout([
145 $class: 'GitSCM',
146 branches: [[name: "${env.MEGACHAT_BRANCH}"]],
147 userRemoteConfigs: [[ url: "${env.GIT_URL_MEGACHAT}", credentialsId: "12492eb8-0278-4402-98f0-4412abfb65c1" ]],
148 extensions: [
149 [$class: "UserIdentity",name: "jenkins", email: "jenkins@jenkins"]
150 ]
151 ])
152 }
153 dir('Modules/DataSource/MEGASDK/Sources/MEGASDK'){
154 //Clone SDK (with PreBuildMerge)
155 checkout([
156 $class: 'GitSCM',
157 branches: [[name: "origin/${env.gitlabSourceBranch}"]],
158 userRemoteConfigs: [[ url: "${GIT_URL_SDK}", credentialsId: "12492eb8-0278-4402-98f0-4412abfb65c1" ]],
159 extensions: [
160 [$class: "UserIdentity",name: "jenkins", email: "jenkins@jenkins"],
161 [$class: 'PreBuildMerge', options: [fastForwardMode: 'FF', mergeRemote: "origin", mergeStrategy: 'DEFAULT', mergeTarget: "${env.gitlabTargetBranch}"]]
162 ]
163 ])
164 }
165 }
166 }
167 stage('Build SDK and iOS'){
168 environment {
169 PATH = "/usr/local/bin:${env.PATH}"
170 LIBTOOLIZE = "/usr/local/bin/glibtoolize"
171 }
172 options{
173 timeout(time: 200, unit: 'MINUTES')
174 }
175 steps{
176 sh "bundle config set --local path 'vendor/bundle'"
177 sh "bundle install"
178 dir("${megachat_sources_workspace}/src"){
179 sh "cmake -P genDbSchema.cmake"
180 }
181 script {
182 if (params.IOS_REBUILD_3RDPARTY == "true") {
183 dir("${sdk_sources_workspace}/bindings/ios/3rdparty"){
184 sh "sh build-all.sh --enable-chat"
185 }
186 }
187 else {
188 sh "bundle exec fastlane configure_sdk_and_chat_library use_cache:true"
189 }
190 }
191 withCredentials([gitUsernamePassword(credentialsId: 'jenkins_sdk_token_with_user', gitToolName: 'Default')]) {
192 sh "bundle exec fastlane update_plugins"
193 sh "bundle exec fastlane build_simulator"
194 }
195 }
196 post {
197 always {
198 sh "tar czvf buildlog.tar.gz buildlog/ || true"
199 archiveArtifacts artifacts: 'buildlog.tar.gz', allowEmptyArchive: true, onlyIfSuccessful: false
200 }
201 }
202 }
203 }
204 post{
205 always {
206 deleteDir()
207 }
208 failure {
209 node('linux') { // We need an agent able to download logs from Jenkins.
210 script {
211 commentFailedBuild()
212 }
213 }
214 }
215 }
216 }
217 }
218 post {
219 success {
220 updateGitlabCommitStatus(name: 'Build iOS', state: 'success')
221 addGitLabMRComment(comment: ":white_check_mark: ${currentBuild.projectName} :green_apple: <b>SDK iOS</b> SUCCEEDED :muscle:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${COMMIT_ID}" )
222 }
223 unstable {
224 updateGitlabCommitStatus(name: 'Build iOS', state: 'failed')
225 addGitLabMRComment(comment: ":interrobang: ${currentBuild.projectName} :green_apple: <b>SDK iOS</b> UNSTABLE :confused:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${COMMIT_ID}" )
226 }
227 aborted {
228 updateGitlabCommitStatus(name: 'Build iOS', state: 'canceled')
229 addGitLabMRComment(comment: ":interrobang: ${currentBuild.projectName} :green_apple: <b>SDK iOS</b> ABORTED :confused:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${COMMIT_ID}" )
230 }
231 }
232 }