dependencies {
    project(":frontend")
}

def getDateSuffix() {
    return "-" + new Date().format('yyyyMMddHHmmss')
}

def env = System.getenv()
def gcpProjectID = env["BPG_GCP_PROJECT_ID"]
def imageTag = 'gcr.io/' + gcpProjectID +'/playground-web:' + project.version + getDateSuffix()

task clean(type: Delete) {
    delete "resources/web"
}

task copyPublicContent(type: Copy) {
    dependsOn(":frontend:build")
    from project(":frontend").buildDir
    into "resources/web" 
}

def inputFiles = [
    project.file("cloudbuild.yaml"),
    project.file("nginx.conf"),
    project.file("Dockerfile"),
    project.file("resources")
]

task cloudBuild(type: Exec) {
    dependsOn clean
    dependsOn copyPublicContent
    inputs.files(inputFiles)
    commandLine "gcloud", "builds", "submit", "--config", "cloudbuild.yaml", 
        "--substitutions=_IMAGE_TAG=" + imageTag, "."
}

task build(type: Exec) {
    dependsOn clean
    dependsOn copyPublicContent
    inputs.files(inputFiles)
    commandLine 'docker', 'build', '-t', imageTag, '.'
}
