node {
    properties([
        parameters([
            string(name: 'NUMBER_OF_SERVERS', defaultValue: '', description: 'Number of servers to create (between 1 and 10).'),
            string(name: 'GROUP', defaultValue: '', description: 'Group name (can not be blank).')
        ])
    ])
    stage('checkout') {
        checkout ([ \
            $class: 'GitSCM', \
            branches: [[name: '*/master']], \
            doGenerateSubmoduleConfigurations: false, \
            extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'devops'], [ $class: 'ScmName', name: 'devops' ], [$class: 'CloneOption', depth: 1, shallow: true]], \
            submoduleCfg: [], \
            userRemoteConfigs: [[credentialsId: '9af2bd5f-a2b4-4470-8356-64e9a5576c0a', url: 'git@github.com:BigscreenVR/devops.git']] \
        ])
        checkout ([ \
            $class: 'GitSCM', \
            branches: [[name: '*/main-ocean']], \
            doGenerateSubmoduleConfigurations: false, \
            extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'cloud'], [ $class: 'ScmName', name: 'cloud' ], [$class: 'CloneOption', depth: 1, shallow: true]], \
            submoduleCfg: [], \
            userRemoteConfigs: [[credentialsId: '52e464f0-e4bd-4adb-9438-a10ffffae1e1', url: 'git@github.com:BigscreenVR/cloud.git']] \
        ])
    }
}
node {
    try {
        stage("pre_build") {
            sh '''#!/bin/bash
NETWORK="main"
FLEET="ocean"
MESSAGE=":ocean::ocean::ocean: **[$NETWORK-$FLEET-$GROUP] Starting job: $JOB_NAME** :rofl::rofl::rofl::ocean::ocean::ocean:"
bash `pwd`/devops/Jenkins/deps/discord.sh \
    --webhook-url="https://discordapp.com/api/webhooks/516791347613532207/YVB-QQcWfwzN5sDunKgNuSmWl3KI1aAEi_QMiX93BMDdTtOmrCxQaiqKGdOHj85g_Yfv" \
    --username "Jenkins" \
    --avatar "https://icon-library.com/images/jenkins-icon/jenkins-icon-15.jpg" \
    --title "$MESSAGE" \
    --color "3066993" \
    --url "$JOB_URL" \
    --timestamp 
'''
        }
        stage("build_media_server_group") {
            echo "Wait for 30 seconds for the servers to finish initializing..."
            sleep(time:30,unit:"SECONDS")
            sh '''#!/bin/bash
. `pwd`/devops/Jenkins/server_builder_v7.sh
NETWORK="main"
FLEET="ocean"
build_media_server_image_if_needed $NETWORK $FLEET
'''
        }
        stage("post_build") {
            sh '''#!/bin/bash
NETWORK="main"
FLEET="ocean"
MESSAGE=":ocean::ocean::ocean: **[$NETWORK-$FLEET-$GROUP] $JOB_NAME ** :rofl::rofl::rofl::ocean::ocean::ocean:"
bash `pwd`/devops/Jenkins/deps/discord.sh \
    --webhook-url="https://discordapp.com/api/webhooks/516791347613532207/YVB-QQcWfwzN5sDunKgNuSmWl3KI1aAEi_QMiX93BMDdTtOmrCxQaiqKGdOHj85g_Yfv" \
    --username "Jenkins" \
    --avatar "https://icon-library.com/images/jenkins-icon/jenkins-icon-15.jpg" \
    --title "$MESSAGE" \
    --color "3066993" \
    --url "$JOB_URL" \
    --description "Built media servers in group $GROUP on $NETWORK-$FLEET" \
    --field "Network;$NETWORK;true" \
    --field "Fleet;$FLEET;true" \
    --field "Group;:$GROUP:;true" \
    --field "Quantity deployed;2;false" \
    --timestamp 
'''
        }
    } catch (e) {
        sh '''#!/bin/bash
NETWORK="main"
FLEET="ocean"
MESSAGE=":x::x::x: **BUILD FAILED [$NETWORK-$FLEET] $JOB_NAME ** :x::x::x:"
bash `pwd`/devops/Jenkins/deps/discord.sh \
    --webhook-url="https://discordapp.com/api/webhooks/516791347613532207/YVB-QQcWfwzN5sDunKgNuSmWl3KI1aAEi_QMiX93BMDdTtOmrCxQaiqKGdOHj85g_Yfv" \
    --username "Jenkins" \
    --avatar "https://icon-library.com/images/jenkins-icon/jenkins-icon-15.jpg" \
    --title "$MESSAGE" \
    --color "15548997" \
    --url "$JOB_URL" \
    --timestamp 
'''
        throw e
    }
}