@@ -1363,7 +1363,7 @@ private function save_runtime_environment_variables()
13631363 $ envs_base64 = base64_encode ($ environment_variables ->implode ("\n" ));
13641364
13651365 // Write .env file to workdir (for container runtime)
1366- $ this ->application_deployment_queue ->addLogEntry ('Creating .env file with runtime variables for build phase . ' , hidden: true );
1366+ $ this ->application_deployment_queue ->addLogEntry ('Creating .env file with runtime variables for container . ' , hidden: true );
13671367 $ this ->execute_remote_command (
13681368 [
13691369 executeInDocker ($ this ->deployment_uuid , "echo ' $ envs_base64' | base64 -d | tee $ this ->workdir /.env > /dev/null " ),
@@ -1402,7 +1402,7 @@ private function generate_buildtime_environment_variables()
14021402 }
14031403
14041404 $ envs = collect ([]);
1405- $ coolify_envs = $ this ->generate_coolify_env_variables ();
1405+ $ coolify_envs = $ this ->generate_coolify_env_variables (forBuildTime: true );
14061406
14071407 // Add COOLIFY variables
14081408 $ coolify_envs ->each (function ($ item , $ key ) use ($ envs ) {
@@ -1979,7 +1979,6 @@ private function set_coolify_variables()
19791979 $ this ->coolify_variables .= "COOLIFY_BRANCH= {$ this ->application ->git_branch } " ;
19801980 }
19811981 $ this ->coolify_variables .= "COOLIFY_RESOURCE_UUID= {$ this ->application ->uuid } " ;
1982- $ this ->coolify_variables .= "COOLIFY_CONTAINER_NAME= {$ this ->container_name } " ;
19831982 }
19841983
19851984 private function check_git_if_build_needed ()
@@ -2230,15 +2229,15 @@ private function generate_nixpacks_env_variables()
22302229 }
22312230
22322231 // Add COOLIFY_* environment variables to Nixpacks build context
2233- $ coolify_envs = $ this ->generate_coolify_env_variables ();
2232+ $ coolify_envs = $ this ->generate_coolify_env_variables (forBuildTime: true );
22342233 $ coolify_envs ->each (function ($ value , $ key ) {
22352234 $ this ->env_nixpacks_args ->push ("--env {$ key }= {$ value }" );
22362235 });
22372236
22382237 $ this ->env_nixpacks_args = $ this ->env_nixpacks_args ->implode (' ' );
22392238 }
22402239
2241- private function generate_coolify_env_variables (): Collection
2240+ private function generate_coolify_env_variables (bool $ forBuildTime = false ): Collection
22422241 {
22432242 $ coolify_envs = collect ([]);
22442243 $ local_branch = $ this ->branch ;
@@ -2273,8 +2272,11 @@ private function generate_coolify_env_variables(): Collection
22732272 if ($ this ->application ->environment_variables_preview ->where ('key ' , 'COOLIFY_RESOURCE_UUID ' )->isEmpty ()) {
22742273 $ coolify_envs ->put ('COOLIFY_RESOURCE_UUID ' , $ this ->application ->uuid );
22752274 }
2276- if ($ this ->application ->environment_variables_preview ->where ('key ' , 'COOLIFY_CONTAINER_NAME ' )->isEmpty ()) {
2277- $ coolify_envs ->put ('COOLIFY_CONTAINER_NAME ' , $ this ->container_name );
2275+ // Only add COOLIFY_CONTAINER_NAME for runtime (not build-time) - it changes every deployment and breaks Docker cache
2276+ if (! $ forBuildTime ) {
2277+ if ($ this ->application ->environment_variables_preview ->where ('key ' , 'COOLIFY_CONTAINER_NAME ' )->isEmpty ()) {
2278+ $ coolify_envs ->put ('COOLIFY_CONTAINER_NAME ' , $ this ->container_name );
2279+ }
22782280 }
22792281 }
22802282
@@ -2311,8 +2313,11 @@ private function generate_coolify_env_variables(): Collection
23112313 if ($ this ->application ->environment_variables ->where ('key ' , 'COOLIFY_RESOURCE_UUID ' )->isEmpty ()) {
23122314 $ coolify_envs ->put ('COOLIFY_RESOURCE_UUID ' , $ this ->application ->uuid );
23132315 }
2314- if ($ this ->application ->environment_variables ->where ('key ' , 'COOLIFY_CONTAINER_NAME ' )->isEmpty ()) {
2315- $ coolify_envs ->put ('COOLIFY_CONTAINER_NAME ' , $ this ->container_name );
2316+ // Only add COOLIFY_CONTAINER_NAME for runtime (not build-time) - it changes every deployment and breaks Docker cache
2317+ if (! $ forBuildTime ) {
2318+ if ($ this ->application ->environment_variables ->where ('key ' , 'COOLIFY_CONTAINER_NAME ' )->isEmpty ()) {
2319+ $ coolify_envs ->put ('COOLIFY_CONTAINER_NAME ' , $ this ->container_name );
2320+ }
23162321 }
23172322 }
23182323
@@ -2328,7 +2333,7 @@ private function generate_env_variables()
23282333 $ this ->env_args = collect ([]);
23292334 $ this ->env_args ->put ('SOURCE_COMMIT ' , $ this ->commit );
23302335
2331- $ coolify_envs = $ this ->generate_coolify_env_variables ();
2336+ $ coolify_envs = $ this ->generate_coolify_env_variables (forBuildTime: true );
23322337 $ coolify_envs ->each (function ($ value , $ key ) {
23332338 $ this ->env_args ->put ($ key , $ value );
23342339 });
@@ -2748,7 +2753,7 @@ private function build_image()
27482753 } else {
27492754 // Traditional build args approach - generate COOLIFY_ variables locally
27502755 // Generate COOLIFY_ variables locally for build args
2751- $ coolify_envs = $ this ->generate_coolify_env_variables ();
2756+ $ coolify_envs = $ this ->generate_coolify_env_variables (forBuildTime: true );
27522757 $ coolify_envs ->each (function ($ value , $ key ) {
27532758 $ this ->build_args ->push ("--build-arg ' {$ key }' " );
27542759 });
@@ -3294,7 +3299,9 @@ private function generate_build_secrets(Collection $variables)
32943299 private function generate_secrets_hash ($ variables )
32953300 {
32963301 if (! $ this ->secrets_hash_key ) {
3297- $ this ->secrets_hash_key = bin2hex (random_bytes (32 ));
3302+ // Use APP_KEY as deterministic hash key to preserve Docker build cache
3303+ // Random keys would change every deployment, breaking cache even when secrets haven't changed
3304+ $ this ->secrets_hash_key = config ('app.key ' );
32983305 }
32993306
33003307 if ($ variables instanceof Collection) {
0 commit comments