Skip to content

Commit fa149c5

Browse files
committed
Add output image capture mechanism
Authored-by: Leonhardt Koepsell <hello@lnhrdt.com>
1 parent 2fb25a7 commit fa149c5

File tree

8 files changed

+48
-20
lines changed

8 files changed

+48
-20
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ All notable changes to this project will be documented in this file.
99
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
1010
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1111

12-
## [0.3.0] - 2025-04-23
12+
## [0.3.0] - 2025-04-24
1313

1414
### Added
1515

1616
- Introduce container task extension with inputs and outputs.
17+
- Explicit capture methods for tracking image outputs at execution time.
1718

1819
## [0.2.1] - 2025-04-23
1920

examples/buildpacks/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ tasks {
2525
"/var/run/docker.sock:/var/run/docker.sock:ro",
2626
)
2727
}
28+
doLast {
29+
container.outputs.captureLocalImage("my-image:latest")
30+
}
2831
}
2932
}

examples/dind-build/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ tasks {
2222
"/var/run/docker.sock:/var/run/docker.sock:ro",
2323
)
2424
}
25+
doLast {
26+
container.outputs.captureLocalImage("my-image:latest")
27+
}
2528
}
2629
}

examples/input-output-chaining/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,8 @@ tasks {
3333
"/var/run/docker.sock:/var/run/docker.sock:ro",
3434
)
3535
}
36+
doLast {
37+
container.outputs.captureLocalImage("my-image:latest")
38+
}
3639
}
3740
}

src/main/kotlin/dev/codebandits/container/gradle/ContainerTaskExtension.kt

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public abstract class ContainerTaskExtension(task: Task) {
1414

1515
public class Inputs internal constructor(private val task: Task) {
1616
public fun localImage(imageReference: String) {
17-
val trackingFile = task.getLocalImageTrackingFile(imageReference)
18-
task.inputs.file(trackingFile.map { regularFile ->
19-
writeLocalImageId(imageReference, regularFile)
20-
regularFile
17+
val trackingFileProvider = task.getLocalImageTrackingFile(imageReference)
18+
task.inputs.file(trackingFileProvider.map { trackingFile ->
19+
writeLocalImageId(imageReference, trackingFile)
20+
trackingFile
2121
})
2222
}
2323

@@ -34,29 +34,38 @@ public abstract class ContainerTaskExtension(task: Task) {
3434

3535
public class Outputs internal constructor(private val task: Task) {
3636
public fun localImage(imageReference: String) {
37-
val trackingFile = task.getLocalImageTrackingFile(imageReference)
38-
task.outputs.file(trackingFile.map { regularFile ->
39-
writeLocalImageId(imageReference, regularFile)
40-
regularFile
37+
val trackingFileProvider = task.getLocalImageTrackingFile(imageReference)
38+
task.outputs.file(trackingFileProvider.map { trackingFile ->
39+
writeLocalImageId(imageReference, trackingFile)
40+
trackingFile
4141
})
42-
task.doLast { writeLocalImageId(imageReference, trackingFile.get()) }
42+
}
43+
44+
public fun captureLocalImage(imageReference: String) {
45+
val trackingFile = task.getLocalImageTrackingFile(imageReference).get()
46+
writeLocalImageId(imageReference, trackingFile)
4347
}
4448

4549
public fun registryImage(imageReference: String, autoRefresh: Boolean = false) {
46-
val trackingFile = task.getLocalImageTrackingFile(imageReference)
47-
task.outputs.file(trackingFile.map { regularFile ->
48-
if (autoRefresh || !regularFile.asFile.exists()) {
49-
writeRegistryImageDigest(imageReference, regularFile)
50+
val trackingFileProvider = task.getRegistryImageTrackingFile(imageReference)
51+
task.outputs.file(trackingFileProvider.map { trackingFile ->
52+
if (autoRefresh || !trackingFile.asFile.exists()) {
53+
writeRegistryImageDigest(imageReference, trackingFile)
5054
}
51-
regularFile
55+
trackingFile
5256
})
5357
task.doLast {
54-
val regularFile = trackingFile.get()
58+
val regularFile = trackingFileProvider.get()
5559
if (autoRefresh || !regularFile.asFile.exists()) {
5660
writeRegistryImageDigest(imageReference, regularFile)
5761
}
5862
}
5963
}
64+
65+
public fun captureRegistryImage(imageReference: String) {
66+
val trackingFile = task.getRegistryImageTrackingFile(imageReference).get()
67+
writeRegistryImageDigest(imageReference, trackingFile)
68+
}
6069
}
6170
}
6271

@@ -74,17 +83,17 @@ private fun Task.getRegistryImageTrackingFile(
7483
return project.layout.buildDirectory.file("images/registry/$fileName")
7584
}
7685

77-
private fun writeLocalImageId(imageReference: String, regularFile: RegularFile) {
78-
val file = regularFile.asFile
86+
private fun writeLocalImageId(imageReference: String, trackingFile: RegularFile) {
87+
val file = trackingFile.asFile
7988
if (!file.parentFile.exists()) {
8089
file.parentFile.mkdirs()
8190
}
8291
val imageId = Local.getImageId(imageReference)
8392
file.writeText(imageId ?: "")
8493
}
8594

86-
private fun writeRegistryImageDigest(imageReference: String, regularFile: RegularFile) {
87-
val file = regularFile.asFile
95+
private fun writeRegistryImageDigest(imageReference: String, trackingFile: RegularFile) {
96+
val file = trackingFile.asFile
8897
if (!file.parentFile.exists()) {
8998
file.parentFile.mkdirs()
9099
}

src/testFeatures/kotlin/dev/codebandits/container/gradle/GroovySyntaxTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class GroovySyntaxTest : GradleProjectTest() {
4444
"/var/run/docker.sock:/var/run/docker.sock:ro",
4545
] as String[])
4646
}
47+
doLast {
48+
container.outputs.captureLocalImage("$imageReference")
49+
}
4750
}
4851
""".trimIndent()
4952
)

src/testFeatures/kotlin/dev/codebandits/container/gradle/TaskImagesTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ class TaskImagesTest : GradleProjectTest() {
109109
"/var/run/docker.sock:/var/run/docker.sock:ro",
110110
)
111111
}
112+
doLast {
113+
container.outputs.captureLocalImage("$imageReference")
114+
}
112115
}
113116
}
114117
""".trimIndent()

src/testToolIntegrations/kotlin/dev/codebandits/container/gradle/BuildpacksIntegrationTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ class BuildpacksIntegrationTest : GradleProjectTest() {
6161
"/var/run/docker.sock:/var/run/docker.sock:ro",
6262
)
6363
}
64+
doLast {
65+
container.outputs.captureLocalImage("$imageReference")
66+
}
6467
}
6568
}
6669
""".trimIndent()

0 commit comments

Comments
 (0)