plugins {
    id 'org.jdrupes.vmoperator.java-application-conventions'
}

dependencies {
}

project.ext.gitBranch = grgit.branch.current.name.replace('/', '-')
def registry = "${project.rootProject.properties['docker.registry']}"
def rootVersion = rootProject.version

task buildImage(type: Exec) {
    inputs.files 'Containerfile'
    
    commandLine 'podman', 'build', '--pull',
        '-t', "${project.name}:${project.gitBranch}",\
        '-f', 'Containerfile', '.'
}

task pushImage(type: Exec) {
    dependsOn buildImage

    commandLine 'podman', 'push', '--tls-verify=false', \
        "${project.name}:${project.gitBranch}", \
        "${registry}/${project.name}:${project.gitBranch}"
}
task tagWithVersion(type: Exec) {
    dependsOn pushImage

    enabled = !rootVersion.contains("SNAPSHOT")
    
    commandLine 'podman', 'push', \
        "${project.name}:${project.gitBranch}",\
        "${registry}/${project.name}:${project.version}"
}

task tagAsLatest(type: Exec) {
    dependsOn tagWithVersion

    enabled = !rootVersion.contains("SNAPSHOT")
        && !rootVersion.contains("alpha") \
        && !rootVersion.contains("beta") \
        || project.rootProject.properties['docker.testRegistry'] \
            && project.rootProject.properties['docker.registry'] \
            == project.rootProject.properties['docker.testRegistry']

    commandLine 'podman', 'push', \
        "${project.name}:${project.gitBranch}",\
        "${registry}/${project.name}:latest"
}

task publishImage {
    dependsOn pushImage
    dependsOn tagWithVersion
    dependsOn tagAsLatest
}
test {
    enabled = project.hasProperty("k8s.testCluster")

    useJUnitPlatform()

    testLogging {
        showStandardStreams = true
    }
    
    systemProperty "k8s.testCluster", project.hasProperty("k8s.testCluster")
        ? project.getProperty("k8s.testCluster") : null
}
