|
| 1 | +#!/usr/bin/env kotlin |
| 2 | +@file:DependsOn("io.github.typesafegithub:github-workflows-kt:1.13.0") |
| 3 | + |
| 4 | +import io.github.typesafegithub.workflows.actions.actions.CheckoutV4 |
| 5 | +import io.github.typesafegithub.workflows.actions.actions.SetupJavaV4 |
| 6 | +import io.github.typesafegithub.workflows.actions.gradle.ActionsSetupGradleV3 |
| 7 | +import io.github.typesafegithub.workflows.annotations.ExperimentalKotlinLogicStep |
| 8 | +import io.github.typesafegithub.workflows.domain.RunnerType.UbuntuLatest |
| 9 | +import io.github.typesafegithub.workflows.domain.triggers.PullRequest |
| 10 | +import io.github.typesafegithub.workflows.domain.triggers.Push |
| 11 | +import io.github.typesafegithub.workflows.dsl.expressions.Contexts |
| 12 | +import io.github.typesafegithub.workflows.dsl.workflow |
| 13 | +import io.github.typesafegithub.workflows.yaml.writeToFile |
| 14 | +import java.net.URI |
| 15 | +import java.net.ConnectException |
| 16 | +import java.net.http.HttpClient |
| 17 | +import java.net.http.HttpRequest |
| 18 | +import java.net.http.HttpResponse.BodyHandlers |
| 19 | +import kotlin.time.Duration.Companion.minutes |
| 20 | +import kotlin.time.TimeSource |
| 21 | + |
| 22 | +val GRADLE_ENCRYPTION_KEY by Contexts.secrets |
| 23 | + |
| 24 | +@OptIn(ExperimentalKotlinLogicStep::class) |
| 25 | +workflow( |
| 26 | + name = "End-to-end tests", |
| 27 | + on = listOf( |
| 28 | + Push(branches = listOf("main")), |
| 29 | + PullRequest(), |
| 30 | + ), |
| 31 | + sourceFile = __FILE__.toPath(), |
| 32 | +) { |
| 33 | + job( |
| 34 | + id = "test-jit-server", |
| 35 | + name = "Test Just-In-Time bindings server", |
| 36 | + runsOn = UbuntuLatest, |
| 37 | + ) { |
| 38 | + // Using bundled bindings to avoid generating them here. |
| 39 | + uses(action = CheckoutV4()) |
| 40 | + uses( |
| 41 | + name = "Set up JDK", |
| 42 | + action = SetupJavaV4( |
| 43 | + javaVersion = "11", |
| 44 | + distribution = SetupJavaV4.Distribution.Zulu, |
| 45 | + ) |
| 46 | + ) |
| 47 | + uses(action = ActionsSetupGradleV3()) |
| 48 | + |
| 49 | + run( |
| 50 | + name = "Start the server", |
| 51 | + command = "./gradlew :jit-binding-server:run &", |
| 52 | + ) |
| 53 | + |
| 54 | + run(name = "Wait for the server to respond") { |
| 55 | + val timeSource = TimeSource.Monotonic |
| 56 | + val waitStart = timeSource.markNow() |
| 57 | + val timeout = 1.minutes |
| 58 | + |
| 59 | + while (timeSource.markNow() - waitStart < timeout) { |
| 60 | + try { |
| 61 | + HttpClient.newHttpClient().send( |
| 62 | + HttpRequest |
| 63 | + .newBuilder(URI("http://0.0.0.0:8080/status")) |
| 64 | + .GET() |
| 65 | + .build(), BodyHandlers.ofString() |
| 66 | + ) |
| 67 | + println("The server is alive!") |
| 68 | + break |
| 69 | + } catch (_: ConnectException) { |
| 70 | + Thread.sleep(5000) |
| 71 | + println("The server is still starting...") |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + run( |
| 77 | + name = "Execute the script using the bindings from the server", |
| 78 | + command = """ |
| 79 | + mv .github/workflows/test-script-consuming-jit-bindings.main.do-not-compile.kts .github/workflows/test-script-consuming-jit-bindings.main.kts |
| 80 | + .github/workflows/test-script-consuming-jit-bindings.main.kts |
| 81 | + """.trimIndent(), |
| 82 | + ) |
| 83 | + } |
| 84 | +}.writeToFile() |
0 commit comments