Skip to content

Commit

Permalink
fix: ci
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmelati committed Jan 15, 2025
1 parent 8e81a6d commit a3da872
Showing 1 changed file with 114 additions and 2 deletions.
116 changes: 114 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import java.io.ByteArrayOutputStream

plugins {
// this is necessary to avoid the plugins to be loaded multiple times
// in each subproject's classloader
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.androidLibrary) apply false
alias(libs.plugins.jetbrainsCompose) apply false
Expand Down Expand Up @@ -68,3 +68,115 @@ subprojects {
}
}
}

tasks.register("checkDockerStatus") {
group = "docker"
description = "Checks if Docker containers are running"
doLast {
val output = ByteArrayOutputStream()
val process = exec {
commandLine("docker", "compose", "ps", "-q", "db", "local-kms-db")
isIgnoreExitValue = true
standardOutput = output
}

val containersRunning = process.exitValue == 0 && output.toString().trim().isNotEmpty()
project.ext.set("containersRunning", containersRunning)

if (containersRunning) {
println("Required Docker containers are already running")
} else {
println("Required Docker containers are not running")
}
}
}

tasks.register("dockerCleanup") {
group = "docker"
description = "Stops and removes specific Docker containers"
doLast {
exec {
commandLine("docker", "compose", "rm", "-fsv", "db", "local-kms-db")
}
}
}

fun waitForDatabase() {
var ready = false
var attempts = 0
val maxAttempts = 30

while (!ready && attempts < maxAttempts) {
try {
val process = exec {
commandLine("docker", "compose", "exec", "-T", "db", "pg_isready", "-U", "postgres")
isIgnoreExitValue = true
}
ready = process.exitValue == 0
} catch (e: Exception) {
}

if (!ready) {
attempts++
Thread.sleep(2000)
println("Waiting for database to be ready... (Attempt $attempts/$maxAttempts)")
}
}

if (!ready) {
throw GradleException("Database failed to become ready within the timeout period")
}
println("Database is ready!")
}

tasks.register("waitForDatabase") {
group = "docker"
description = "Waits for the database to be ready"
doLast {
waitForDatabase()
}
}

tasks.register("dockerStart") {
group = "docker"
description = "Starts specific Docker containers"
doLast {
exec {
commandLine("docker", "compose", "up", "-d", "db", "local-kms-db")
}
waitForDatabase()
}
}

tasks.register("dockerEnsureRunning") {
group = "docker"
description = "Ensures Docker containers are running, starting them if needed"
dependsOn("checkDockerStatus")

doLast {
if (!project.ext.has("containersRunning") || !project.ext.get("containersRunning").toString().toBoolean()) {
exec {
commandLine("docker", "compose", "rm", "-fsv", "db", "local-kms-db")
}
exec {
commandLine("docker", "compose", "up", "-d", "db", "local-kms-db")
}
}
waitForDatabase()
project.ext.set("containersRunning", true)
}
}

subprojects {
tasks.matching { it.name == "build" }.configureEach {
dependsOn(rootProject.tasks.named("dockerEnsureRunning"))

doFirst {
if (!rootProject.ext.has("containersRunning") || !rootProject.ext.get("containersRunning").toString()
.toBoolean()
) {
throw GradleException("Docker containers are not running. Please run './gradlew dockerEnsureRunning' first.")
}
}
}
}

0 comments on commit a3da872

Please sign in to comment.