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

fix: gradle tasks from different subproject are not isolated #337

Merged
Merged
3 changes: 0 additions & 3 deletions .github/workflows/e2e-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ jobs:
distribution: 'temurin'
java-version: '21'

- name: Build application
run: ${{ env.GRADLE_EXEC }} build

- name: Run Acceptance Tests
id: acceptance-tests
run: ${GRADLE_EXEC} runSuites
10 changes: 5 additions & 5 deletions .github/workflows/smoke-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ jobs:
restore-keys: |
${{ runner.os }}-gradle-

- name: Build And Run Smoke Tests Container
run: ${{ env.GRADLE_EXEC }} buildAndRunSmokeTestsContainer -x test
- name: Start the Smoke Test Container
run: ${{ env.GRADLE_EXEC }} startDockerContainerSmokeTest

- name: Run Smoke Test
- name: Run Smoke Tests
working-directory: server/src/test/resources/
run: ./smoke-test.sh

- name: Stop Smoke Tests Container
- name: Stop Smoke Test Container
if: always()
run: docker compose -p block-node stop
run: ${{ env.GRADLE_EXEC }} stopDockerContainer

- name: Print Server Container Logs
if: always()
Expand Down
8 changes: 5 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ gradle-app.setting

.idea
.DS_Store
# .env files
server/docker/.env
/server/data/

# .env files (from anywhere)
.env

server/data/

# manual test files
server/src/test/resources/test_output/
Expand Down
106 changes: 59 additions & 47 deletions server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,79 +63,91 @@ tasks.register("bumpVersion") {
replaceVersion("gradle.properties", "(?<=^version=).+")
}

// Vals
val dockerProjectRootDirectory: Directory = layout.projectDirectory.dir("docker")
val dockerBuildRootDirectory: Directory = layout.buildDirectory.dir("docker").get()

// Docker related tasks
val copyDockerFolder: TaskProvider<Copy> =
tasks.register<Copy>("copyDockerFolder") {
description = "Copies the docker folder to the build root directory"
group = "docker"

var updateDockerEnv =
tasks.register<Exec>("updateDockerEnv") {
description =
"Creates the .env file in the docker folder that contains environment variables for docker"
from(dockerProjectRootDirectory)
into(dockerBuildRootDirectory)
}

// @todo(#343) - createProductionDotEnv is temporary and used by the suites,
// once the suites no longer rely on the .env file from the build root we
// should remove this task
val createProductionDotEnv: TaskProvider<Exec> =
tasks.register<Exec>("createProductionDotEnv") {
description = "Creates the default dotenv file for the Block Node Server"
group = "docker"

workingDir(layout.projectDirectory.dir("docker"))
dependsOn(copyDockerFolder)
workingDir(dockerBuildRootDirectory)
commandLine("sh", "-c", "./update-env.sh ${project.version} false false")
}

tasks.register<Exec>("createDockerImage") {
description = "Creates the docker image of the Block Node Server based on the current version"
group = "docker"
val createDockerImage: TaskProvider<Exec> =
tasks.register<Exec>("createDockerImage") {
description =
"Creates the production docker image of the Block Node Server based on the current version"
group = "docker"

dependsOn(updateDockerEnv, tasks.assemble)
workingDir(layout.projectDirectory.dir("docker"))
commandLine("./docker-build.sh", project.version, layout.projectDirectory.dir("..").asFile)
}
dependsOn(copyDockerFolder, tasks.assemble)
workingDir(dockerBuildRootDirectory)
commandLine("sh", "-c", "./docker-build.sh ${project.version}")
}

tasks.register<Exec>("startDockerContainer") {
description = "Starts the docker container of the Block Node Server of the current version"
description =
"Starts the docker production container of the Block Node Server for the current version"
group = "docker"

dependsOn(updateDockerEnv)
workingDir(layout.projectDirectory.dir("docker"))
commandLine("sh", "-c", "docker-compose -p block-node up -d")
dependsOn(createDockerImage)
workingDir(dockerBuildRootDirectory)
commandLine(
"sh",
"-c",
"./update-env.sh ${project.version} false false && docker compose -p block-node up -d"
)
}

tasks.register<Exec>("startDockerDebugContainer") {
description = "Starts the docker container of the Block Node Server of the current version"
tasks.register<Exec>("startDockerContainerDebug") {
description =
"Starts the docker debug container of the Block Node Server for the current version"
group = "docker"

workingDir(layout.projectDirectory.dir("docker"))

dependsOn(createDockerImage)
workingDir(dockerBuildRootDirectory)
commandLine(
"sh",
"-c",
"./update-env.sh ${project.version} true false && docker compose -p block-node up -d"
)
}

tasks.register<Exec>("stopDockerContainer") {
description = "Stops running docker containers of the Block Node Server"
tasks.register<Exec>("startDockerContainerSmokeTest") {
description =
"Starts the docker smoke test container of the Block Node Server for the current version"
group = "docker"

dependsOn(updateDockerEnv)
workingDir(layout.projectDirectory.dir("docker"))
commandLine("sh", "-c", "docker-compose -p block-node stop")
dependsOn(createDockerImage)
workingDir(dockerBuildRootDirectory)
commandLine(
"sh",
"-c",
"./update-env.sh ${project.version} false true && docker compose -p block-node up -d"
)
}

tasks.register("buildAndRunSmokeTestsContainer") {
doFirst {
// ensure smoke test .env properties before creating the container
exec {
workingDir(layout.projectDirectory.dir("docker"))
commandLine("sh", "-c", "./update-env.sh ${project.version} false true")
}
}
tasks.register<Exec>("stopDockerContainer") {
description = "Stops running docker containers of the Block Node Server"
group = "docker"

// build the project
dependsOn(tasks.build)

doLast {
// build and start smoke test container
exec {
workingDir(layout.projectDirectory.dir("docker"))
commandLine(
"sh",
"-c",
"./docker-build.sh ${project.version} && docker compose -p block-node up -d"
)
}
}
dependsOn(copyDockerFolder)
workingDir(dockerBuildRootDirectory)
commandLine("sh", "-c", "docker compose -p block-node stop")
}
9 changes: 5 additions & 4 deletions server/docker/docker-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ fi

VERSION=$1

echo "CREATING CONTAINER FOR VERSION ${VERSION}"
echo "Using project directory: ${2}"
echo "Building image [block-node-server:${VERSION}]"
echo

# run docker build
echo "Building container:"
docker buildx build --load -t "block-node-server:${VERSION}" --build-context distributions=../build/distributions --build-arg VERSION="${VERSION}" . || exit "${?}"
docker buildx build --load -t "block-node-server:${VERSION}" --build-context distributions=../distributions --build-arg VERSION="${VERSION}" . || exit "${?}"

echo
echo "Image [block-node-server:${VERSION}] built successfully!"
2 changes: 0 additions & 2 deletions server/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

services:
block-node-server:
container_name: block-node-server
Expand Down
4 changes: 3 additions & 1 deletion server/docker/update-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# This script is called by gradle and get the current project version as an input param

if [ $# -lt 1 ]; then
echo "USAGE: $0 <VERSION> <DEBUG> <SMOKE_TEST>"
# <VERSION> is required!
echo "USAGE: $0 <VERSION> [DEBUG] [SMOKE_TEST]"
exit 1
fi

Expand Down Expand Up @@ -37,3 +38,4 @@ fi
# Output the values
echo ".env properties:"
cat .env
echo
24 changes: 3 additions & 21 deletions suites/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,13 @@ mainModuleInfo {
// workaround until https://github.com/hashgraph/hedera-block-node/pull/216 is integrated
dependencies.constraints { implementation("org.slf4j:slf4j-api:2.0.6") }

val updateDockerEnv =
tasks.register<Exec>("updateDockerEnv") {
description =
"Creates the .env file in the docker folder that contains environment variables for Docker"
group = "docker"

workingDir(layout.projectDirectory.dir("../server/docker"))
commandLine("sh", "-c", "./update-env.sh ${project.version} false false")
}

// Task to build the Docker image
tasks.register<Exec>("createDockerImage") {
description = "Creates the Docker image of the Block Node Server based on the current version"
group = "docker"

dependsOn(updateDockerEnv, tasks.assemble)
workingDir(layout.projectDirectory.dir("../server/docker"))
commandLine("./docker-build.sh", project.version, layout.projectDirectory.dir("..").asFile)
}

tasks.register<Test>("runSuites") {
description = "Runs E2E Test Suites"
group = "suites"
modularity.inferModulePath = false
dependsOn("createDockerImage")

// @todo(#343) - :server:createProductionDotEnv should disappear
dependsOn(":server:createDockerImage", ":server:createProductionDotEnv")

useJUnitPlatform()
testLogging { events("passed", "skipped", "failed") }
Expand Down
23 changes: 13 additions & 10 deletions suites/src/main/java/com/hedera/block/suites/BaseSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@
* <p>The Block Node Application version is retrieved dynamically from an environment file (.env).
*/
public abstract class BaseSuite {
/**
* Dotenv instance to load the environment variables from the .env file that
* is inside the build root of the :server.
*/
// @todo(#343) - do not use build/environment related files from other
// projects directly like that, the SERVER_DOTENV should be constructed
// in another way
protected static final Dotenv SERVER_DOTENV = Dotenv.configure()
.directory("../server/build/docker")
.filename(".env")
.load();

/** Container running the Block Node Application */
protected static GenericContainer<?> blockNodeContainer;
Expand Down Expand Up @@ -92,6 +103,7 @@ public static void setup() {
public static void teardown() {
if (blockNodeContainer != null) {
blockNodeContainer.stop();
blockNodeContainer.close();
}
if (executorService != null) {
executorService.shutdownNow();
Expand Down Expand Up @@ -175,18 +187,9 @@ protected static Configuration loadSimulatorDefaultConfiguration() throws IOExce
/**
* Retrieves the Block Node server version from the .env file.
*
* <p>This method loads the .env file from the "../server/docker" directory and extracts the
* value of the "VERSION" environment variable, which represents the version of the Block Node
* server to be used in the container.
*
* @return the version of the Block Node server as a string
*/
private static String getBlockNodeVersion() {
Dotenv dotenv = Dotenv.configure()
.directory("../server/docker")
.filename(".env")
.load();

return dotenv.get("VERSION");
return SERVER_DOTENV.get("VERSION");
}
}
Loading