Skip to content

Publishing docker images within a build like other artifacts #171

@vierbergenlars

Description

@vierbergenlars

For normal gradle artifacts, consuming them from another subproject of the build works with cross-project publications: https://docs.gradle.org/current/userguide/cross_project_publications.html#sec:simple-sharing-artifacts-between-projects

How it is now

Currently, docker images can not be consumed this way, which prevents some nice things from working:

  • having some isolation between different projects
  • not needing to explicitly describe the dependency on a task from an other project
// projectA/build.gradle

plugins {
   id "eu.xenit.docker"
}
// projectB/build.gradle

plugins {
  id "eu.xenit.docker"
}

evaluationDependsOn(":projectA")

createDockerFile {
  dependsOn(":projectA:buildDockerImage")
  from(project(":projectA").tasks.named("buildDockerImage").flatMap({ it.imageId }))
}

How Gradle does it for other artifacts

In its simplest form, an output (file/directory) that is created by a task in project A is attached to a configuration in project A.
To use it from project B, a configuration is created and a dependency is added on project(path: ":projectA", configuration: "XX"), which is then consumed by the task from project B that needs the artifact.

// projectA/build.gradle
plugins {
  id "java"
}
// projectB/build.gradle
configurations {
  YY
}

dependencies {
  YY project(path: ":projectA", configuration: "archives")
}

How it could be

// projectA/build.gradle

plugins {
   id "eu.xenit.docker"
}
// projectB/build.gradle

plugins {
   id "eu.xenit.docker"
}

configurations {
   YY
}

dependencies {
   YY project(path: ":projectA", configuration: "dockerImage")
}

createDockerFile {
   from(YY)
}

Necessary changes

Automatically create an outgoing configuration named dockerImage. This configuration should have a single artifact that represents the image ID of the image built by buildDockerImage. Since configurations can only contain files, it shall be a textfile containing the image ID. The artifact should also be builtBy(buildDockerImage), so it is automatically built when the configuration is consumed an used.

On the consuming side, a Dockerfile convention should add a method: from(Configuration), which will add a dependency on the configuration, and internally wire to from(Provider<String>). In the provider, we will assert that there is only a single file present in the configuration (with Configuration#getSingleFile()), and then read the image ID from that file.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions