Skip to content

Commit

Permalink
Register push tasks for every image definition
Browse files Browse the repository at this point in the history
These push tasks allow to specify --name and --tag options
  • Loading branch information
SgtSilvio committed Feb 29, 2024
1 parent 8903576 commit 860b624
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,48 @@ package io.github.sgtsilvio.gradle.oci

import io.github.sgtsilvio.gradle.oci.attributes.DISTRIBUTION_TYPE_ATTRIBUTE
import io.github.sgtsilvio.gradle.oci.dsl.OciExtension
import io.github.sgtsilvio.gradle.oci.internal.concatCamelCase
import io.github.sgtsilvio.gradle.oci.internal.dsl.OciExtensionImpl
import io.github.sgtsilvio.gradle.oci.internal.mainToEmpty
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.invoke
import org.gradle.kotlin.dsl.register

/**
* @author Silvio Giebl
*/
class OciPlugin : Plugin<Project> {

override fun apply(project: Project) {
project.extensions.create(OciExtension::class, EXTENSION_NAME, OciExtensionImpl::class)
project.dependencies.attributesSchema.attribute(DISTRIBUTION_TYPE_ATTRIBUTE)
val extension = project.extensions.create(OciExtension::class, EXTENSION_NAME, OciExtensionImpl::class)
registerPushTasks(project, extension)
}

private fun registerPushTasks(project: Project, extension: OciExtension) {
extension.imageDefinitions.all {
val imageDefName = name
val pushName = "push".concatCamelCase(name.mainToEmpty())
val pushTask = project.tasks.register<OciPushSingleTask>(pushName.concatCamelCase("ociImage"))
val pushImageDependencies = extension.imageDependencies.create(pushName) {
add(project) {
capabilities {
for (capability in capabilities.set.get()) {
requireCapability("${capability.group}:${capability.name}")
}
}
}.name(pushTask.flatMap { it.imageName }).tag(pushTask.flatMap { it.imageTags })
}
pushTask {
group = TASK_GROUP_NAME
description = "Pushes the $imageDefName OCI image to a registry."
from(pushImageDependencies)
}
}
}
}

const val EXTENSION_NAME = "oci"
const val TASK_GROUP_NAME = "oci"
const val TASK_GROUP_NAME = "oci"
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.sgtsilvio.gradle.oci

import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.options.Option
import org.gradle.kotlin.dsl.property
import org.gradle.kotlin.dsl.setProperty
import org.gradle.work.DisableCachingByDefault
import org.gradle.workers.WorkerExecutor
import javax.inject.Inject

/**
* @author Silvio Giebl
*/
@DisableCachingByDefault(because = "Pushing to an external registry")
abstract class OciPushSingleTask @Inject constructor(workerExecutor: WorkerExecutor) : OciPushTask(workerExecutor) {

@get:Internal
@get:Option(
option = "name",
description = "Names the image. If not specified the imageName defined in the image definition is used.",
)
val imageName = project.objects.property<String>()

@get:Internal
@get:Option(
option = "tag",
description = "Tags the image. Option can be specified multiple times. The value '.' translates to the imageTag defined in the image definition. If not specified the imageTag defined in the image definition is used.",
)
val imageTags = project.objects.setProperty<String>()
}

0 comments on commit 860b624

Please sign in to comment.