From ad18213660a3e409f10411985e4afca9b753a6f8 Mon Sep 17 00:00:00 2001 From: Atanas Atanasov Date: Tue, 19 Nov 2024 14:01:32 +0200 Subject: [PATCH] fix: gradle tasks from different subproject are not isolated (#337) Signed-off-by: Atanas Atanasov --- .github/workflows/e2e-tests.yaml | 3 - .github/workflows/smoke-test.yaml | 10 +- .gitignore | 8 +- server/build.gradle.kts | 106 ++++++++++-------- server/docker/docker-build.sh | 9 +- server/docker/docker-compose.yml | 2 - server/docker/update-env.sh | 4 +- suites/build.gradle.kts | 24 +--- .../com/hedera/block/suites/BaseSuite.java | 23 ++-- 9 files changed, 93 insertions(+), 96 deletions(-) diff --git a/.github/workflows/e2e-tests.yaml b/.github/workflows/e2e-tests.yaml index db0b22dd0..07494b9d4 100644 --- a/.github/workflows/e2e-tests.yaml +++ b/.github/workflows/e2e-tests.yaml @@ -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 diff --git a/.github/workflows/smoke-test.yaml b/.github/workflows/smoke-test.yaml index 8bc38e220..955d18e59 100644 --- a/.github/workflows/smoke-test.yaml +++ b/.github/workflows/smoke-test.yaml @@ -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() diff --git a/.gitignore b/.gitignore index 3d41d1db2..f78eebcac 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/server/build.gradle.kts b/server/build.gradle.kts index ca5357d30..24aad29a3 100644 --- a/server/build.gradle.kts +++ b/server/build.gradle.kts @@ -63,42 +63,65 @@ 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 = + tasks.register("copyDockerFolder") { + description = "Copies the docker folder to the build root directory" + group = "docker" -var updateDockerEnv = - tasks.register("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 = + tasks.register("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("createDockerImage") { - description = "Creates the docker image of the Block Node Server based on the current version" - group = "docker" +val createDockerImage: TaskProvider = + tasks.register("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("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("startDockerDebugContainer") { - description = "Starts the docker container of the Block Node Server of the current version" +tasks.register("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", @@ -106,36 +129,25 @@ tasks.register("startDockerDebugContainer") { ) } -tasks.register("stopDockerContainer") { - description = "Stops running docker containers of the Block Node Server" +tasks.register("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("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") } diff --git a/server/docker/docker-build.sh b/server/docker/docker-build.sh index 1273284f9..c31c5bad4 100755 --- a/server/docker/docker-build.sh +++ b/server/docker/docker-build.sh @@ -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!" diff --git a/server/docker/docker-compose.yml b/server/docker/docker-compose.yml index 9688ec14f..d182b4e3b 100644 --- a/server/docker/docker-compose.yml +++ b/server/docker/docker-compose.yml @@ -1,5 +1,3 @@ -version: '3.8' - services: block-node-server: container_name: block-node-server diff --git a/server/docker/update-env.sh b/server/docker/update-env.sh index 98e282d63..9594fcf8d 100755 --- a/server/docker/update-env.sh +++ b/server/docker/update-env.sh @@ -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 " + # is required! + echo "USAGE: $0 [DEBUG] [SMOKE_TEST]" exit 1 fi @@ -37,3 +38,4 @@ fi # Output the values echo ".env properties:" cat .env +echo \ No newline at end of file diff --git a/suites/build.gradle.kts b/suites/build.gradle.kts index aea9374ed..85af453d4 100644 --- a/suites/build.gradle.kts +++ b/suites/build.gradle.kts @@ -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("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("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("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") } diff --git a/suites/src/main/java/com/hedera/block/suites/BaseSuite.java b/suites/src/main/java/com/hedera/block/suites/BaseSuite.java index 54408c41d..9631a7d54 100644 --- a/suites/src/main/java/com/hedera/block/suites/BaseSuite.java +++ b/suites/src/main/java/com/hedera/block/suites/BaseSuite.java @@ -50,6 +50,17 @@ *

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; @@ -92,6 +103,7 @@ public static void setup() { public static void teardown() { if (blockNodeContainer != null) { blockNodeContainer.stop(); + blockNodeContainer.close(); } if (executorService != null) { executorService.shutdownNow(); @@ -175,18 +187,9 @@ protected static Configuration loadSimulatorDefaultConfiguration() throws IOExce /** * Retrieves the Block Node server version from the .env file. * - *

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"); } }