diff --git a/.github/workflows/smoke-test.yaml b/.github/workflows/smoke-test.yaml index 8bc38e22..24676931 100644 --- a/.github/workflows/smoke-test.yaml +++ b/.github/workflows/smoke-test.yaml @@ -79,7 +79,7 @@ jobs: ${{ runner.os }}-gradle- - name: Build And Run Smoke Tests Container - run: ${{ env.GRADLE_EXEC }} buildAndRunSmokeTestsContainer -x test + run: ${{ env.GRADLE_EXEC }} startDockerContainerSmokeTest - name: Run Smoke Test working-directory: server/src/test/resources/ diff --git a/server/build.gradle.kts b/server/build.gradle.kts index 2b703e49..ca3d33df 100644 --- a/server/build.gradle.kts +++ b/server/build.gradle.kts @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Hedera Hashgraph, LLC + * Copyright (C) 2022-2024 Hedera Hashgraph, LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,60 +64,92 @@ tasks.register("bumpVersion") { } // Vals -val buildRootAbsolutePath: String = - layout.buildDirectory.get().asFile.toPath().toAbsolutePath().toString() -val buildRootEnvFileAbsolutePath: String = "$buildRootAbsolutePath/.env" -val dockerRootDirectory: Directory = layout.projectDirectory.dir("docker") +val dockerProjectRootDirectory: Directory = layout.projectDirectory.dir("docker") +val dockerBuildRootDirectory: Directory = layout.buildDirectory.dir("docker").get() // Docker related tasks -val updateDockerEnv: TaskProvider = - tasks.register("updateDockerEnv") { +val copyDockerFolder: TaskProvider = + tasks.register("copyDockerFolder") { + description = "Copies the docker folder to the build root directory" + group = "docker" + + from(dockerProjectRootDirectory) + into(dockerBuildRootDirectory) + } + +val createDockerImage: TaskProvider = + tasks.register("createDockerImage") { description = - "Creates the .env file in the docker folder that contains environment variables for docker" + "Creates the production docker image of the Block Node Server based on the current version" group = "docker" - workingDir(dockerRootDirectory) + dependsOn(copyDockerFolder, tasks.assemble) + workingDir(dockerBuildRootDirectory) commandLine( "sh", "-c", - "./update-env.sh $buildRootAbsolutePath ${project.version} false false" + "./update-env.sh ${project.version} false false && ./docker-build.sh ${project.version}" ) } -val createDockerImage: TaskProvider = - tasks.register("createDockerImage") { +val createDockerImageDebug: TaskProvider = + tasks.register("createDockerImageDebug") { description = - "Creates the docker image of the Block Node Server based on the current version" + "Creates the debug docker image of the Block Node Server based on the current version" group = "docker" - dependsOn(updateDockerEnv, tasks.assemble) - workingDir(dockerRootDirectory) - commandLine("./docker-build.sh", project.version, layout.projectDirectory.dir("..").asFile) + dependsOn(copyDockerFolder, tasks.assemble) + workingDir(dockerBuildRootDirectory) + commandLine( + "sh", + "-c", + "./update-env.sh ${project.version} true false && ./docker-build.sh ${project.version}" + ) + } + +val createDockerImageSmokeTest: TaskProvider = + tasks.register("createDockerImageSmokeTest") { + description = + "Creates the smoke tests docker image of the Block Node Server based on the current version" + group = "docker" + + dependsOn(copyDockerFolder, tasks.assemble) + workingDir(dockerBuildRootDirectory) + commandLine( + "sh", + "-c", + "./update-env.sh ${project.version} false true && ./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 container of the Block Node Server for the current version" group = "docker" dependsOn(createDockerImage) - workingDir(dockerRootDirectory) - commandLine( - "sh", - "-c", - "docker compose --env-file $buildRootEnvFileAbsolutePath -p block-node up -d" - ) + workingDir(dockerBuildRootDirectory) + commandLine("sh", "-c", "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" - dependsOn(createDockerImage) - workingDir(dockerRootDirectory) + dependsOn(createDockerImageDebug) + workingDir(dockerBuildRootDirectory) + commandLine("sh", "-c", "docker compose -p block-node up -d") +} + +tasks.register("startDockerContainerSmokeTest") { + description = "Starts the docker smoke test container of the Block Node Server for the current version" + group = "docker" + + dependsOn(createDockerImageSmokeTest) + workingDir(dockerBuildRootDirectory) commandLine( "sh", "-c", - "./update-env.sh $buildRootAbsolutePath ${project.version} true false && docker compose --env-file $buildRootEnvFileAbsolutePath -p block-node up -d" + "docker compose -p block-node up -d" ) } @@ -125,35 +157,7 @@ tasks.register("stopDockerContainer") { description = "Stops running docker containers of the Block Node Server" group = "docker" - workingDir(dockerRootDirectory) + dependsOn(copyDockerFolder) + workingDir(dockerBuildRootDirectory) commandLine("sh", "-c", "docker compose -p block-node stop") } - -tasks.register("buildAndRunSmokeTestsContainer") { - doFirst { - // ensure smoke test .env properties before creating the container - exec { - workingDir(dockerRootDirectory) - commandLine( - "sh", - "-c", - "./update-env.sh $buildRootAbsolutePath ${project.version} false true" - ) - } - } - - // build the project - dependsOn(tasks.build) - - doLast { - // build and start smoke test container - exec { - workingDir(dockerRootDirectory) - commandLine( - "sh", - "-c", - "./docker-build.sh ${project.version} && docker compose --env-file $buildRootEnvFileAbsolutePath -p block-node up -d" - ) - } - } -} diff --git a/server/docker/docker-build.sh b/server/docker/docker-build.sh index d74dbc51..c31c5bad 100755 --- a/server/docker/docker-build.sh +++ b/server/docker/docker-build.sh @@ -7,11 +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 image [block-node-server:${VERSION}]" -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 fcbeccfe..d182b4e3 100644 --- a/server/docker/docker-compose.yml +++ b/server/docker/docker-compose.yml @@ -3,7 +3,7 @@ services: container_name: block-node-server image: block-node-server:${VERSION} env_file: - - ../build/.env + - .env ports: - "8080:8080" - "5005:5005" diff --git a/server/docker/update-env.sh b/server/docker/update-env.sh index 29d3bd6c..9594fcf8 100755 --- a/server/docker/update-env.sh +++ b/server/docker/update-env.sh @@ -3,21 +3,17 @@ # This scripts create a '.env' file that is used for docker & docker-compose as an input of environment variables. # This script is called by gradle and get the current project version as an input param -if [ $# -lt 2 ]; then - # and are required! - echo "USAGE: $0 [DEBUG] [SMOKE_TEST]" +if [ $# -lt 1 ]; then + # is required! + echo "USAGE: $0 [DEBUG] [SMOKE_TEST]" exit 1 fi -project_build_root=$1 -project_version=$2 +project_version=$1 # determine if we should include debug opts -[ "$3" = true ] && is_debug=true || is_debug=false +[ "$2" = true ] && is_debug=true || is_debug=false # determine if we should include smoke test env variables -[ "$4" = true ] && is_smoke_test=true || is_smoke_test=false - -# work only inside the scope of the project build root -cd "$project_build_root" || exit 1 +[ "$3" = true ] && is_smoke_test=true || is_smoke_test=false echo "VERSION=$project_version" > .env echo "REGISTRY_PREFIX=" >> .env @@ -42,3 +38,4 @@ fi # Output the values echo ".env properties:" cat .env +echo \ No newline at end of file