An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
fix: preserve Docker build cache by excluding dynamic variables from build-time contexts
Andras Bacsai4e896cca05a3
diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.phpindex 297585562b..8c1769181f 100644--- a/app/Jobs/ApplicationDeploymentJob.php+++ b/app/Jobs/ApplicationDeploymentJob.php@@ -1363,7 +1363,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue$envs_base64 = base64_encode($environment_variables->implode("\n"));// Write .env file to workdir (for container runtime)- $this->application_deployment_queue->addLogEntry('Creating .env file with runtime variables for build phase.', hidden: true);+ $this->application_deployment_queue->addLogEntry('Creating .env file with runtime variables for container.', hidden: true);$this->execute_remote_command([executeInDocker($this->deployment_uuid, "echo '$envs_base64' | base64 -d | tee $this->workdir/.env > /dev/null"),@@ -1402,7 +1402,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue}$envs = collect([]);- $coolify_envs = $this->generate_coolify_env_variables();+ $coolify_envs = $this->generate_coolify_env_variables(forBuildTime: true);// Add COOLIFY variables$coolify_envs->each(function ($item, $key) use ($envs) {@@ -1979,7 +1979,6 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue$this->coolify_variables .= "COOLIFY_BRANCH={$this->application->git_branch} ";}$this->coolify_variables .= "COOLIFY_RESOURCE_UUID={$this->application->uuid} ";- $this->coolify_variables .= "COOLIFY_CONTAINER_NAME={$this->container_name} ";}private function check_git_if_build_needed()@@ -2230,7 +2229,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue}// Add COOLIFY_* environment variables to Nixpacks build context- $coolify_envs = $this->generate_coolify_env_variables();+ $coolify_envs = $this->generate_coolify_env_variables(forBuildTime: true);$coolify_envs->each(function ($value, $key) {$this->env_nixpacks_args->push("--env {$key}={$value}");});@@ -2238,7 +2237,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue$this->env_nixpacks_args = $this->env_nixpacks_args->implode(' ');}- private function generate_coolify_env_variables(): Collection+ private function generate_coolify_env_variables(bool $forBuildTime = false): Collection{$coolify_envs = collect([]);$local_branch = $this->branch;@@ -2273,8 +2272,11 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueueif ($this->application->environment_variables_preview->where('key', 'COOLIFY_RESOURCE_UUID')->isEmpty()) {$coolify_envs->put('COOLIFY_RESOURCE_UUID', $this->application->uuid);}- if ($this->application->environment_variables_preview->where('key', 'COOLIFY_CONTAINER_NAME')->isEmpty()) {- $coolify_envs->put('COOLIFY_CONTAINER_NAME', $this->container_name);+ // Only add COOLIFY_CONTAINER_NAME for runtime (not build-time) - it changes every deployment and breaks Docker cache+ if (! $forBuildTime) {+ if ($this->application->environment_variables_preview->where('key', 'COOLIFY_CONTAINER_NAME')->isEmpty()) {+ $coolify_envs->put('COOLIFY_CONTAINER_NAME', $this->container_name);+ }}}@@ -2311,8 +2313,11 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueueif ($this->application->environment_variables->where('key', 'COOLIFY_RESOURCE_UUID')->isEmpty()) {$coolify_envs->put('COOLIFY_RESOURCE_UUID', $this->application->uuid);}- if ($this->application->environment_variables->where('key', 'COOLIFY_CONTAINER_NAME')->isEmpty()) {- $coolify_envs->put('COOLIFY_CONTAINER_NAME', $this->container_name);+ // Only add COOLIFY_CONTAINER_NAME for runtime (not build-time) - it changes every deployment and breaks Docker cache+ if (! $forBuildTime) {+ if ($this->application->environment_variables->where('key', 'COOLIFY_CONTAINER_NAME')->isEmpty()) {+ $coolify_envs->put('COOLIFY_CONTAINER_NAME', $this->container_name);+ }}}@@ -2328,7 +2333,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue$this->env_args = collect([]);$this->env_args->put('SOURCE_COMMIT', $this->commit);- $coolify_envs = $this->generate_coolify_env_variables();+ $coolify_envs = $this->generate_coolify_env_variables(forBuildTime: true);$coolify_envs->each(function ($value, $key) {$this->env_args->put($key, $value);});@@ -2748,7 +2753,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue} else {// Traditional build args approach - generate COOLIFY_ variables locally// Generate COOLIFY_ variables locally for build args- $coolify_envs = $this->generate_coolify_env_variables();+ $coolify_envs = $this->generate_coolify_env_variables(forBuildTime: true);$coolify_envs->each(function ($value, $key) {$this->build_args->push("--build-arg '{$key}'");});@@ -3294,7 +3299,9 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf");private function generate_secrets_hash($variables){if (! $this->secrets_hash_key) {- $this->secrets_hash_key = bin2hex(random_bytes(32));+ // Use APP_KEY as deterministic hash key to preserve Docker build cache+ // Random keys would change every deployment, breaking cache even when secrets haven't changed+ $this->secrets_hash_key = config('app.key');}if ($variables instanceof Collection) {