Skip to content

Commit

Permalink
set repository to publish docker image
Browse files Browse the repository at this point in the history
  • Loading branch information
manudevelopia committed Sep 14, 2024
1 parent ea2920b commit c610acc
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 14 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ For a more in-depth explanation of this issue, refer to the following resources:

```groovy
plugins {
id 'info.developia.gradle.docker.slimjar' version '1.0.1'
id 'info.developia.gradle.docker.slimjar' version '1.0.2'
}
By default, the plugin uses the rootProject.name and version to build the image name. You can override this by adding a docker block to your build.gradle file.
By default, the plugin uses the rootProject.name and 'latest' version to build the image name. You can override this by adding a docker block to your build.gradle file.
```
docker {
repository 'my-repository'
image 'my-application'
version '1.0.0'
dockerfile 'Dockerfile'
}
```
3 changes: 2 additions & 1 deletion plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ repositories {

description = "Developia docker slim jar gradle plugin"
group = 'info.developia.gradle.docker.slimjar'
version = '1.0.1'
version = '1.0.2'

dependencies {
compileOnly 'org.codehaus.groovy:groovy:3.0.19'
testImplementation 'org.spockframework:spock-core:2.2-groovy-3.0'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class GradleDockerPluginFunctionalTest extends Specification {

def "can run task"() {
given:
String repository = "my-repository"
String image = "my-project"
String version = "0.0.1"
settingsFile << ""
Expand All @@ -33,6 +34,7 @@ class GradleDockerPluginFunctionalTest extends Specification {
runtimeClasspath
}
docker {
repository = '$repository'
image = '$image'
version = '$version'
dockerfile = 'Dockerfile'
Expand All @@ -43,14 +45,14 @@ class GradleDockerPluginFunctionalTest extends Specification {
"""

when:
def runner = GradleRunner.create()
runner.forwardOutput()
runner.withPluginClasspath()
runner.withArguments("slimjar")
runner.withProjectDir(projectDir)
def result = runner.build()
def result = GradleRunner.create()
.forwardOutput()
.withPluginClasspath()
.withArguments("slimjar")
.withProjectDir(projectDir)
.build()

then:
result.output.containsIgnoreCase("Slim Jar Docker image $image:$version has been created")
result.output.containsIgnoreCase("Slim Jar Docker image $repository/$image:$version has been created")
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package info.developia.gradle.docker.slimjar

class DockerExtension {
String repository
String image
String version
String dockerfile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import org.gradle.api.tasks.TaskAction
import static SlimJarDockerPlugin.LIBS_FOLDER

class DockerTask extends DefaultTask {
@Input
String repository
@Input
String image
@Input
Expand All @@ -32,7 +34,7 @@ class DockerTask extends DefaultTask {
project.copy {
from project.configurations.runtimeClasspath
into project.layout.buildDirectory.dir(destinationFolder)
duplicatesStrategy 'exclude'
duplicatesStrategy
}
}

Expand All @@ -47,11 +49,11 @@ class DockerTask extends DefaultTask {
}

private void createImage() {
println "Building Docker image $image:$version from $dockerfile"
println "Building Docker image $repository$image:$version from $dockerfile"
project.exec {
workingDir project.rootDir
executable 'docker'
args 'build', '-f', dockerfile, '.', '-t', "$image:$version"
args 'build', '-f', dockerfile, '.', '-t', "$repository/$image:$version"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package info.developia.gradle.docker.slimjar

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction

class PublishTask extends DefaultTask {
@Input
String repository
@Input
String image
@Input
String version

@TaskAction
def run() {
push()
}

private void push() {
println "Publishing Docker image $repository$image:$version"
project.exec {
workingDir project.rootDir
executable 'docker'
args 'push', "$repository/$image:$version"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,30 @@ class SlimJarDockerPlugin implements Plugin<Project> {
def dockerExtension = createExtension(project)
project.tasks.register("slimjar", DockerTask) {
group = "docker"
repository = dockerExtension.repository
image = dockerExtension.image
version = dockerExtension.version
dockerfile = dockerExtension.dockerfile
destinationFolder = dockerExtension.destinationFolder
doLast {
println "Slim Jar Docker image $image:$version has been created"
println "Slim Jar Docker image $repository/$image:$version has been created"
}
}
project.tasks.register("publish", PublishTask) {
group = "docker"
repository = dockerExtension.repository
image = dockerExtension.image
version = dockerExtension.version
doLast {
println "Docker image $repository/$image:$version has been published"
}
}
}

static DockerExtension createExtension(Project project) {
def extension = project.extensions.create('docker', DockerExtension)
extension.with {
repository = 'library'
image = project.rootProject.name
version = 'latest'
dockerfile = 'docker/Dockerfile'
Expand Down

0 comments on commit c610acc

Please sign in to comment.