pipeline {
    agent { label 'linux' }

    options { 
        buildDiscarder(logRotator(numToKeepStr: '135', daysToKeepStr: '21'))
        gitLabConnection('GitLabConnectionJenkins')
    }

    environment {
        BUILD_OPTIONS = ' '
        APIURL_TO_TEST = ' '
    }

    stages {
        stage('clean previous runs'){
            steps{
                deleteDir()
            }
        }
        stage('Get build parameters'){
            parallel{
                stage('Get current build description'){
                    steps{
                        script{
                            println "${env.gitlabMergeRequestDescription}"
                            env.currentBuildDescription = currentBuild.description
                        }
                    }
                }
                stage('Get build options'){
                    when {
                        allOf {
                            expression { env.gitlabTriggerPhrase != null }
                            expression { env.gitlabTriggerPhrase.contains('BUILD_OPTIONS') }
                        }
                    }
                    steps {
                        script{
                            BUILD_OPTIONS = sh(script: 'echo "$gitlabTriggerPhrase" | grep BUILD_OPTIONS | awk -F "BUILD_OPTIONS="  \'{print \$2}\' | cut -d"\"" -f2', returnStdout: true).trim()
                            println BUILD_OPTIONS
                        }
                    }
                    post{
                        always {
                            script{                        
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.JOB_NAME} FAILURE when getting the additional build parameters :worried:<br/>Build results: [Jenkins [${env.JOB_NAME} ${env.BUILD_DISPLAY_NAME}]](${env.RUN_DISPLAY_URL})<br/>Commit: ${env.GIT_COMMIT}" )
                                }
                            }
                        }
                    }
                }

                stage('Get subjob Jenkinsfile branch to be used'){
                    when {
                        expression { 
                            env.gitlabMergeRequestDescription.contains('JENKINSFILE_SUBJOB_BRANCH')
                        }
                    }
                    steps {
                        script{
                            env.JENKINSFILE_SUBJOB_BRANCH = sh(script: 'echo "$gitlabMergeRequestDescription" | grep JENKINSFILE_SUBJOB_BRANCH | awk -F "JENKINSFILE_SUBJOB_BRANCH="  \'{print \$2}\' | cut -d" " -f1', returnStdout: true).trim()
                            println JENKINSFILE_SUBJOB_BRANCH
                            if (JENKINSFILE_SUBJOB_BRANCH == ""){
                                echo "JENKINSFILE_SUBJOB_BRANCH was not found on description so master will be used by default"
                            }
                        }
                    }
                    post{
                        always {
                            script{                        
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.JOB_NAME} FAILURE when getting the JENKINSFILE_SUBJOB_BRANCH branch :worried:<br/>Build results: [Jenkins [${env.JOB_NAME} ${env.BUILD_DISPLAY_NAME}]](${env.RUN_DISPLAY_URL})<br/>Commit: ${env.GIT_COMMIT}" )
                                }
                            }
                        }
                    }
                }

                stage('Get MEGAchat branch'){
                    when {
                        expression { 
                            env.gitlabMergeRequestDescription.contains('MEGACHAT_BRANCH_TO_TEST')
                        }
                    }
                    steps {
                        script{
                            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()
                            println MEGACHAT_BRANCH
                            if (MEGACHAT_BRANCH == ""){
                                echo "MEGACHAT_BRANCH was not found on description so develop will be used by default"
                            }
                        }
                    }
                    post{
                        always {
                            script{                        
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.JOB_NAME} FAILURE when getting the MEGAchat branch :worried:<br/>Build results: [Jenkins [${env.JOB_NAME} ${env.BUILD_DISPLAY_NAME}]](${env.RUN_DISPLAY_URL})<br/>Commit: ${env.GIT_COMMIT}" )
                                }
                            }
                        }
                    }
                }

                stage('Get Android branch'){
                    when {
                        expression { 
                            env.gitlabMergeRequestDescription.contains('ANDROID_BRANCH_TO_TEST')
                        }
                    }
                    steps {
                        script{
                            env.ANDROID_BRANCH = sh(script: 'echo "$gitlabMergeRequestDescription" | grep ANDROID_BRANCH_TO_TEST | awk -F "ANDROID_BRANCH_TO_TEST="  \'{print \$2}\' | cut -d" " -f1', returnStdout: true).trim()
                            println ANDROID_BRANCH
                            if (ANDROID_BRANCH == ""){
                                echo "ANDROID_BRANCH was not found on description so develop will be used by default"
                            }
                        }
                    }
                    post{
                        always {
                            script{                        
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.JOB_NAME} FAILURE when getting the Android branch :worried:<br/>Build results: [Jenkins [${env.JOB_NAME} ${env.BUILD_DISPLAY_NAME}]](${env.RUN_DISPLAY_URL})<br/>Commit: ${env.GIT_COMMIT}" )
                                }
                            }
                        }
                    }
                }
                
                stage('Get iOS branch'){
                    when {
                        expression { 
                            env.gitlabMergeRequestDescription.contains('IOS_BRANCH_TO_TEST')
                        }
                    }
                    steps {
                        script{
                            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()
                            println IOS_BRANCH
                            if (IOS_BRANCH == ""){
                                echo "IOS_BRANCH was not found on description so develop will be used by default"
                            }
                        }
                    }
                    post{
                        always {
                            script{                        
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.JOB_NAME} FAILURE when getting the iOS branch :worried:<br/>Build results: [Jenkins [${env.JOB_NAME} ${env.BUILD_DISPLAY_NAME}]](${env.RUN_DISPLAY_URL})<br/>Commit: ${env.GIT_COMMIT}" )
                                }
                            }
                        }
                    }
                }

                stage('Get API URL'){
                    when {
                        expression { 
                            env.gitlabMergeRequestDescription.contains('USE_APIURL_TO_TEST')
                        }
                    }
                    steps {
                        script{
                            APIURL_TO_TEST = sh(script: 'echo "$gitlabMergeRequestDescription" | grep USE_APIURL_TO_TEST | awk -F "USE_APIURL_TO_TEST="  \'{print \$2}\' | cut -d" " -f1', returnStdout: true).trim()
                            println APIURL_TO_TEST
                            if (APIURL_TO_TEST == ""){
                                APIURL_TO_TEST = "https://g.api.mega.co.nz/"
                                echo "APIURL_TO_TEST was not found on description so ${APIURL_TO_TEST} will be used by default"
                            }
                            echo "APIURL_TO_TEST will be ${APIURL_TO_TEST}"
                        }
                    }
                    post{
                        always {
                            script{                        
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.JOB_NAME} FAILURE when getting the SDK branch :worried:<br/>Build results: [Jenkins [${env.JOB_NAME} ${env.BUILD_DISPLAY_NAME}]](${env.RUN_DISPLAY_URL})<br/>Commit: ${env.GIT_COMMIT}" )
                                }
                            }
                        }
                    }
                }

            }
        }


        stage('Build SDK'){
            parallel{
                stage('Build macOS'){
                    when {
                        anyOf {
                            expression { env.gitlabTriggerPhrase != null && ( env.gitlabTriggerPhrase == 'trigger compilation' || env.gitlabTriggerPhrase.startsWith('trigger compilation macos'))  }
                            allOf{
                                expression { env.gitlabTriggerPhrase == null }
                                triggeredBy "GitlabWebhookCause" 
                            }
                        }
                    }
                    steps {
                        updateGitlabCommitStatus(name: 'macOS build & test', state: 'running')
                        script {
                            echo "Triggering SDK job for macOS."
                            def triggeredBuild = build job: 'MegaSDK-macosARM64-MR', wait: true, propagate: false, parameters: [
                                string(name: 'SOURCE_BRANCH', value: "${env.gitlabSourceBranch}"),
                                string(name: 'TARGET_BRANCH', value: "${env.gitlabTargetBranch}"),
                                string(name: 'BUILD_OPTIONS', value: "${BUILD_OPTIONS}"),
                                string(name: 'APIURL_TO_TEST', value: "${APIURL_TO_TEST}"),
                                string(name: 'BUILD_DESCRIPTION', value: "${env.currentBuildDescription}"),
                                booleanParam(name: 'BUILD_MACOS', value: true),
                                ].plus(env.JENKINSFILE_SUBJOB_BRANCH ? [string(name: 'JENKINSFILE_SUBJOB_BRANCH', value: "${JENKINSFILE_SUBJOB_BRANCH}")] : [])
                            env.TRIGGERED_BUILD_URL_MACOS = triggeredBuild.getAbsoluteUrl()
                            env.TRIGGERED_PROJECT_NAME_MACOS = triggeredBuild.getProjectName()
                            env.TRIGGERED_BUILD_DISPLAY_NAME_MACOS = triggeredBuild.getFullDisplayName()
                        }
                    }
                    
                    post{
                        always {
                            script{
                                if (currentBuild.currentResult == 'SUCCESS'){
                                    addGitLabMRComment(comment: ":white_check_mark: ${env.TRIGGERED_PROJECT_NAME_MACOS} :green_apple: <b>macOS</b> SUCCEEDED :muscle:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_MACOS}]](${env.TRIGGERED_BUILD_URL_MACOS})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'macOS build & test', state: 'success')
                                }                                
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.TRIGGERED_PROJECT_NAME_MACOS} :green_apple: <b>macOS</b> FAILURE  :worried:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_MACOS}]](${env.TRIGGERED_BUILD_URL_MACOS})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'macOS build & test', state: 'failed')
                                    error "Marking parent job as failed because ${env.TRIGGERED_BUILD_DISPLAY_NAME_MACOS}: ${env.TRIGGERED_BUILD_URL_MACOS} has failed"
                                }
                                if (currentBuild.currentResult == 'ABORTED'){
                                    addGitLabMRComment(comment: ":interrobang: ${env.TRIGGERED_PROJECT_NAME_MACOS} :green_apple: <b>macOS</b> ABORTED  :confused:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_MACOS}]](${env.TRIGGERED_BUILD_URL_MACOS})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'macOS build & test', state: 'canceled')
                                    error "Marking parent job as aborted because ${env.TRIGGERED_BUILD_DISPLAY_NAME_MACOS}: ${env.TRIGGERED_BUILD_URL_MACOS} has been canceled"
                                }                                
                                if (currentBuild.currentResult == 'UNSTABLE'){
                                    addGitLabMRComment(comment: ":interrobang: ${env.TRIGGERED_PROJECT_NAME_MACOS} :green_apple: <b>macOS</b> UNSTABLE  :confused:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_MACOS}]](${env.TRIGGERED_BUILD_URL_MACOS})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'macOS build & test', state: 'failed')
                                    error "Marking parent job as failed because ${env.TRIGGERED_BUILD_DISPLAY_NAME_MACOS}: ${env.TRIGGERED_BUILD_URL_MACOS} has failed"
                                }
                            }
                        }
                    }
                }

                stage('Build Linux'){
                    when{
                        anyOf {
                            expression { env.gitlabTriggerPhrase != null && ( env.gitlabTriggerPhrase == 'trigger compilation' || env.gitlabTriggerPhrase.startsWith('trigger compilation linux'))  }
                            allOf{
                                expression { env.gitlabTriggerPhrase == null }
                                triggeredBy "GitlabWebhookCause" 
                            }
                        }
                    }
                    steps {
                        updateGitlabCommitStatus(name: 'Linux build & test', state: 'running')
                        script {
                            echo "Triggering SDK job for Linux."
                            def triggeredBuild = build job: 'MegaSDK-linux64-MR', wait: true, propagate: false, parameters: [
                                string(name: 'SOURCE_BRANCH', value: "${env.gitlabSourceBranch}"),
                                string(name: 'TARGET_BRANCH', value: "${env.gitlabTargetBranch}"),
                                string(name: 'BUILD_OPTIONS', value: "${BUILD_OPTIONS}"),
                                string(name: 'APIURL_TO_TEST', value: "${APIURL_TO_TEST}"),
                                string(name: 'BUILD_DESCRIPTION', value: "${env.currentBuildDescription}"),
                                booleanParam(name: 'BUILD_LINUX', value: true)
                                ].plus(env.JENKINSFILE_SUBJOB_BRANCH ? [string(name: 'JENKINSFILE_SUBJOB_BRANCH', value: "${JENKINSFILE_SUBJOB_BRANCH}")] : [])
                            env.TRIGGERED_BUILD_URL_LINUX = triggeredBuild.getAbsoluteUrl()
                            env.TRIGGERED_PROJECT_NAME_LINUX = triggeredBuild.getProjectName()
                            env.TRIGGERED_BUILD_DISPLAY_NAME_LINUX = triggeredBuild.getFullDisplayName()
                        }
                    }
                    post{
                        always {
                            script{
                                if (currentBuild.currentResult == 'SUCCESS'){
                                    addGitLabMRComment(comment: ":white_check_mark: ${env.TRIGGERED_PROJECT_NAME_LINUX} :penguin: <b>Linux</b> SUCCEEDED :muscle:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_LINUX}]](${env.TRIGGERED_BUILD_URL_LINUX})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'Linux build & test', state: 'success')
                                }                                
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.TRIGGERED_PROJECT_NAME_LINUX} :penguin: <b>Linux</b> FAILURE  :worried:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_LINUX}]](${env.TRIGGERED_BUILD_URL_LINUX})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'Linux build & test', state: 'failed')
                                    error "Marking parent job as failed because ${env.TRIGGERED_BUILD_DISPLAY_NAME_LINUX}: ${env.TRIGGERED_BUILD_URL_LINUX} has failed"
                                }
                                if (currentBuild.currentResult == 'ABORTED'){
                                    addGitLabMRComment(comment: ":interrobang: ${env.TRIGGERED_PROJECT_NAME_LINUX} :penguin: <b>Linux</b> ABORTED  :confused:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_LINUX}]](${env.TRIGGERED_BUILD_URL_LINUX})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'Linux build & test', state: 'canceled')
                                    error "Marking parent job as aborted because ${env.TRIGGERED_BUILD_DISPLAY_NAME_LINUX}: ${env.TRIGGERED_BUILD_URL_LINUX} has been canceled"
                                }                                
                                if (currentBuild.currentResult == 'UNSTABLE'){
                                    addGitLabMRComment(comment: ":interrobang: ${env.TRIGGERED_PROJECT_NAME_LINUX} :penguin: <b>Linux</b> UNSTABLE  :confused:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_LINUX}]](${env.TRIGGERED_BUILD_URL_LINUX})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'Linux build & test', state: 'failed')
                                    error "Marking parent job as failed because ${env.TRIGGERED_BUILD_DISPLAY_NAME_LINUX}: ${env.TRIGGERED_BUILD_URL_LINUX} has failed"
                                }
                            }

                        }
                    }
                }

                stage('Build Windows'){
                    when {
                        anyOf {
                            expression { env.gitlabTriggerPhrase != null && ( env.gitlabTriggerPhrase == 'trigger compilation' || env.gitlabTriggerPhrase.startsWith('trigger compilation windows'))  }
                            allOf{
                                expression { env.gitlabTriggerPhrase == null }
                                triggeredBy "GitlabWebhookCause" 
                            }
                        }
                    }
                    steps {
                        updateGitlabCommitStatus(name: 'Windows build & test', state: 'running')
                        script {
                            echo "Triggering SDK job for Windows."
                            def triggeredBuild = build job: 'MegaSDK-winx64-MR', wait: true, propagate: false, parameters: [
                                string(name: 'SOURCE_BRANCH', value: "${env.gitlabSourceBranch}"),
                                string(name: 'TARGET_BRANCH', value: "${env.gitlabTargetBranch}"),
                                string(name: 'BUILD_OPTIONS', value: "${BUILD_OPTIONS}"),
                                string(name: 'APIURL_TO_TEST', value: "${APIURL_TO_TEST}"),
                                string(name: 'BUILD_DESCRIPTION', value: "${env.currentBuildDescription}"),
                                booleanParam(name: 'BUILD_WINDOWS', value: true)
                                ].plus(env.JENKINSFILE_SUBJOB_BRANCH ? [string(name: 'JENKINSFILE_SUBJOB_BRANCH', value: "${JENKINSFILE_SUBJOB_BRANCH}")] : [])
                            env.TRIGGERED_BUILD_URL_WINDOWS = triggeredBuild.getAbsoluteUrl()
                            env.TRIGGERED_PROJECT_NAME_WINDOWS = triggeredBuild.getProjectName()
                            env.TRIGGERED_BUILD_DISPLAY_NAME_WINDOWS = triggeredBuild.getFullDisplayName()
                        }
                    }
                    post{
                        always {
                            script{
                                if (currentBuild.currentResult == 'SUCCESS'){
                                    addGitLabMRComment(comment: ":white_check_mark: ${env.TRIGGERED_PROJECT_NAME_WINDOWS} <b>Windows</b> SUCCEEDED :muscle:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_WINDOWS}]](${env.TRIGGERED_BUILD_URL_WINDOWS})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'Windows build & test', state: 'success')
                                }                                
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.TRIGGERED_PROJECT_NAME_WINDOWS} <b>Windows</b> FAILURE  :worried:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_WINDOWS}]](${env.TRIGGERED_BUILD_URL_WINDOWS})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'Windows build & test', state: 'failed')
                                    error "Marking parent job as failed because ${env.TRIGGERED_BUILD_DISPLAY_NAME_WINDOWS}: ${env.TRIGGERED_BUILD_URL_WINDOWS} has failed"
                                }
                                if (currentBuild.currentResult == 'ABORTED'){
                                    addGitLabMRComment(comment: ":interrobang: ${env.TRIGGERED_PROJECT_NAME_WINDOWS} <b>Windows</b> ABORTED  :confused:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_WINDOWS}]](${env.TRIGGERED_BUILD_URL_WINDOWS})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'Windows build & test', state: 'canceled')
                                    error "Marking parent job as aborted because ${env.TRIGGERED_BUILD_DISPLAY_NAME_WINDOWS}: ${env.TRIGGERED_BUILD_URL_WINDOWS} has been canceled"
                                }                                
                                if (currentBuild.currentResult == 'UNSTABLE'){
                                    addGitLabMRComment(comment: ":interrobang: ${env.TRIGGERED_PROJECT_NAME_WINDOWS} <b>Windows</b> UNSTABLE  :confused:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_WINDOWS}]](${env.TRIGGERED_BUILD_URL_WINDOWS})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'Windows build & test', state: 'failed')
                                    error "Marking parent job as failed because ${env.TRIGGERED_BUILD_DISPLAY_NAME_WINDOWS}: ${env.TRIGGERED_BUILD_URL_WINDOWS} has failed"
                                }
                            }

                        }
                    }
                }

                stage('Build MEGAchat'){
                    when {
                        anyOf {
                            expression { env.gitlabTriggerPhrase != null && ( env.gitlabTriggerPhrase == 'trigger compilation' || env.gitlabTriggerPhrase.startsWith('trigger compilation megachat'))  }
                            allOf{
                                expression { env.gitlabTriggerPhrase == null }
                                triggeredBy "GitlabWebhookCause" 
                            }
                        }
                    }
                    steps {
                        updateGitlabCommitStatus(name: 'MEGAchat build & test', state: 'running')
                        script {
                            echo "Triggering SDK job for MEGAchat."
                            def triggeredBuild = build job: 'MegaSDK-MEGAchat-MR', wait: true, propagate: false, parameters: [
                                string(name: 'SOURCE_BRANCH', value: "${env.gitlabSourceBranch}"),
                                string(name: 'TARGET_BRANCH', value: "${env.gitlabTargetBranch}"),
                                string(name: 'BUILD_OPTIONS', value: "${BUILD_OPTIONS}"),
                                string(name: 'APIURL_TO_TEST', value: "${APIURL_TO_TEST}"),
                                string(name: 'BUILD_DESCRIPTION', value: "${env.currentBuildDescription}"),
                                booleanParam(name: 'BUILD_MEGACHAT', value: true)
                                ].plus(env.MEGACHAT_BRANCH ? [string(name: 'MEGACHAT_BRANCH', value: "${MEGACHAT_BRANCH}")] : []).plus(env.JENKINSFILE_SUBJOB_BRANCH ? [string(name: 'JENKINSFILE_SUBJOB_BRANCH', value: "${JENKINSFILE_SUBJOB_BRANCH}")] : [])

                            env.TRIGGERED_BUILD_URL_MEGACHAT = triggeredBuild.getAbsoluteUrl()
                            env.TRIGGERED_PROJECT_NAME_MEGACHAT = triggeredBuild.getProjectName()
                            env.TRIGGERED_BUILD_DISPLAY_NAME_MEGACHAT = triggeredBuild.getFullDisplayName()
                        }
                    }
                    post{
                        always {
                            script{
                                if (currentBuild.currentResult == 'SUCCESS'){
                                    addGitLabMRComment(comment: ":white_check_mark: ${env.TRIGGERED_PROJECT_NAME_MEGACHAT} :penguin: <b>MEGAchat</b> SUCCEEDED :muscle:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_MEGACHAT}]](${env.TRIGGERED_BUILD_URL_MEGACHAT})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'MEGAchat build & test', state: 'success')
                                }                                
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.TRIGGERED_PROJECT_NAME_MEGACHAT} :penguin: <b>MEGAchat</b> FAILURE  :worried:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_MEGACHAT}]](${env.TRIGGERED_BUILD_URL_MEGACHAT})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'MEGAchat build & test', state: 'failed')
                                    error "Marking parent job as failed because ${env.TRIGGERED_BUILD_DISPLAY_NAME_MEGACHAT}: ${env.TRIGGERED_BUILD_URL_MEGACHAT} has failed"
                                }
                                if (currentBuild.currentResult == 'ABORTED'){
                                    addGitLabMRComment(comment: ":interrobang: ${env.TRIGGERED_PROJECT_NAME_MEGACHAT} :penguin: <b>MEGAchat</b> ABORTED  :confused:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_MEGACHAT}]](${env.TRIGGERED_BUILD_URL_MEGACHAT})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'MEGAchat build & test', state: 'canceled')
                                    error "Marking parent job as aborted because ${env.TRIGGERED_BUILD_DISPLAY_NAME_MEGACHAT}: ${env.TRIGGERED_BUILD_URL_MEGACHAT} has been canceled"
                                }                                
                                if (currentBuild.currentResult == 'UNSTABLE'){
                                    addGitLabMRComment(comment: ":interrobang: ${env.TRIGGERED_PROJECT_NAME_MEGACHAT} :penguin: <b>MEGAchat</b> UNSTABLE  :confused:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_MEGACHAT}]](${env.TRIGGERED_BUILD_URL_MEGACHAT})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'MEGAchat build & test', state: 'failed')
                                    error "Marking parent job as failed because ${env.TRIGGERED_BUILD_DISPLAY_NAME_MEGACHAT}: ${env.TRIGGERED_BUILD_URL_MEGACHAT} has failed"
                                }
                            }

                        }
                    }
                }

                stage('Build Android'){
                    when {
                        anyOf {
                            expression { env.gitlabTriggerPhrase != null && ( env.gitlabTriggerPhrase == 'trigger compilation' || env.gitlabTriggerPhrase.startsWith('trigger compilation android'))  }
                            allOf{
                                expression { env.gitlabTriggerPhrase == null }
                                triggeredBy "GitlabWebhookCause" 
                            }
                        }
                    }
                    steps {
                        updateGitlabCommitStatus(name: 'Android build', state: 'running')
                        script {
                            echo "Triggering SDK job for Android Example and Android App."
                            def triggeredBuild = build job: 'MegaSDK-crossAndroid-MR', wait: true, propagate: false, parameters: [
                                string(name: 'SOURCE_BRANCH', value: "${env.gitlabSourceBranch}"),
                                string(name: 'TARGET_BRANCH', value: "${env.gitlabTargetBranch}"),
                                string(name: 'BUILD_OPTIONS', value: "${BUILD_OPTIONS}"),
                                string(name: 'APIURL_TO_TEST', value: "${APIURL_TO_TEST}"),
                                string(name: 'BUILD_DESCRIPTION', value: "${env.currentBuildDescription}"),
                                booleanParam(name: 'BUILD_ANDROID', value: true)
                                ].plus(env.ANDROID_BRANCH ? [string(name: 'ANDROID_BRANCH', value: "${ANDROID_BRANCH}")] : []).plus(env.MEGACHAT_BRANCH ? [string(name: 'MEGACHAT_BRANCH', value: "${MEGACHAT_BRANCH}")] : []).plus(env.JENKINSFILE_SUBJOB_BRANCH ? [string(name: 'JENKINSFILE_SUBJOB_BRANCH', value: "${JENKINSFILE_SUBJOB_BRANCH}")] : [])

                            env.TRIGGERED_BUILD_URL_ANDROID = triggeredBuild.getAbsoluteUrl()
                            env.TRIGGERED_PROJECT_NAME_ANDROID = triggeredBuild.getProjectName()
                            env.TRIGGERED_BUILD_DISPLAY_NAME_ANDROID = triggeredBuild.getFullDisplayName()
                        }
                    }
                    post{
                        always {
                            script{
                                if (currentBuild.currentResult == 'SUCCESS'){
                                    addGitLabMRComment(comment: ":white_check_mark: ${env.TRIGGERED_PROJECT_NAME_ANDROID} :penguin: <b>Android</b> SUCCEEDED :muscle:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_ANDROID}]](${env.TRIGGERED_BUILD_URL_ANDROID})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'Android build', state: 'success')
                                }                                
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.TRIGGERED_PROJECT_NAME_ANDROID} :penguin: <b>Android</b> FAILURE  :worried:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_ANDROID}]](${env.TRIGGERED_BUILD_URL_ANDROID})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'Android build', state: 'failed')
                                    error "Marking parent job as failed because ${env.TRIGGERED_BUILD_DISPLAY_NAME_ANDROID}: ${env.TRIGGERED_BUILD_URL_ANDROID} has failed"
                                }
                                if (currentBuild.currentResult == 'ABORTED'){
                                    addGitLabMRComment(comment: ":interrobang: ${env.TRIGGERED_PROJECT_NAME_ANDROID} :penguin: <b>Android</b> ABORTED  :confused:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_ANDROID}]](${env.TRIGGERED_BUILD_URL_ANDROID})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'Android build', state: 'canceled')
                                    error "Marking parent job as aborted because ${env.TRIGGERED_BUILD_DISPLAY_NAME_ANDROID}: ${env.TRIGGERED_BUILD_URL_ANDROID} has been canceled"
                                }                                
                                if (currentBuild.currentResult == 'UNSTABLE'){
                                    addGitLabMRComment(comment: ":interrobang: ${env.TRIGGERED_PROJECT_NAME_ANDROID} :penguin: <b>Android</b> UNSTABLE  :confused:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_ANDROID}]](${env.TRIGGERED_BUILD_URL_ANDROID})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'Android build', state: 'failed')
                                    error "Marking parent job as failed because ${env.TRIGGERED_BUILD_DISPLAY_NAME_ANDROID}: ${env.TRIGGERED_BUILD_URL_ANDROID} has failed"
                                }
                            }

                        }
                    }
                }

                stage('Build iOS'){
                    when {
                        anyOf {
                            expression { env.gitlabTriggerPhrase != null && ( env.gitlabTriggerPhrase == 'trigger compilation' || env.gitlabTriggerPhrase.startsWith('trigger compilation ios'))  }
                            allOf{
                                expression { env.gitlabTriggerPhrase == null }
                                triggeredBy "GitlabWebhookCause" 
                            }
                        }
                    }
                    steps {
                        updateGitlabCommitStatus(name: 'iOS build', state: 'running')
                        script {
                            echo "Triggering SDK job for iOS."
                            def triggeredBuild = build job: 'MegaSDK-crossiOS-MR', wait: true, propagate: false, parameters: [
                                string(name: 'SOURCE_BRANCH', value: "${env.gitlabSourceBranch}"),
                                string(name: 'TARGET_BRANCH', value: "${env.gitlabTargetBranch}"),
                                string(name: 'BUILD_OPTIONS', value: "${BUILD_OPTIONS}"),
                                string(name: 'APIURL_TO_TEST', value: "${APIURL_TO_TEST}"),
                                string(name: 'BUILD_DESCRIPTION', value: "${env.currentBuildDescription}"),
                                booleanParam(name: 'BUILD_IOS', value: true)
                                ].plus(env.IOS_BRANCH ? [string(name: 'IOS_BRANCH', value: "${IOS_BRANCH}")] : []).plus(env.MEGACHAT_BRANCH ? [string(name: 'MEGACHAT_BRANCH', value: "${MEGACHAT_BRANCH}")] : []).plus(env.JENKINSFILE_SUBJOB_BRANCH ? [string(name: 'JENKINSFILE_SUBJOB_BRANCH', value: "${JENKINSFILE_SUBJOB_BRANCH}")] : [])

                            env.TRIGGERED_BUILD_URL_IOS = triggeredBuild.getAbsoluteUrl()
                            env.TRIGGERED_PROJECT_NAME_IOS = triggeredBuild.getProjectName()
                            env.TRIGGERED_BUILD_DISPLAY_NAME_IOS = triggeredBuild.getFullDisplayName()
                        }
                    }
                    post{
                        always {
                            script{
                                if (currentBuild.currentResult == 'SUCCESS'){
                                    addGitLabMRComment(comment: ":white_check_mark: ${env.TRIGGERED_PROJECT_NAME_IOS} :green_apple: <b>SDK iOS</b> SUCCEEDED :muscle:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_IOS}]](${env.TRIGGERED_BUILD_URL_IOS})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'iOS build', state: 'success')
                                }                                
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.TRIGGERED_PROJECT_NAME_IOS} :green_apple: <b>SDK iOS</b> FAILURE  :worried:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_IOS}]](${env.TRIGGERED_BUILD_URL_IOS})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'iOS build', state: 'failed')
                                    error "Marking parent job as failed because ${env.TRIGGERED_BUILD_DISPLAY_NAME_IOS}: ${env.TRIGGERED_BUILD_URL_IOS} has failed"
                                }
                                if (currentBuild.currentResult == 'ABORTED'){
                                    addGitLabMRComment(comment: ":interrobang: ${env.TRIGGERED_PROJECT_NAME_IOS} :green_apple: <b>SDK iOS</b> ABORTED  :confused:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_IOS}]](${env.TRIGGERED_BUILD_URL_IOS})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'iOS build', state: 'canceled')
                                    error "Marking parent job as aborted because ${env.TRIGGERED_BUILD_DISPLAY_NAME_IOS}: ${env.TRIGGERED_BUILD_URL_IOS} has been canceled"
                                }                                
                                if (currentBuild.currentResult == 'UNSTABLE'){
                                    addGitLabMRComment(comment: ":interrobang: ${env.TRIGGERED_PROJECT_NAME_IOS} :green_apple: <b>SDK iOS</b> UNSTABLE  :confused:<br/>Build results: [Jenkins [${env.TRIGGERED_BUILD_DISPLAY_NAME_IOS}]](${env.TRIGGERED_BUILD_URL_IOS})<br/>Commit: ${env.GIT_COMMIT}" )
                                    updateGitlabCommitStatus(name: 'iOS build', state: 'failed')
                                    error "Marking parent job as failed because ${env.TRIGGERED_BUILD_DISPLAY_NAME_IOS}: ${env.TRIGGERED_BUILD_URL_IOS} has failed"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
