/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * License); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

apply plugin: 'org.apache.beam.module'
apply plugin: 'base'
applyDockerNature()

if (!project.hasProperty('imageJavaVersion')) {
    throw new GradleException('imageJavaVersion project property must be set')
}

def imageJavaVersion = project.findProperty('imageJavaVersion')

description = "Apache Beam :: SDKs :: Java :: Container :: Java ${imageJavaVersion} Container"

configurations {
    dockerDependency
}

dependencies {
    dockerDependency project(path: ":sdks:java:container:distroless", configuration: "dockerDependency")
}

def pushContainers = project.rootProject.hasProperty(["isRelease"]) || project.rootProject.hasProperty("push-containers")

def base_name = "${project.docker_image_default_repo_prefix}java${imageJavaVersion}_sdk"
def root = project.rootProject.hasProperty(["docker-repository-root"]) ?
        project.rootProject["docker-repository-root"] :
        project.docker_image_default_repo_root
def tag = project.rootProject.hasProperty(["docker-tag"]) ?
        project.rootProject["docker-tag"] : project.sdk_version

def base = containerImageName(
        name: base_name,
        root: root,
        tag: tag)

docker {
    name containerImageName(
            name: "${base_name}_distroless",
            root: root,
            tag: tag)
    tags containerImageTags()
    dockerfile project.file("../Dockerfile")
    files "./build/"
    buildArgs([
            'BEAM_BASE': base,
            'DISTROLESS_BASE': "gcr.io/distroless/java${imageJavaVersion}-debian12"
    ])
    buildx project.useBuildx()
    platform(*project.containerPlatforms())
    load project.useBuildx() && !pushContainers
    push pushContainers
}

dockerPrepare.dependsOn ":sdks:java:container:java${imageJavaVersion}:docker"