GoogleContainerTools / jib

🏗 Build container images for your Java applications.
Apache License 2.0
13.69k stars 1.44k forks source link

Allow several jib configurations in gradle plugin #1828

Closed johannespostler closed 1 month ago

johannespostler commented 5 years ago

Description of the issue: Sometimes it is required to have several configurations in gradle depending on the lifecycle stage of the code deployed. Specifically I'd like to have two jib configurations that tag images differently and push them to different behavious.

Expected behavior:

I want to have a gradle configuration like so:

task jibProduction(type: JibTask)  {
    to {
        image = 'eu.gcr.io/project/production-image'
    }
}

task jibTesting(type: JibTask)  {
    to {
        image = 'eu.gcr.io/project/testing-image'
    }
}

Steps to reproduce:

Use above gradle config closure.

Environment: Gradle 4.10

Kotlin DSL: 1.0-rc-3 Kotlin: 1.2.60 Groovy: 2.4.15 Ant: Apache Ant(TM) version 1.9.11 compiled on March 23 2018 JVM: 1.8.0_181 (Oracle Corporation 25.181-b13) OS: Linux 5.1.12-300.fc30.x86_64 amd64

jib-gradle-plugin Configuration: see above

Log output: Cannot create a proxy class for abstract class 'JibTask'.

Additional Information: I also tried to create a JibExtension and use this to configure a jibTask.

Any other alternative to achieve a similar result is also fine. For example, the same problem can be tackled with the appEngine gradle plugin using configuration files:

appengine {
    deploy {
        projectId = 'my-app-engine-project'
    }
}

task appengineDeployProduction {
    group = "app engine standard environment"
    description = "Deploys the app on the production environment."
    doFirst {
        copy{
            from("src/main/webapp/WEB-INF/appengine-web"){
                include "appengine-web-production.xml"
            }
            into("src/main/webapp/WEB-INF")
            rename('appengine-web-production.xml', 'appengine-web.xml')
        }
    }
    finalizedBy appengineDeploy
}

task appengineDeployTesting {
    group = "app engine standard environment"
    description = "Deploys the app on the testing environment."
    doFirst {
        copy{
            from("src/main/webapp/WEB-INF/appengine-web"){
                include "appengine-web-testing.xml"
            }
            into("src/main/webapp/WEB-INF")
            rename('appengine-web-testing.xml', 'appengine-web.xml')
        }
    }
    doLast {
        appengine.deploy.projectId = "my-app-engine-testing-project"
    }
    finalizedBy appengineDeploy
}
chanseokoh commented 5 years ago

Not sure if you will like it, but what if you use a property (for example, passing -Pprod)?

if (project.hasProperty('prod')) {
  jib.to.image = 'eu.gcr.io/project/production-image'
} else {
  jib.to.image = 'eu.gcr.io/project/testing-image'
}
johannespostler commented 5 years ago

Hi Chanseok, We tried this and combined it with settings this property in two distinct tasks (finalizedBy jib). However, it didn't work as Gradle expected to have the property during the configuration phase already. We hence came up with the following solution:

task jibProduction {
    doFirst {
        jib.to.image = "eu.gcr.io/project/image-prod"
    }
    finalizedBy ':subproject:jib'
}
task jibTesting {
    doFirst {
        jib.to.image = "eu.gcr.io/project/image-testing"
    }
    finalizedBy ':subproject:jib'
}

This works fine for us. Thanks for the help! I'm not sure as to whether to close the issue or not, as several gradle jib-tasks might still be useful im some cases?

salvianreynaldi commented 1 year ago

we can create new BuildDockerTask, BuildImageTask and BuildTarTask tasks. However, integrations with other gradle plugins which assume the use of the default jib task will break though