Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker support based on Jib #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sudo: false
addons:
apt:
packages:
- oracle-java8-installer
- oracle-java9-installer

language: java

jdk:
- oraclejdk8
- oraclejdk9
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ plugins {

repositories {
jcenter()
repositories {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really needed?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh nevermind, you do need it.

Going into src/test/gradle/vertx-web-sample running gradle tasks fails because of that.

Copy link
Author

@danielpetisme danielpetisme Sep 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The com.google.cloud.tools:com.google.cloud.tools.jib.gradle.plugin seems only present in mavenCentral and not jcenter.

The Gradle plugin portal is a mirror to jcenter and Maven Central thats why it works wit the this snippet.

The options I can see:

  • Ask Google to publish the jib-gradle-plugin to jcenter (the jib maven plugin is already published). Do you know anyone able to do that?
  • Wait for the official publication, that will freeze this PR for a while
  • Update the project build files to include mavenCentral() (cleaner than pointing the plugin portal)
repositories {
  jcenter()
  mavenCentral()
  mavenLocal()
}

We should also update the documentation to explicit the need of Maven Central as a replacement/complement of JCenter.

WDYT?

maven {
url "https://plugins.gradle.org/m2/"
}
}
mavenLocal()
}

Expand All @@ -35,6 +40,7 @@ dependencies {
implementation ('com.netflix.nebula:nebula-dependency-recommender:5.2.0') {
exclude group: 'org.jetbrains.kotlin'
}
implementation 'com.google.cloud.tools.jib:com.google.cloud.tools.jib.gradle.plugin:0.9.7'

testImplementation 'junit:junit:4.12'
testImplementation 'com.mashape.unirest:unirest-java:1.4.9'
Expand Down
25 changes: 25 additions & 0 deletions src/main/kotlin/io/vertx/gradle/VertxPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package io.vertx.gradle

import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import com.google.cloud.tools.jib.gradle.JibExtension
import com.google.cloud.tools.jib.gradle.JibPlugin
import netflix.nebula.dependency.recommender.DependencyRecommendationsPlugin
import netflix.nebula.dependency.recommender.provider.RecommendationProviderContainer
import org.apache.tools.ant.taskdefs.condition.Os
Expand Down Expand Up @@ -57,6 +59,7 @@ class VertxPlugin : Plugin<Project> {
addVertxCoreDependency(project)
defineMainClassName(project)
configureShadowPlugin(project)
configureJibPlugin(project)
configureVertxRunTask(project)
configureVertxDebugTask(project)
}
Expand Down Expand Up @@ -91,6 +94,7 @@ class VertxPlugin : Plugin<Project> {
project.pluginManager.apply(ApplicationPlugin::class.java)
project.pluginManager.apply(ShadowPlugin::class.java)
project.pluginManager.apply(DependencyRecommendationsPlugin::class.java)
project.pluginManager.apply(JibPlugin::class.java)
logger.debug("The plugins needed by the Vert.x plugin have been applied")
}

Expand Down Expand Up @@ -141,6 +145,27 @@ class VertxPlugin : Plugin<Project> {
logger.debug("The shadow plugin has been configured")
}

private fun configureJibPlugin(project: Project) {
val jibExtension = project.extensions.getByName("jib") as JibExtension
val vertxExtension = project.vertxExtension()
val tag = if (project.version != Project.DEFAULT_VERSION) project.version else "latest"
jibExtension.apply {
from { imageConfiguration ->
imageConfiguration.image = "openjdk:8-jdk-alpine"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
imageConfiguration.image = "openjdk:8-jdk-alpine"
imageConfiguration.image = "openjdk:8-jre-alpine"

I think 'jre' is what we need

}
to { imageConfiguration ->
imageConfiguration.image = "${project.name}:$tag"
}
container { containerParameters ->
containerParameters.useCurrentTimestamp = true
containerParameters.jvmFlags = vertxExtension.jvmArgs
containerParameters.mainClass = vertxExtension.launcher
containerParameters.args = vertxExtension.args
}
}
logger.debug("The jib plugin has been configured")
}

private fun createVertxTasks(project: Project) {
project.tasks.create("vertxRun", JavaExec::class.java).dependsOn("classes")
project.tasks.create("vertxDebug", JavaExec::class.java).dependsOn("classes")
Expand Down
40 changes: 37 additions & 3 deletions src/test/kotlin/io/vertx/gradle/VertxPluginTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

package io.vertx.gradle

import com.mashape.unirest.http.Unirest
import org.assertj.core.api.Assertions.assertThat
import org.gradle.testkit.runner.GradleRunner
import org.junit.Test
import java.io.File
import java.util.concurrent.TimeUnit
import java.util.jar.JarFile
import com.mashape.unirest.http.Unirest
import kotlin.concurrent.thread

/**
* @author [Julien Ponge](https://julien.ponge.org/)
Expand Down Expand Up @@ -72,12 +73,45 @@ class VertxPluginTest {
assertThat(response.body).isEqualTo("Yo!")
}
}

@Test
fun `check that it builds a Docker image with Jib`() {
GradleRunner.create()
.withProjectDir(File("src/test/gradle/simple-project"))
.withPluginClasspath()
.withArguments("clean", "jibDockerBuild")
.build()
run("docker", "pull", "simple-project")
val inspection = run("docker", "inspect", "simple-project")
assertThat(inspection).containsSequence("simple-project:latest")
assertThat(inspection).containsSequence("\"io.vertx.core.Launcher\"")
}
}

private fun run(vararg command: String, block: () -> Unit) {
val process = ProcessBuilder(command.toList()).start()
private fun run(vararg command: String, block: () -> Unit = {}): String {
val process = ProcessBuilder(command.toList())
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
var stdout = ""
var stopReading = false
val readerThread = thread(start = true) {
var reader = process.inputStream.bufferedReader()
try {
var line = reader.readLine()
while (line != null && !stopReading) {
stdout += line
line = reader.readLine()
}
} catch (e: Exception) {
//Nothing to do
}
}
Thread.sleep(1000)
block()
process.destroyForcibly()
process.waitFor(30, TimeUnit.SECONDS)
stopReading = true
readerThread.join()
return stdout
}