0

I got a gradle task to build docker images for all my projects. These run fine except for the rootProject which has a dependent task to be executed before the DockerimageBuild itself. This dependency should only apply for therootProject` and not the subprojects. I googled extensively and read the docs but didn't find the perfect solution to add a dependency, probably i am missing a obvious point.

This is my task (stripped out some of the gradle file).

allprojects {
apply plugin: com.bmuschko.gradle.docker.DockerRemoteApiPlugin
    repositories { jcenter() }

docker {
    registryCredentials {
            username = dockerRegistryUsername
            password = dockerRegistryPassword
    }
}

// task build_DockerImage(type: DockerBuildImage, dependsOn: [copy_ImageSource, prepare_ImageConfig]) {
task build_DockerImage(type: DockerBuildImage) {
    group 'Docker'
    description "Builds the docker image: ${dockerTag} locally"
    println "Project: ${rootProject.projectDir}/ci/images/${project.name}"
    inputDir = file("${rootProject.projectDir}/ci/images/${project.name}")
    tag = "azeti/${project.name}:${dockerTag}"
}

It should have dependsOn: copy_ImageSource but i can't figure out the right notation for it.

Thanks for your help. Regards, Sebastian

Sebastian
  • 1
  • 2

1 Answers1

0

The solution is as simple as comparing the current project's name with the ones of the root project and only applying dependsOnfor a match.

I have stripped out rest of the code for the sake of simplicity, check the if condition.

task build_DockerImage(type: DockerBuildImage) {
    group 'Docker'
    description "Builds the docker image: ${dockerTag} locally"
    if(project.name.equals(rootProject.name) ) {
        dependsOn copy_ImageSource
    }
    inputDir = file("${rootProject.projectDir}/ci/images/${project.name}")
    tag = "azeti/${project.name}:${dockerTag}"
}
Sebastian
  • 1
  • 2