def runDocker(image, cmd) {
    powershell """
        docker run `
          --rm `
          -w "${env.WORKSPACE}" `
          -v "${env.WORKSPACE}:${env.WORKSPACE}:rw" `
          -e "WORKSPACE=${env.WORKSPACE}" `
          -e "BUILD_NUMBER=${env.BUILD_NUMBER}" `
          $image powershell -C "$cmd"
    """
}

def linuxImages = [
    'openjdk:11-jdk',
    'adoptopenjdk/openjdk11-openj9:latest'
]

def createLinuxBuild(dockerImage) {
    return {
        stage("Smoke ${dockerImage}") {
            node {
                checkout scm
                docker.image(dockerImage).inside {
                    try {
                        timeout(30) {
                            sh './gradlew --no-daemon ' + params.gradle_options + ' build'
                        }
                    } finally {
                        junit testResults: '**/build/test-results/**/*.xml', allowEmptyResults: true
                    }
                }
            }
        }
    }
}

def linuxImagesPrivate = [
    'pegasyseng/jdk:oracle-11-0.0.1',
    'pegasyseng/jdk:corretto-11-0.0.1',
    'pegasyseng/jdk:openjdk-12',
    'pegasyseng/jdk:zulu-openjdk-11',
    'pegasyseng/jdk:zulu-openjdk-12'
]

def createLinuxBuildPrivateImage(dockerImage) {
    return {
        stage("Smoke ${dockerImage}") {
            node {
                checkout scm
                docker.withRegistry('https://registry.hub.docker.com', 'dockerhub-pegasysengci') {
                    docker.image(dockerImage).inside {
                        try {
                            timeout(30) {
                                sh './gradlew --no-daemon ' + params.gradle_options + ' build'
                            }
                        } finally {
                            junit testResults: '**/build/test-results/**/*.xml', allowEmptyResults: true
                        }
                    }
                }
            }
        }
    }
}

def windowsImages = [
    "openjdk:11-windowsservercore",
    "openjdk:12-windowsservercore"
]

def createWindowsBuild(dockerImage) {
    return {
        stage("Smoke ${dockerImage}") {
            node("windows-server-2019") {
                checkout scm
                try {
                    timeout(30) {
                        runDocker(
                            dockerImage,
                            ".\\gradlew --no-daemon " + params.gradle_options + " build"
                        )
                    }
                } finally {
                    junit testResults: "**\\build\\test-results\\**\\*.xml", allowEmptyResults: true
                }
            }
        }
    }
}

def builds = [:]

if (params.javas != 'all') {
    builds = builds + (windowsImages.findAll {it.contains(params.javas)}.collectEntries {
        ["Smoke ${it}", createWindowsBuild(it)]
    })
    builds = builds + (linuxImages.findAll {it.contains(params.javas)}.collectEntries {
        ["Smoke ${it}", createLinuxBuild(it)]
    })
    builds = builds + (linuxImagesPrivate.findAll {it.contains(params.javas)}.collectEntries {
        ["Smoke ${it}", createLinuxBuildPrivateImage(it)]
    })
} else {
    builds = builds + (windowsImages.collectEntries {
        ["Smoke ${it}", createWindowsBuild(it)]
    })
    builds = builds + (linuxImages.collectEntries {
        ["Smoke ${it}", createLinuxBuild(it)]
    })
    builds = builds + (linuxImagesPrivate.collectEntries {
        ["Smoke ${it}", createLinuxBuildPrivateImage(it)]
    })
}

parallel builds
